Lagrange

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Experiment No:

Name of the Experiment: Study of Lagrange Interpolation Method to


Obtain the Desire Value of the Function.

Objectives:
In this experiment we use Lagrange Interpolation method to find the value of
dependent variable y at any point x by using the given data table.

Theory:
The Lagrange interpolation formula is a way to find a polynomial, which
takes on certain values at arbitrary points. Lagrange interpolation
formula gives us the unique polynomial of degree less or equal to n, which
takes specified values at any (n+1) given points. ... Of course for
inverse interpolation we have to ensure that the function is injective (i.e.
one-one) near the value of x being sought.
This is again an  Nth degree polynomial approximation formula to the
function f(x), which is known at discrete points xi, i = 0, 1, 2 . . . Nth. If f(x) is
approximated with an Nth degree polynomial then the Nth divided difference
of f(x) constant and (N+1)th  divided difference is zero. That is
f [x0, x1, . . . xn, x] = 0

From the second property of divided difference we can write,


f0 fn fx
 +  + . . . +    =  0
(x0 - x1) . . . (x0 - xn)(x0 - x) (xn - x0) . . . (xn - xn-1)(xn - x)   (x - x0) . . . (x - xn)

or

  (x - x1) . . . (x - xn)   (x - x0) .  .  . (x - xn-1)  


f(x)  f0  + .   .   .
 fn
=  +
  (x0 - x1) . . . (x0 - xn)   (xn - x0) .  .  . (xn - xn-1)  
 The generalized formula of Lagrange interpolation is:

Since Lagrange's interpolation is also an Nth degree polynomial


approximation to f(x) and the N  degree
th
polynomial passing
through (N+1) points is unique hence the Lagrange's and Newton's divided
difference approximations are one and the same. However, Lagrange's
formula is more convinent to use in computer programming and Newton's
divided difference formula is more suited for hand calculations .

Tools:
i) Windows 10 (Operating System)
ii) Matlab 2015a

Methodology:

Algorithm:
1. Start
2. Read number of data (n)
3. Read data Xi and Yi for i=1 ton n
4. Read value of independent variables say xp
Whose corresponding value of dependent say yp is to be
determined.
5. Initialize: yp = 0

6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i

7. Display value of yp as interpolated value.


8. Stop
Flow Chart:
Code:
x1=input('Enter value of X :\n');
n=input('Enter value of N :\n');

for i=1:n+1
x(i)=input('Enter the value of xi :\n');
f(i)=input('Enter the value of fi :\n');

end

sum=0;

for i=1:n+1
prodfunc=1;
for j=1:n+1
if j~=i
prodfunc=((prodfunc)*(x1-x(j)))/(x(i)-x(j));
end
end
sum=sum+f(i)*prodfunc;
end

fprintf(' value of f(%f) : %f \n',x1,sum);

Output:

Enter value of X :
3
Enter value of N :
4
Enter the value of xi :
1.5
Enter the value of fi :
-.25
Enter the value of xi :
3
Enter the value of fi :
2
Enter the value of xi :
6
Enter the value of fi :
20
Enter the value of xi :
9
Enter the value of fi :
4
Enter the value of xi :
5
Enter the value of fi :
8
value of f(3.000000) : 2.000000

Precautions:
1. As we used crack version of matlab, data connections were turned off
during coding.

2. No syntax or logical error were found while checking .

Discussion:

In this experiment we able to solve the function by using Lagrange


Interpolation method and we get the value is 2.00000

Reference:
1. https://www.codewithc.com/lagrange-interpolation-in-matlab
2. Steven C. Chapra , Raymond P. Canal, “Numerical Method for Engineers”.

You might also like