How to make ode45 perform steps exactly 0.01 on the T axis?

I use Matlab to solve a differential equation. I want to make ode45 take constant steps, so it always increases 0.01 on the T axis when solving the equation. How to do it?

ode45 sequentially performs optimized, random steps, and I cannot figure out how to get it to execute sequential small steps of 0.01. Here is the code:

options= odeset('Reltol',0.001,'Stats','on'); %figure(1); %clf; init=[xo yo zo]'; tspan=[to tf]; %tspan = t0:0.01:tf; [T,Y]=ode45(name,tspan,init,options); 
+4
source share
6 answers

Based on the documentation for the ode functions , you should be able to do what you specified in the commented line:

 tspan = to:0.01:tf; %# Obtain solutions at specific times [T,Y] = ode45(name,tspan,init,options); 

EDIT:

For precision solutions, when using fixed pitch sizes, refer to this excerpt from the link above:

Specifying tspan more than two elements does not affect the internal time steps that the solver uses to cross the interval from tspan(1) to tspan(end) . All solvers in the ODE set receive output values ​​using continuous extensions of the main formula. Although the solver is not necessarily a step-by-step point specified in tspan , the solutions produced at the indicated time points have the same accuracy order, since the solutions are calculated at internal time points.

Thus, even if you indicate that you want a solution at certain points in time, solvers still internally perform a series of adaptive steps between the specified points in time, approaching the values ​​at these fixed points in time.

+11
source

ode45 invariably uses adaptive step size, the documentation fixes this problem and recommends other solvers instead of a fixed step size - see ode4 (fourth-order Runge-Kutta), which is a fairly safe bet for solving most odes - at least according to Numerical Recipes

+5
source

This can be done, just need to use a few more commands.

NEW:

 options= odeset('Reltol',0.001,'Stats','on'); %figure(1); %clf; init=[xo yo zo]'; to=some number; tf= some number; nsteps= number of points you want function evaluated on. tspan = linspace(t0,tf, nsteps); [T,Y]=ode45(@function,tspan,init,options); 

You can verify that the required numbers were required using the command size (variable).

+4
source

Well, you cannot guarantee that it will ONLY calculate these steps, but you can provide the maximum step size (and if you make it small enough, you can be almost sure that you have all the required time points and take only these samples )

 options= odeset('MaxStep',1); [t,s] = ode45(@myode,tspan,[0;0],options); 

To find out more, you can go to here :

+2
source

Your script does not indicate the size of the fixed step, it only shows that the solution will be printed based on the provided time step. Try checking the structure of the ODE solution and you will realize that it actually uses a different timestamp.

The best way to get it to use a fixed time interval is to ensure that both RelTol and AbsTol are large enough.

+1
source

ODE45 (they say four-five is not forty-five) calculates the optimal step and even returns in time, if the error is large, check Runge-Kutta if you are interested. As a user, you do not notice this, since the ODE output will be interpolated so that it matches the tspan vector. Therefore, if you set tspan to 0: 1E-5: 1, and you want to integrate a lightweight ODE such as dydt = -y, ODE45 will take a few steps, but the vector you choose from ode will be in the time steps declared in tspan i.e. 1E-5. If you want to integrate the rigid equation dydt = 1E2 * (1-y) * y, then ODE45 will take large and very small steps, but the result will be the same, since in the future you should use ode15s, because ODE45 cannot process hard systems.

Hope this helps

amuses Marco

+1
source

Source: https://habr.com/ru/post/1305613/


All Articles