Lagrange Interpolation Polynomial Code for coefficients

3.1k Views Asked by At

Can someone help me with developing a simple MATLAB code that inputs the n datasets $ \lbrace x_i : i=0,...,n \rbrace$ and $ \lbrace f^i : i=0,...,n \rbrace$ and produces the coefficients $ \lbrace c_i : i=0,...,n \rbrace$ of the Lagrange Polynomials?

I have an understanding of how to achieve the polynomial itself, but extracting each coefficients I am new to. Any help would be great :)

Thanks :)

1

There are 1 best solutions below

0
On
function a = coefPolinomLagrange (x, y)
n = length(x);
M = [];
for i=1:n
    M = [M; x.^(i-1)'];
end

denom = M;
det_denom = det(denom);

Mcirc = [M;y';M(1:end-1,:)];

for i=1:n
    numer = Mcirc(i+1:i+n,:);
    a(i) = (-1)^(n-i) * det(numer) / det_denom;
end
a = fliplr(a);
end