MATLAB: Second order differential equation using ode45 and an events option: (195/32) * D2y + 0.005*(Dy)^2 = 0

events optionode45second order

This is the equation I set up from a free fall with air resistance question. The differential equations with MatLab text suggests I use the events option to ode45 to detect significant events. (y(5 seconds))
mfile:
function [event, stop, dir] = eventfunct2(~, y)
event = y(5)- 0;
stop = 1;
dir = 0;
Code:
f = @(t, y) [y(2); -0.005.*y(2).^2];
[ta, ya] = ode45(f, [0 10], [0 1000]);
figure; plot(ta, ya(:, 1))
% Express the second order as a firt order and use ode45 to graph and
% approximate the value of y(5) ~ 650
type eventfunct2
options = odeset('Events', @eventfunct2);
[t, y, tev, yev] = ode45(f, [0 5], [0 1000], options);
[tev, yev];
errors:
Attempted to access y(5); index out of bounds because numel(y)=2.
Error in eventfunct2 (line 2) event = y(5)- 0;
Error in odeevents (line 28) eventValue = feval(eventFcn,t0,y0,eventArgs{:});
Error in ode45 (line 147) [haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = …
Error in ProbSetD (line 11) [t, y, tev, yev] = ode45(f, [0 5], [0 1000], options);
PLEASE HELP!!! I don't think I am using events right!!

Best Answer

y at 5 seconds is not y(5). You need to examine the variable t to determine time, not y. So pass in t and base your event on t.