milez
milez

Reputation: 2201

How to compare results from Matlab ode solvers

I am solving a differential equation system with Matlab like this

tspan = [0 10];    
res = ode15s(@(t,x) func,tspan,x0);

and I have another system that is slighlty different than the one specified in @func, solved like

res2 = ode15s(@(t,x) func2,tspan,x0);

I would like to compare the results by calculating the difference at each time step. But since Matlab solvers are variable time step, the res.y (solutions) matrices have different number of columns. How can I make the results comparable? I tried

tspan = [0:0.01:10];  

But the solvers still seem to use a variable time step. So how can I make the results comparable? Thank you in advance.

Edit: I wish to clarify, I do not need to force the solvers to take any specific steps, just need a method to interpolate the results or something. In other words, this does not help me.

Upvotes: 0

Views: 156

Answers (2)

Thomas Hardin
Thomas Hardin

Reputation: 236

Using Matlab's built-in spline interpolation:

tFine = 0:0.01:10;
resFine = interp1(res.x,res.y,tFine,'spline');
res2Fine = interp1(res2.x,res2.y,tFine,'spline');

Upvotes: 2

Mad Physicist
Mad Physicist

Reputation: 114240

If speed is not an issue, you can solve each equation multiple times with tspan set to [0 tf], where tf is the elements of [0:0.01:10]. This will guarantee that the last value of the solution is the one you need:

tsteps = [0:0.01:10]
for tf in tsteps:
    tspan = [0 tf];
    res = ode15s(@(t,x) func, tspan, x0);
    res2 = ode15s(@(t,x) func2, tspan, x0);
    % Extract whatever you need here
end
% Compare what you extracted here

Upvotes: 1

Related Questions