Why Index exceeds the number of array elements (1) if I change the input order in the anonymous function?

1 vue (au cours des 30 derniers jours)
Here is the funciton:
function [Y] = d_o(X,t,m,k,c)
x = X(1);
y = X(2);
Y(1,1)=y;
Y(2,1)=1/m*(-k*x-c*y);
end
Here is the script:
T = 0:0.1:100;
y0 = [0.1,1];
m = 1; k = 0.1; c = 0.05;
d_o_an = @(X,t) d_o(X,t,m,k,c);
[tout,yout] = ode45(d_o_an,T,y0);
plot(tout, yout);
I think the problem is in the anonymous function, if I change @(X,t) to @(t,X), the code can run. But why?
  3 commentaires
Jiaying Lu
Jiaying Lu le 24 Nov 2020
Sorry, I don't get you. Could you explain more related to my code?
VBBV
VBBV le 24 Nov 2020
Modifié(e) : VBBV le 24 Nov 2020
%if
%d_o_an = d_o(X,t,m,k,c);
[tout,yout] = ode45(@d_o,T,y0);
plot(tout, yout);
Use @ to invoke the function d_o directly

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 24 Nov 2020
ode45 calls the function to be integrated as f(t, Y) . So the time must be the first input.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by