Scilab Program for Lagrange Polynomial Interpolation

Problem:

Write a scilab program for lagrange polynomial interpolation.

Solution:

Lagrange interpolation formula is
f(x)=
(x-x1)(x-x2).....(x-xn) (x0-x1)(x0-x2).....(x0-xn)
f(x0) +
(x-x0)(x-x2).....(x-xn) (x1-x0)(x1-x2).....(x1-xn)
f(x1) + ..... +
(x-x0)(x-x1).....(x-xn-1) (xn-x0)(xn-x1).....(xn-xn-1)
f(xn)
to implement scilab program for lagrange interpolation.

Scilab Program / Source Code:

The following is the source code of scilab program for polynomial interpolation by numerical method known as lagrange interpolation.
/* Aim: Scilab program for lagrange polynomial interpolation */

X=[1 2 5 10];  //set the arguments
Y=[10 75 100 12];  //set the corresponding values of f(x)

n=length(X);  // store the length of total arguments

x=2.5;  // set value of x for interpolation

L=0;  // to store the result

for i=1:n
    N=1;D=1;
    for j=1:n  // Loop to calculate numerator and denominator
        if(i==j)  // skip the calculation of numerator and denominator when i==j
            continue;
        else
            N=N*(x-X(j));  // calculate numerator
            D=D*(X(i)-X(j));  //calculate denominator
        end
    end
    L=L+(N/D)*Y(i);  // calculate according to formula and add to result L.
end

disp(L) // display the result