MATLAB: Please Check the code commented with a star. there i want to find the time when h is 0. I am new to MATLAB and can not find the error.

codedifferential equationsfunctionMATLAB

Chicken Ballistics
Instructions are in the task pane to the left. Complete and submit each task one at a time.
Task 2
Solve the system of ODEs.
grid on
hold on
tRange=[0 4.5];
Y0=[0;20];
[tSol,YSol]=ode45(@chicken,tRange,Y0);
Task 3
Extract and plot the height.
h=YSol(:,1);
plot(tSol,h);
fzero(@(t) h(find(abs(tSol-t)<0.0001)),[4 4.25])%**please check this line**
%plot(tSol,h);
function dYdt = chicken(t, Y)
% TODO - Extract the height h from input vector Y
h=Y(1);
% TODO - Extract the velocity u from input vector Y
u=Y(2);
% TODO - Complete expression for dhdt
dhdt=u;
% TODO - Complete expression for dudt
dudt=-9.8;
% TODO - Define output column vector dYdt
dYdt=[dhdt;dudt];
end

Best Answer

There are several ways to do this, the most reliable being event functions. Since you want to find one value one time, try this:
tRange=[0 4.5];
Y0=[0;20];
[tSol,YSol]=ode45(@chicken,tRange,Y0);
h=YSol(:,1);
plot(tSol,h);
idx = find(h < 0, 1);
hIs0 = interp1(h(idx-1:idx+1), tSol(idx-1:idx+1), 0)
producing:
hIs0 =
4.08086009640834