MATLAB event function help

35 ビュー (過去 30 日間)
Peter Delaney
Peter Delaney 2018 年 8 月 29 日
回答済み: Peter Delaney 2018 年 8 月 29 日
Hi, new to matlab and doing the academy courses to help, running through the ODE course and coming to an example where they suggest adding an event function to stop the calculation when a projectile hits the ground.
Its a series of 2 odes and its a simple code:
tRange = [0 3];
Y0 = [0 20];
[tSol,YSol] = ode45(@myode,tRange,Y0)
h = YSol(:,1)
plot(tSol,h)
function dYdt = myode(t,Y)
h = Y(1);
u=Y(2);
dhdt = u;
dudt = -9.8;
dYdt = [dhdt;dudt];
end
from the event function page I think you are supposed to set it up as:
options = odeset('Events',@event_function)
[tSol,YSol,te,ye,ie] =(@myode,tRange,Y0,options)
function [value,isterminal,direction] = event_function(t,h)
value = (Y(1) ~= 0);
isterminal = 1;
direction = 0;
but get a load of errors, what am I doing wrong?

採用された回答

Torsten
Torsten 2018 年 8 月 29 日
function main
tRange = [0 10];
Y0 = [0 20];
options = odeset('Events',@event_function)
[tSol,YSol,te,ye,ie] =ode45(@myode,tRange,Y0,options)
h = YSol(:,1);
plot(tSol,h)
end
function [value,isterminal,direction] = event_function(t,h)
value = h(1);
isterminal = 1;
direction = 0;
end
function dYdt = myode(t,Y)
h = Y(1);
u=Y(2);
dhdt = u;
dudt = -9.8;
dYdt = [dhdt;dudt];
end

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 8 月 29 日
tRange = [0 3];
Y0 = [0 20];
options = odeset('Events',@event_function);
[tSol,YSol,te,ye,ie] = ode45(@myode,tRange,Y0,options);
h = YSol(:,1);
plot(tSol,h)
function dYdt = myode(t,Y)
h = Y(1);
u=Y(2);
dhdt = u;
dudt = -9.8;
dYdt = [dhdt;dudt];
end
function [value,isterminal,direction] = event_function(t,Y)
value = (Y(1) ~= 0);
isterminal = 1;
direction = 0;
end
  1 件のコメント
Peter Delaney
Peter Delaney 2018 年 8 月 29 日
Hi, Thanks for your help, when I run the code I still get this error messaged:
Undefined function 'sign' for input arguments of type 'logical'.
Error in odezero (line 46) indzc = find((sign(vL) ~= sign(vR)) & (direction .* (vR - vL) >= 0));
Error in ode45 (line 353) odezero(@ntrp45,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNegative);
Error in reaction (line 4) [tSol,YSol,te,ye,ie] = ode45(@myode,tRange,Y0,options);

サインインしてコメントする。


Peter Delaney
Peter Delaney 2018 年 8 月 29 日
Perfect, thank you!!

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by