ode45: use span of length 2

조회 수: 17 (최근 30일)
Valeria Iegorova
Valeria Iegorova 2021년 2월 24일
답변: Steven Lord 2021년 2월 24일
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.

답변 (2개)

Star Strider
Star Strider 2021년 2월 24일
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
Valeria Iegorova
Valeria Iegorova 2021년 2월 24일
I think that my question was misunderstood.
Your code returns 15 values as sol. This definitely does not look like the solution evaluated at only 2 points 0.0 and 1.0.
If I have tspan of length 3, I get sol of length 3, just as I want. The same for any other length of tspan, except for 2. Once the tspan is of length 2, the solution vector gets much larger.
Star Strider
Star Strider 2021년 2월 24일
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
Steven Lord 2021년 2월 24일
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.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by