Help with my 2nd Order ODE Solver.

1 次查看(过去 30 天)
A penny dropped from the top of a building (138meters). Using ode45 compute the motion of the penny’s fall. Which I have done below. I just need the function to stop when the penny hits the ground. I can guess the penny hits roughly at 13.25 seconds from the graph and math. In my second funtion I am trying to make it where my function stops when the pennny hits the goround(y=0). Plz help any suggestions will be helpful.
yo = 138;
yf = 0;
a = 9.8;
to = 0;
tf = 20; making the final time longer than need be becuse I want to make my event fcn to work.
time = [to tf];
iv = [138 0];
Options = odeset('RelTol',100*eps,'Events',@ground);
[t,y,te,ue,ie] = ode45(@f,time,iv,Options);
%%%FUNTIONS%%%
function rk=f(t,y)
ag = 9.8;
vt = 11;
rk = [y(2); (-ag)*(1-(y(2)/vt)^2)] % this is my system of equations
return
end
function [value, isterminal, direction] = ground(t,y)
gd = 138;
value = y(2)-gd;
isterminal = 1;
direction = -1;
end

采纳的回答

Star Strider
Star Strider 2020-5-15
Trigger the event on ‘y(1)’, and set ‘gd’ to zero:
function [value, isterminal, direction] = ground(t,y)
gd = 0;
value = y(1)-gd;
isterminal = 1;
direction = -1;
end
That worked as I suspect it should, stopping the integration at about 13.3 seconds simulation time.

更多回答(0 个)

类别

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