Event function in ode45
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi!
Could somebody please tell me what am I doing wrong? I am solving a system of differential equations, and i would like it to stop, when y reaches zero. I want to write an event function. My code is exactly like it's written in the instructions.
function [value,isterminal,direction] = ProjectileEventsFcn(t,X)
value = X(3);
isterminal = 1;
direction = 0;
end
But when I run it, i get the error:
Error using ProjectileEventsFcn (line 2)
Not enough input arguments.
And the parameter t in (t,X) is shadowed with a brown colour.
My system is
F=@(t,X,par) [X(2); -1/2*prod(par(1:3))/par(4)*(((X(2)+par(5))^2)+(X(4))^2)^(1/2)*X(2);
X(4); -9.8-1/2*prod(par(1:3))/par(4)*(((X(2)+par(5))^2)+(X(4))^2)^(1/2)*X(4)].
Thank you for your help!
1 commentaire
Jan
le 27 Sep 2017
Please post the complete error message and the line, which causes it. Where is "t shadowed with a brown color"?
Réponses (2)
Josh Meyer
le 27 Sep 2017
Modifié(e) : Josh Meyer
le 27 Sep 2017
My guess is you are using the "old" way to pass parameters to the ODE function F, where they are specified as trailing input arguments:
opts = odeset('Events',@ProjectileEventsFcn)
[t,y,te,ye,ie] = ode45(F,tspan,y0,opts,par);
There are two ways to resolve this:
- When you use trailing arguments to specify parameters, the events function needs to accept the same number of input arguments as the ODE function, even if they are not used. So if you change the functional signature of the event function to accept three inputs the issue is fixed:
function [value,isterminal,direction] = ProjectileEventsFcn(t,X,p)
- The "modern" way to pass parameters is with an anonymous function in the call to the solver. You can leave your events function as-is (accepts 2 inputs) and just change your call to the solver to:
[t,y,te,ye,ie] = ode45(@(t,y) F(t,y,par),tspan,y0,opts);
1 commentaire
Walter Roberson
le 27 Sep 2017
Ah, that's obscure! Passing extra arguments that way has not been documented in quite a few years, so I never knew the extra arguments would be passed to the event function as well.
Jan
le 27 Sep 2017
Perhaps it is a typo only when defining the event function:
opts = odeset('Events', ProjectileEventsFcn); % Fails
opts = odeset('Events', @ProjectileEventsFcn); % Works
You function to be integrated looks ugly. This is not an error, but hard to debug:
F=@(t,X,par) [X(2); -1/2*prod(par(1:3))/par(4)*(((X(2)+par(5))^2)+(X(4))^2)^(1/2)*X(2); ...
X(4); -9.8-1/2*prod(par(1:3))/par(4)*(((X(2)+par(5))^2)+(X(4))^2)^(1/2)*X(4)].
I'd prefer a function instead of an anonymous function:
function dx = F(t, X, par)
C = -0.5 * prod(par(1:3)) / par(4) * sqrt((X(2) + par(5))^2 + X(4)^2) * X(2);
dx = [X(2); ...
C; ...
X(4); ...
-9.8 - C * X(4)];
Then call the integrator by providing the parameter through an anonymous function:
[t, X] = ode45(@(t, x) F(t, X, par), ...
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!