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

24 次查看(过去 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,20:23
编辑:Torsten 2024-9-14,20:28
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,20:30
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 CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by