【matlab笔记】杂乱版
求Lagrange插值多项式
syms x;
X = [1, 3/2, 0, 2]
Y = [3, 13/4, 3, 5/3]
n = length(X);
L = sym('1');
P = sym('0');
for i = 1:n
% 求出 Li(x)
Li = sym('1');
for j = 1:n
if j~=i
Li = Li*(x-X(j))/(X(i)-X(j));
end
end
% 更新插值多项式 P(x)
P = P + Y(i)*Li;
end
disp('拉格朗日插值多项式为:');
pretty(simplify(P)); % pretty函数使输出更易读