How to pass a function handle as an argument in ode45?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tarek Hajj Shehadi
il 18 Ott 2022
Commentato: Tarek Hajj Shehadi
il 18 Ott 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?
0 Commenti
Risposta accettata
Davide Masiello
il 18 Ott 2022
Modificato: Davide Masiello
il 18 Ott 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),'-.')
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!