Matlab - Lagranges Interpolation Method
Matlab - Lagranges Interpolation Method
Theory
The Lagrange interpolation formula is a way to find a polynomial which takes on certain values at arbitrary points. Specifically, it gives a
constructive proof of the theorem below
Given n distinct real values x1, x2, ,…,xn and n real values y1, y2, …,yn (not necessarily distinct), there is a unique polynomial P with real
coefficients satisfying P(xi)=yi for i∈{1,2,…,n}, such that.deg(P)<n.
The above Matlab code for Lagrange method is written for interpolation of polynomials fitting a set of points. The program uses a user-defined
function named LAGRANGE(X, Y) with two input parameters which are required to be row vectors.
All the inputs which are required to give to the program are embedded in the source code. The values of X and Y initially set in the program are:
X = [1 2 3 4 5 6 7 8] and Y = [0 1 0 1 0 1 0 1]
Typical Algorithm/Flowchart:
Algorithm:
Step 1. Read x,n
Step2. for i=1 to (n+1) is steps of 1 do Read xi,fi end for {the above statements reads x,s and the corresponding values of f is }
Step 3. Sum=0
Step 4. for i=1 to (n+1) in steps of 1 do
Step 5. Profvnc=1
Step 6. for J=1 to (n+1) in steps of 1 do
Step 7. If (j≠i) then prodfunc=prodfunc X(x-xj) / (xi-xj) endfor
Step 8. Sum=Sum+fi x Prodfunc {sum is the value of f at x} end for
Step 9. Write x, sum
Step 10. STOP
Example:
Lagrange’s Interpolation Method