0% found this document useful (0 votes)
214 views3 pages

Matlab - Lagranges Interpolation Method

Lagrange interpolation is a method to find a polynomial that passes through a given set of points. The Lagrange interpolation formula constructs a polynomial P(x) that takes on the values yi at points xi. The algorithm reads the x-values and corresponding y-values, then calculates the Lagrange basis polynomials to determine the coefficients of P(x), which is a polynomial of degree n-1 that interpolates the points. The MATLAB code provided implements this method by calculating the polynomial weights using basis polynomials with roots at each other x-value, then evaluating P(x) for given x-values.

Uploaded by

arvind yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views3 pages

Matlab - Lagranges Interpolation Method

Lagrange interpolation is a method to find a polynomial that passes through a given set of points. The Lagrange interpolation formula constructs a polynomial P(x) that takes on the values yi at points xi. The algorithm reads the x-values and corresponding y-values, then calculates the Lagrange basis polynomials to determine the coefficients of P(x), which is a polynomial of degree n-1 that interpolates the points. The MATLAB code provided implements this method by calculating the polynomial weights using basis polynomials with roots at each other x-value, then evaluating P(x) for given x-values.

Uploaded by

arvind yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lagrange’s 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.

MATLAB Code (in General)

% Lagrange Interpolation MATLAB Program


function [P,R,S] = lagrangepoly(X,Y,XX)
X = [1 2 3 4 5 6 7 8]; % inputting the values of given x
Y = [0 1 0 1 0 1 0 1]; % inputting the values of given y
%[P,R,S] = lagrangepoly(X,Y);
xx = 0.5 : 0.01 : 8.5;
%plot(xx,polyval(P,xx),X,Y,'or',R,S,'.b',xx,spline(X,Y,xx),'--g')
%grid
%axis([0.5 8.5 -5 5])
if size(X,1) > 1; X = X'; end % checking for parameters
if size(Y,1) > 1; Y = Y'; end
if size(X,1) > 1 || size(Y,1) > 1 || size(X,2) ~= size(Y,2)
error('both inputs must be equal-length vectors') % displaying error
end % end of scope of if
N = length(X);
pvals = zeros(N,N);
% for evaluating the polynomial weights for each order
for i = 1:N
% the polynomial with roots may be values of X other than this one
pp = poly(X( (1:N) ~= i));
pvals(i,:) = pp ./ polyval(pp, X(i));
end
P = Y*pvals;
if nargin==3
YY = polyval(P,XX); % output is YY with given XX
P = YY; % assigning to output
end
% end of scope of if
if nargout > 1 % checking for conndtions
R = roots( ((N-1):-1:1) .* P(1:(N-1)) );
if nargout > 2
% the evalustion of acual value at the poins of zero derivative
S = polyval(P,R);
end
end

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]

A sample output of this MATLAB program is given below:


Lagrange’s Interpolation Method

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

You might also like