Ode23 is not outputting solutions at the times I specified between t0 and tf in tspan=[t0, t1, t2..., tf]

1 回表示 (過去 30 日間)
I am using ode23() to solve an ODE between t=0 and t=7, and I want solutions at every 0.1 seconds. I am calling ode23 with tspan=[0, 0.1, 0.2, ..., 7] (which is 71 entries long) but the output solution is only 38 entries long at the times chosen by the solver. It is exactly the same as when tspan=[0 7]. Why is not listening to the argument I gave?
% define ODE
function dydt = myfun(t, y, c, m, g)
v = sqrt(y(2)^2+y(4)^2);
dydt = [y(2); -c*v*y(2)/m; y(4); -c*v*y(4)/m-g];
end
% Simulate ODE
c = 0.028;
m = 0.3;
g = 9.81;
y0 = [0; 35*cosd(65); 0; 35*sind(65)];
times = [0:0.1:7];
soln = ode23(@(t, y) myfun(t, y, c, m, g), times, y0);
xc = soln.y(1, :)';
yc = soln.y(3, :)';
figure
plot(xc, yc)

採用された回答

Torsten
Torsten 2024 年 9 月 14 日
編集済み: Torsten 2024 年 9 月 14 日
If you use the "soln" structure as result, ode23 only respects start and end point from your tspan vector.
After the integrator has finished, you would have to use "deval" to interpolate the solution to the time points of your choice.
I suggest using the [T,Y] solution structure instead:
% Simulate ODE
c = 0.028;
m = 0.3;
g = 9.81;
y0 = [0; 35*cosd(65); 0; 35*sind(65)];
times = [0:0.1:7];
[T,Y] = ode23(@(t, y) myfun(t, y, c, m, g), times, y0);
xc = Y(:,1);
yc = Y(:,3);
figure
plot(xc, yc)
% define ODE
function dydt = myfun(t, y, c, m, g)
v = sqrt(y(2)^2+y(4)^2);
dydt = [y(2); -c*v*y(2)/m; y(4); -c*v*y(4)/m-g];
end
  1 件のコメント
Spencer
Spencer 2024 年 9 月 14 日
Thank you! I now see in the documentation I should have used deval() to find the values at the times I want if I was going to say soln = ode23(). I like your method of [T, Y]=ode23() better.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by