ode45: use span of length 2
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Good afternoon.
I am trying to use ode45(...) function to approximate the solution to the differential equation. Moreover, I am trying to do it with different spans by using different step sizes in the span.
So, I am changing the parameter dt:
tspan = 0:dt:1;
ode_func = @(t, x, flag) 1 - x + t;
[~, sol] = ode45(ode_func, tspan, x0);
Where dt takes values from `[0.001:0.001:0.01 0.02:0.01:0.1 0.1:0.1:0.5 1.0].
The problem is that, when I take the dt=1.0. The tspan is then just [0 1], and the ode45() funcion takes it as just start and end points of the range (and calculates the span of 41 values instead of using just these 2 values).
How to force ode45 to use JUST THESE values? I understand that it doesn't make much sense, but I want to plot a graph of dependency of the errors on that step size.
0 commentaires
Réponses (2)
Star Strider
le 24 Fév 2021
The tspan argument can be anything you want it to be (within limits).
To have ode45 to evaluate and output at only those values:
tspan = [0.001:0.001:0.01 0.02:0.01:0.1 0.1:0.1:0.5 1.0];
tspan = unique(tspan);
ode_func = @(t, x, flag) 1 - x + t;
x0 = 0;
[~, sol] = ode45(ode_func, tspan, x0);
figure
plot(tspan, sol, '-p')
grid
xlabel('Time')
ylabel('x')
The unique call is necessary because of some repeated (duplicated) values in the original vector.
2 commentaires
Star Strider
le 24 Fév 2021
It actually returns 24 elements, because that is the number of elements in tspan, as your vector defines them.
The MATLAB ODE solvers are adaptive, and given only 2 elements for tspan, return as many solutions as the algorithm determines that it needs to correctly evaluate the system. Given a tspan vector of more than 2 elements, it reports (interpolates) solutions at only those time points, although it evaluates the same way between the lower and upper limits of the tspan vector.
Steven Lord
le 24 Fév 2021
If you want to only evaluate the solution of your ODE at a specific set of times even if that set of times has only two elements (which instead triggers the behavior where the ODE solver chooses at which points to return the solution) call the ODE solver with one output argument and pass that output argument along with the vector of times at which you want to evaluate the solution into deval.
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!