Generating a random number inside a function decleration
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Geoff Hayes
el 20 de Jun. de 2016
Please quantify forever. Does it take seconds, minutes, hours? Also, what is the code that you have written to invoke this function?
Respuestas (4)
James Tursa
el 20 de Jun. de 2016
Editada: James Tursa
el 20 de Jun. de 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 comentario
Walter Roberson
el 20 de Jun. de 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.
John D'Errico
el 20 de Jun. de 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.
0 comentarios
Anderson Francisco Silva
el 31 de Ag. de 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 comentario
John D'Errico
el 31 de Ag. de 2020
This is not a stiff problem, but a stochastic problem. You cannot use ode15s to solve it.
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!