MATLAB: Help with the 2nd Order ODE Solver.

differential equationsode45

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);
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
function [value, isterminal, direction] = ground(t,y)
gd = 138;
value = y(2)-gd;
isterminal = 1;
direction = -1;

Best Answer

  • 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;
    That worked as I suspect it should, stopping the integration at about 13.3 seconds simulation time.