Event function in ode45

8 次查看(过去 30 天)
Janja Tusar
Janja Tusar 2017-9-27
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 个评论
Jan
Jan 2017-9-27
Please post the complete error message and the line, which causes it. Where is "t shadowed with a brown color"?

请先登录,再进行评论。

回答(2 个)

Josh Meyer
Josh Meyer 2017-9-27
编辑:Josh Meyer 2017-9-27
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 个评论
Walter Roberson
Walter Roberson 2017-9-27
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
Jan 2017-9-27
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), ...

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by