How to pass a function handle as an argument in ode45?

2 ビュー (過去 30 日間)
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2022 年 10 月 18 日
コメント済み: Tarek Hajj Shehadi 2022 年 10 月 18 日
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?

採用された回答

Davide Masiello
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),'-.')
  1 件のコメント
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2022 年 10 月 18 日
Semi-colons.... are the mother and father of all evil.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by