MATLAB Answers

Why does ode45 generate different sized vectors depending on how output is defined?

14 views (last 30 days)
Here is the function I am trying to solve along with the domain and initial condition:
tspan=[0 10];
func=@(t,x) -2*x+t;
If I try to solve the ode with the following format, the vectors are of size 57x1:
[t,x] = ode45(func, tspan, x0);
However, if I try to solve the ode with another format, the vectors are of size 1x15:
sol = ode45(func, tspan, x0);
The reason I want to know this is because I want the length and number of time steps during the integration. If I had to guess, I would say that it is given with the second format because if I write it like this:
options = odeset('Stats','on');
[t,x] = ode45(func, tspan, x0, options);
the following appears in the command window :
14 successful steps
2 failed attempts
97 function evaluations
This tells me that there are 14 time steps so the size of sol.t is the correct one. Am I missing something?


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Sep 2019
sol = ode45(___) returns a structure that you can use with deval to evaluate the solution at any point on the interval [t0 tf].
Effectively, when you use that syntax, ode45() simplifies the output down to something with typically fewer points that deval() would evaluate to give values within your error tolerance to the outputs of the other form of the sequence.
To get statistics when you have no event function, use
[t, y, stats] = ode45(....)
If you have an event function, then stats will be the 6th output.
The number of timesteps will be the first element of the numeric vector returned. The number of function evaluations will be the third element.

  1 Comment

Andres Morfin
Andres Morfin on 16 Sep 2019
So this output:
[t,x] = ode45(func, tspan, x0);
is doing some sort of interpolation ( with deval() ) to get the values at times that are not in the integration steps?

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by