Generating a random number inside a function decleration

5 views (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 20 Jun 2016
Please quantify forever. Does it take seconds, minutes, hours? Also, what is the code that you have written to invoke this function?
bio lim
bio lim on 20 Jun 2016
Edited: bio lim on 20 Jun 2016
By forever I meant more than twenty minutes. I have not tried running it more than that.

Sign in to comment.

Answers (4)

James Tursa
James Tursa on 20 Jun 2016
Edited: James Tursa on 20 Jun 2016
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 Comment
Walter Roberson
Walter Roberson on 20 Jun 2016
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.

Sign in to comment.


John D'Errico
John D'Errico on 20 Jun 2016
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.

Anderson Francisco Silva
Anderson Francisco Silva on 31 Aug 2020
Hello friend, try using an ODE15s solver, your problem may be rigid. And also consider good practices for these solvers at the link.
  1 Comment
John D'Errico
John D'Errico on 31 Aug 2020
This is not a stiff problem, but a stochastic problem. You cannot use ode15s to solve it.

Sign in to comment.


Bruno Luong
Bruno Luong on 31 Aug 2020
Edited: Bruno Luong on 31 Aug 2020
The right tool is not odexxx but this one is you have the toolbox.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!