ODE45, simple events

조회 수: 43 (최근 30일)
Stevon Kline
Stevon Kline 2019년 10월 29일
댓글: Stevon Kline 2019년 10월 29일
I'm trying to understand 'Events" and how to trigger them. But I'm not quite understanding how it works because my graph still runs past y = 0, and when I output the [t,y,te,ye,ie], I get empty sets. When I change check to = (y>=0), I get some negative number for ye.
h = 1000;
g = 9.8;
k = 1/10;
y0 = h;
t0 = 0; tfinal = 30;
tspan = [t0,tfinal];
options = odeset('Events',@landing);
v = @(t,y) -g*t;
ode45(v,tspan,y0,options);
function [check,stop,direction]=landing(t,y)
check = (y~=0);
stop = 1;
direction=0;
end
If somebody help me with what I'm missing. Thanks. I'd like the the integration to stop when it hits the ground. (ignore unused variables, I actually have to add drag force to this equation with k but I'm trying to learn the basics first).

채택된 답변

Steven Lord
Steven Lord 2019년 10월 29일
check = (y~=0);
You don't want to check only for y actually touching the ground. If the ODE solver were to evaluate your function at time t = 1 (when your projectile is above ground level) and t = 2 (when your projectile is under ground level) but not t = 1.6 (when it exactly reached the ground) this events function would not detect the landing.
The ballode example has an events function you can use for this purpose. In fact, that example seems pretty similar to the problem you're trying to solve. You can open that example in the Editor and take a look at its events function.
You don't want your events function to return a logical value, you want it to return the height above ground and let MATLAB stop the solver when that height crosses zero (landing.)
  댓글 수: 1
Stevon Kline
Stevon Kline 2019년 10월 29일
Thanks, so I'm stilling getting a negative value. So that just means I need to increase the step value of of tspan to get the exact point t, when it crosses zero. Because its close, but still getting a negative value, a very small one.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alex Mcaulley
Alex Mcaulley 2019년 10월 29일
function [check,stop,direction]=landing(t,y)
check = y;
stop = 1;
direction=0;
end
  댓글 수: 1
Stevon Kline
Stevon Kline 2019년 10월 29일
Thank you.

댓글을 달려면 로그인하십시오.

카테고리

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