Solving ODE just for one time step

조회 수: 6 (최근 30일)
Ari
Ari 2025년 5월 6일
댓글: Sam Chak 2025년 5월 16일
I want to solve some ODE using matlab solver (ode45, ode15s, etc) just for one time step. The time step is decided internally by the solver. Is this possible to do?
  댓글 수: 12
Sam Chak
Sam Chak 2025년 5월 16일
Hi @Ari
Could you describe the meaning of the desired "computational savings" in @John D'Errico's approach? I do not fully understand it. However, from my perspective, John's "last point" approach does not waste computational effort, as it allows the ODE solver to adaptively determine the internal time step needed for accurate numerical integration between the sampling points.
In contrast, your MPC-like "look-ahead-but-take-only-the-first-computation" approach computes one-third of the maximum time in each iteration. If possible, please provide a desired performance index so that both your approach and John’s can be fairly compared.
Sam Chak
Sam Chak 2025년 5월 16일
For example, despite the sampling time now being set to 1 second, the solution provided by the ode45 solver remains very accurate at the sampling points. The forward Euler method, however, cannot achieve the same level of accuracy with a time step of 1 second.
sol_t = 0; % initial time
sol_y = 1; % initial value
window = 1; % time window (1 sec)
tmax = 6; % intended simulation time interval
ode = @(t,y) 0.25*(1 + sin(t)) - 0.1*sqrt(y); % here 1+sin(t) is input function
while sol_t(end) < tmax
sol = ode15s(ode, [sol_t(end) sol_t(end)+window], sol_y(end));
sol_t = [sol_t sol.x(end)];
sol_y = [sol_y sol.y(end)];
end
figure
disp(sol_t)
0 1 2 3 4 5 6
% discard the final solution point because it exceeds tmax
plot(sol_t(1:end), sol_y(1:end), ':o'), grid on
title('Solution provided by the ode45 solver')
xlabel('t'), ylabel('y(t)')

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2025년 5월 7일
편집: John D'Errico 2025년 5월 7일
Then I fail to see the problem.
You want only the solution at the end point.
% A simple ODE. with y(0) == 1, the analytical solution is just 2*exp(t)-t-1
odefun = @(t,y) t + y;
[t,y] = ode45(odefun,[0,2],1)
t = 41×1
0 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = 41×1
1.0000 1.0525 1.1103 1.1737 1.2428 1.3181 1.3997 1.4881 1.5836 1.6866
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Multiple time steps were generated. So what? TAKE THE LAST ONE.
y = y(end)
y = 11.7781
Is it correct?
2*exp(2) - 2 - 1
ans = 11.7781
What is the problem? This works, basically always. It gave you more information than you wanted, wringing your hands with worry. Take the result you want to see, and ignore the rest.
If it really, really, really, desperately, upsets you that you need to do that extra step, then wrap the ODE solver in a caller function, that does exactly that, returning only the final element of y. Again, what is the problem?
function yfinal = myode45(odefun,tspan,y0)
[t,y] = ode45(odefun,tspan,y0);
yfinal = y(end);
end
myode45(odefun,[0,2],1)
ans = 11.7781

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by