Langranges Formula

//Lagrange's Formula
//--- prod stands for product

x=input("Enter value of x as Row Matrix: ")
y=input("Enter value of y as Row Matrix: ")
xg=input("Enter value of xg to interpolate: ")
n=length(x)
add=0
for i=1:n
    prod=1
for j=1:n
    if(j~=i) then
        prod=prod*(xg-x(j))/(x(i)-x(j))
end
end
add=add+prod*y(i)
end
disp(add,"Interpolated answer:")

Output
-->exec('D:\Scilab prog by me\Lagranges.sce', -1)
Enter value of x as Row Matrix: [1 3 4 8 10]
Enter value of y as Row Matrix: [2 10 17 65 101]
Enter value of xg to interpolate: 2

 Interpolated answer:  

    5.

-->exec('D:\Scilab prog by me\Lagranges.sce', -1)
Enter value of x as Row Matrix: [1 3 4 8 10]
Enter value of y as Row Matrix: [2 10 17 65 101]
Enter value of xg to interpolate: 15

 Interpolated answer:  

    226.  

No comments:

Post a Comment