|
I'm writing a code for polynomial interpolation by Lagrange method. But I can't execute my program. I don't know where I went wrong. Any input would be much appreciated.
% Setting up the Lagrangian polynomial
z=sym('z');
d = input('enter f(x)= \n')
f=inline(d);
x0=input('inter x0 = \n')
x=x0
L0=(z-xdata(2))/(xdata(1)-xdata(2));
L1=(z-xdata(1))/(xdata(2)-xdata(1));
fl=L0*ydata(1)+L1*ydata(2);
fxdesired=subs(fl,z,xdesired);
fprev=fxdesired;
datapoints=3;
p=1;
for i=1:n
if d(i) <= datapoints
xdata(p)=x(i);
ydata(p)=y(i);
p=p+1;
end
end
% Calculating coefficients of Newton's Divided difference polynomial
% Setting up the Lagrangian polynomial
z=sym('z');
L0=((z-xdata(2))*(z-xdata(3)))/((xdata(1)-xdata(2))*(xdata(1)-xdata(3)));
L1=((z-xdata(1))*(z-xdata(3)))/((xdata(2)-xdata(1))*(xdata(2)-xdata(3)));
L2=((z-xdata(1))*(z-xdata(2)))/((xdata(3)-xdata(1))*(xdata(3)-xdata(2)));
fq=L0*ydata(1)+L1*ydata(2)+L2*ydata(3);
fxdesired=subs(fq,z,xdesired);
fnew=fxdesired;
ea=abs((fnew-fprev)/fnew*100);
if ea >= 5
sd=0;
else
sd=floor(2-log10(abs(ea)/0.5));
end
|