Event function in ode45

조회 수: 25 (최근 30일)
Janja Tusar
Janja Tusar 2017년 9월 27일
댓글: Walter Roberson 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