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!