Generating a random number inside a function decleration

조회 수: 4 (최근 30일)
bio lim
bio lim 2016년 6월 20일
댓글: John D'Errico 2020년 8월 31일
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
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?
bio lim
bio lim 2016년 6월 20일
편집: bio lim 2016년 6월 20일
By forever I meant more than twenty minutes. I have not tried running it more than that.

댓글을 달려면 로그인하십시오.

답변 (4개)

James Tursa
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
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
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.

Anderson Francisco Silva
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
John D'Errico 2020년 8월 31일
This is not a stiff problem, but a stochastic problem. You cannot use ode15s to solve it.

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2020년 8월 31일
편집: Bruno Luong 2020년 8월 31일
The right tool is not odexxx but this one is you have the toolbox.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by