How to pass a function handle as an argument in ode45?
조회 수: 2 (최근 30일)
이전 댓글 표시
Consider the following code:
R = 0.5;
l = 1;
g = 9.81;
omega = 0.25;
F = @(t,y) [y(2), (R*omega^2/l)*cos(y(1) - omega*t) - (g/l)*sin(y(1))];
t0 = 0;
T = 10;
y0 = [pi/4,pi/4];
h = 0.05;
[ts,yt] = VectorRK4(F,t0,T,y0,h);
[T,Y] = ode45(F,[0 10],[pi/4 pi/4]);
figure;
plot(T,Y(:,1),'-',T,Y(:,2),'-.');
I am trying to simulate my own RK4 algorithm. The error arises in using the ode45 command I am obtaining the following line of error:
Error using odearguments
@(T,Y)[Y(2),(R*OMEGA^2/L)*COS(Y(1)-OMEGA*T)-(G/L)*SIN(Y(1))] must return a column vector.
How can this be fixed?
댓글 수: 0
채택된 답변
Davide Masiello
2022년 10월 18일
편집: Davide Masiello
2022년 10월 18일
Just use a semicolon, ode45 requires the output to be a column array.
R = 0.5;
l = 1;
g = 9.81;
omega = 0.25;
F = @(t,y) [y(2); (R*omega^2/l)*cos(y(1) - omega*t) - (g/l)*sin(y(1))];
t0 = 0;
T = 10;
y0 = [pi/4,pi/4];
h = 0.05;
% [ts,yt] = VectorRK4(F,t0,T,y0,h);
[T,Y] = ode45(F,[0 10],[pi/4 pi/4]);
figure;
plot(T,Y(:,1),'-',T,Y(:,2),'-.')
추가 답변 (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!