You are on page 1of 3

function p=interpLagrange(X,Y,Q)

n=length(X);
p=zeros(1,n);
r=zeros(1,n);
for k=1:n
L=1;
for i=1:n
if i~=k
L=conv(L,[1 -X(i)])/(X(k)-X(i));
end
end
p=p+Y(k)*L;

end
%x=linspace(min(X),max(X),100);
%y=polyval(p,x);
%plot(x,y)
%hold on
%plot(X,Y,'*')

T=zeros(1,Y(1));
K=zeros(1,Y(1));
S=zeros(1,Y(1));
for y=1:1:Y(1)

for l=1:n
if l==n
r(1,l)=-y ;
else
r(1,l)=0;
end

end

A=r+p;
q=roots(A);
m=length(A);

for z=1:m-1
if X(1)<=q(z,1)&& q(z,1)<0&&imag(q(z,1))==0
a=q(z,1);

elseif 0<q(z,1)<=X(n)&& imag(q(z,1))==0


b=q(z,1);

end

end
c=100;
dx=(b-a)/c;
s=0;
for i=1:n
for j=1:c
x=(a+i*dx);
x1=a;
x2=b;
s=0;
f=A(1,i)*x.^(n-i);
e=A(1,i)*x1.^(n-i);
v=A(1,i)*x2.^(n-i);
f=f+s;
end
s=s+f;
end
Area=(e+2*s+v)*(b-a)/(2*c);

E=y+Q.^2/((Area.^2)*2*9.81);
T(1,y)=E;
K(1,y)=y;

end

for k=1:Y(1)
L=1;
for i=1:Y(1)
if i~=k
L=conv(L,[1 -K(i)])/(K(k)-T(i));
end

end
S=S+T(k)*L;

end
lh=linspace(min(K),max(K),100);
y=polyval(S,lh);
plot(lh,y)

plot(T,K,'*')
lo=length(T);
for o=1:lo
if T(1,o)==min(T)
Yc=K(1,o);
break
end

end
Yc
end

You might also like