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)
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!