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

2 vues (au cours des 30 derniers jours)
Tarek Hajj Shehadi
Tarek Hajj Shehadi le 18 Oct 2022
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?

Réponse acceptée

Davide Masiello
Davide Masiello le 18 Oct 2022
Modifié(e) : Davide Masiello le 18 Oct 2022
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 commentaire
Tarek Hajj Shehadi
Tarek Hajj Shehadi le 18 Oct 2022
Semi-colons.... are the mother and father of all evil.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by