Is it possible to access the value of the variable step size (of the independent variable) used by ODE45 within the ode function?

3 views (last 30 days)
If I am solving for y, which is a function of t, using ODE45, will it be possible for me to access and print the value of the variable step size dt used by ODE45? If yes, can anyone tell me how to do it? I would like to know the value of dt used during each time step, and print it as the solution progreses.
  2 Comments
Torsten
Torsten on 1 Jun 2021
Edited: Torsten on 1 Jun 2021
If you choose tspan = [tstart tend] in the call to ODE45, you will get the solution at the times the solver has chosen. The difference between T(i+1) and T(i) will be the dt variable you are looking for.
If you need the dt values during the calculation, you will have to use the OutputFcn function.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 1 Jun 2021
No, ode45 does not provide this information to the ODE function you pass into it.
Why are you interested in the steps ode45 uses internally (which may not be the same as the steps between values returned by the solver and which in fact may not be positive if a proposed step needs to be rejected because the solution does not satisfy the error tolerances?)
If you want this information to display the progress of the ODE solver use an OutputFcn. See the ballode and orbitode examples included with MATLAB for a demonstration of this technique.
If your ODE function includes delays meaning that your equation is not y' = f(t, y) but y' = f(t, y, y(t-delay1), etc.) then use a delay differential equation solver like dde23 instead.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 1 Jun 2021
Hi,
As if I undertood your question correctly, you'd like to save and display simulation iteration process, correct?
If this is the case, you can employ this syntax:
[t, Sol]=ode45(@(t,u)????, [0, ????], ICs, OPTs)
Or an alternative way:
Sol = ode45(@(t,u)????, [0, ????], ICs, OPTs) % Sol is a structure contains lots of data

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!