clear all;
close all;
Delta_t=0.01;
t=0:Delta_t:2600.0;
rh=0.025;
y(1)=2.0;
for n=1:length(t)-1
y(n+1)=y(n)-Delta_t*(sqrt(2*9.81*y(n))*rh^2)/(2-0.5*y(n))^2;
end
plot(t,y,’k-‘,’linewidth’,5)
xlabel(‘t(s)’)
ylabel(‘y(m)’)
%
% Getting the spline coefficients
%
[a,b,c]=QuadraticSplineCoeffs(t,y);
%
%Specifying the upper and lower guess.
% Get the upper and lower guess from your plots
%
tupper=2300;
tlower=1500;
troot=(tupper+tlower)/2;
while abs(QuadraticSplineEval(a,b,c,t,troot)-0.1)>1.0e-3
ylower=QuadraticSplineEval(a,b,c,t,tlower)-0.1
yupper=QuadraticSplineEval(a,b,c,t,tupper)-0.1
yroot=QuadraticSplineEval(a,b,c,t,troot)-0.1
if yroot*ylower < 0
tupper= troot;
elseif yroot*yupper < 0
tlower= troot;
end
troot=(tupper+tlower)/2.0
end
function fi=QuadraticSplineEval(a,b,c,xdata,xi)
n=length(xdata)-1;
fi=zeros(1,length(xi));
for i=1:length(xi)
for j=1:n
if xdata(j)>xi(i)
j=j-1;
break
end
end
fi(i)=a(j)+b(j)*(xi(i)-xdata(j))+c(j)*(xi(i)-xdata(j))^2;
end
end
function [a,b,c]=QuadraticSplineCoeffs(xdata,fdata)
n=length(xdata)-1; %/* number of intervals */
a=zeros(1,n+1);
b=zeros(1,n+1);
c=zeros(1,n+1);
for i=1:n+1
a(i)=fdata(i);
end
c(1) = 0.0;
h(1) = xdata(2)-xdata(1);
for i=2:n
h(i) = xdata(i+1)-xdata(i);
c(i) = (1/h(i))*(a(i+1)/h(i)-a(i)*((1/h(i-1))+(1/h(i)))+a(i-1)/h(i-1)-c(i-1)*h(i-1));
end
for i=1:n
b(i)=(a(i+1)-a(i))/h(i)-c(i)*h(i);
end
end