Generating a random number inside a function decleration
4 次查看(过去 30 天)
显示 更早的评论
I am trying to solve a first order differential equation using ode45. My function declaration is as follows.
function dx = function7(t,x)
s = rng;
maxAngle = pi/4;
minAngle = -pi/4;
theta = (maxAngle - minAngle).*rand(1) + minAngle;
dx = zeros(3,1);
v_target = 1;
dx(1) = v_target * cos(theta);
dx(2) = v_target * sin(theta);
dx(3) = theta;
end
I want my theta to be random between pi/4 to -pi4 at each time step changing randomly. In order words, I am creating a trajectory of a point at constant speed with random heading. However, the program is taking forever, and I believe that the problem lies when I declare the random variable. Thanks.
2 个评论
Geoff Hayes
2016-6-20
Please quantify forever. Does it take seconds, minutes, hours? Also, what is the code that you have written to invoke this function?
回答(4 个)
James Tursa
2016-6-20
编辑:James Tursa
2016-6-20
Is your rand method even valid for arbitrary size time steps and the intermediate derivatives that ode45 is using internally? This might be messing up the internal error management for one, since no matter how small the step size is that ode45 wants to use, the size of that random angle is still between pi/4 and -pi/4. Also, seems like you are fooling the integrator by doing this since the intermediate derivatives used by ode45 in taking a step will not be consistent. They could all be pointed in different directions, leading to ode45 thinking that the errors are still too large for the current step size and dropping the step size down again.
To do what you are trying to do, I would think it necessary to code up an RK4 or Euler integrator manually, keep the step size constant, and for each RK4 or Euler step use the same random angle for all intermediate derivatives. Looks kinda like you are essentially doing a type of random walk.
1 个评论
Walter Roberson
2016-6-20
In my experience, ode45 itself never goes backwards in time (at least not when the tspan is monotonically increasing), but that some of the other ode* routines might go backwards in time. ode45 does sometimes use the same time for subsequent iterations, unless x0 is scalar. Using a different random value for the same time is effectively a discontinuity.
John D'Errico
2016-6-20
This will cause ODE45 (or any such adaptive solver) to work terribly.
Essentially, it will see the random component of your function, and decide that it is changing rapidly, so it will DECREASE the step size. Then it sees another random number, and it will decrease it again.
Another way of looking at it is that ODE45 uses a high order integration to solve the integration. But that implies that your problem is DIFFERENTIABLE! Is a random process differentiable? Of course not!
Sorry, but this is the wrong way to solve your problem. As James points out, this is a random walk. It is not something that you want to use a tool like ODE45 on.
0 个评论
Anderson Francisco Silva
2020-8-31
Hello friend, try using an ODE15s solver, your problem may be rigid. And also consider good practices for these solvers at the link.
1 个评论
John D'Errico
2020-8-31
This is not a stiff problem, but a stochastic problem. You cannot use ode15s to solve it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!