Sie sind auf Seite 1von 3

Name of the experiment: Study and verification using matlab of Lagrange Interpolation to interpolate a value

which is not in the given data.


Objectives: Objectives of this experiment are:-
1. To study about Lagrange Interpolation.
2. To draw a flowchart for Lagrange Interpolation.
3. To create a MATLAB program to interpolate a polynomial using this method.
Theory: This method uses only function value. For a given set of distinct points xj and numbers yj, the Lagrange
polynomial is the polynomial of the least degree that at each point xj assumes the corresponding value yj (i.e. the
functions coincide at each point). The interpolating polynomial of the least degree is unique, however, and it is
therefore more appropriate to speak of "the Lagrange form" of that unique polynomial rather than "the Lagrange
interpolation polynomial", since the same polynomial can be arrived at through multiple methods.
Methodology:

(xx2)(xx3) (xx1)(xx3) (xx1)(xx2)


f(x)=f(x1) + f(x2) (x2x1)(x2x3) + f(x3) (x3x1)(x3x2)
(x1x2)(x1x3)



(x xi)
f(x) = f(xi)
(xi xj)
j!=i
j=1
=1
Algorithm:
1. Start
2. Read a,n;
3. for i=1 to n in steps of 1 do Read x , f(x)
end for
4. sum = 0
5. for i=1 to n in steps of 1 do
6. product = 1
7. for j=1 to n in steps of 1 do
8. if (j!=i) then
product=product *(a-xj)/(xi-xj)
end for
9. sum=sum+fi*product
end for
10. Print sum
11. stop.

Flowchart:
Problem Statement: Find the Lagrange interpolation polynomial to fit the following data at x=4

x 1.5 3 6

f(x) -0.25 2 20

Matlab Code for Lagrange Interpolation:

n=input('Number of tabulated value: ');


disp('Enter values of x & f(x) respectively.')
for i=1:n
fprintf('x(%d)=',i);
x(i)=input('');
fprintf('f(%d)=',i);
f(i)=input('');
end
s=0;
xd=input('Enter desired value of x: ');
for i=1:n
p=1;
for j=1:n
if(i==j)
continue;
else
p=p*((xd-x(j))/(x(i)-x(j)));
end
end
s=s+(f(i)*product);
end
disp('Result:')
disp(s)

Output:
Number of tabulated value: 3
Enter values of x & f(x) respectively.
x(1) = 1.5
f(1) = -.25
x(2) = 3
f(2) = 2
x(3) = 6
f(3) = 20
Enter desired value of x : 4
Result: 6
Result: The value of f(x) at x=4 is 6.
Conclusion: In this experiment we learned the MATLAB code of Lagrange interpolation method. Now we can
interpolate a data easily by using Lagrange interpolation formula.