【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函数使输出更易读
posted @ 2023-11-01 19:52  码鸽  阅读(4)  评论(0编辑  收藏  举报