Remember a math homework
learn to use the Lagrange polynomial
n=15; %5,10,15插值点数
syms t;
x = linspace(-5,5,n);
y=f(x);
for i=1:n
L(i)=Lagrange(x,n,i,t);
end
LN=sum(y.*L);
t=-5:0.01:5;
y0=5./(1+t.^2);
plot(t,y0);
hold on;
ezplot(LN,t);
a=strcat('实验一:',num2str(n),'次插值');
title(a);
legend('原函数','插值函数');
function fi=Lagrange(x,n,i,t)
fu=1;
fd=1;
for j=1:n %循环计算
if i~=j
fu=fu.*(t-x(j));
fd=fd.*(x(i)-x(j));
end
end
fi=fu/fd;
end
function y=f(x)
y=5./(1+x.^2);
end