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 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