Академический Документы
Профессиональный Документы
Культура Документы
0
3482
Share on Facebook
Tweet on Twitter
Named after Joseph Louis Lagrange, Lagrange Interpolation is a popular technique of numerical
analysis for interpolation of polynomials. In a set of distinct point and numbers xj and yj
respectively, this method is the polynomial of the least degree at each x j by assuming
corresponding value at yj. Lagrange Polynomial Interpolation is useful in Newton-Cotes Method
of numerical integration and in Shamirs secret sharing scheme in Cryptography.
In this tutorial, were going to write a program for Lagrange Interpolation in MATLAB, and go
through its mathematical derivation along with a numerical example. You can also check out our
earlier tutorial where we discussed a C program for this interpolation technique.
Also, when i j, m j does not produce it and one term in the product will be for m = I, that is, [x i
xi]/[xj xi] = 0
So, for all i j, lj(x) includes the term ( x xi ) in the numerator, therefore the entire product will be
found to be zero at x = xj
This is the required formula which will also be used in the program code for Lagrange Interpolation
in MATLAB.
NOTE:
The Lagrange Interpolation formula can also be derived from Newtons divided difference formula.
When a polynomial function f(x) is be approximated with an n th degree polynomial, nth divided
difference of f(x) is constant and the (n+1)th divided difference is zero.
Mathematically, f[x0, x1, x2, x3, . . . . . .. . xn] = 0
%grid
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
%grid
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.
The row vectors X and Y define a set of n points which are used in Lagrange method for the
determination of (n-1)th order polynomial in X which passes through these points. The function of
P in the program is to return the n coefficients which define the polynomial in the same order as
used by POLY and POLYVAL.
Similarly, R and S are defined to return x-coordinates and y-values at n-1 extreme of the resulting
polynomial. YY returns the value of the polynomial sampled at the points which are specified in
XX.
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:
clc;
clear all;
close all;
y=[12 13 14 16]; %Change here for different function
x=[5 6 9 11];
a=15;
m=ans1+ans2+ans3+ans4;
y
x
fprintf('the value of x corresponding to y=15 is %f',m);
Lagrange Interpolation
(curvilinear interpolation)
The computations in this small article show the Lagrange interpolation. The code computes y-
coordinates of points on a curve given their x-coordinates.
You must enter coordinates of known points on the curve, no two having the same abscissa.
Example 2
Given the following points from a sine curve, what are the y-values for x = -2,47 and x = 1.5?
x = [-5 -4 -3 -2 -1 0 1 2 3 4 5];
y = [.958 .757 -.141 -.909 -.841 0 .841 .909 .141 -.757 -.959];
x3 = -2.47;
y3 = lagrange_interp(x,y,x3)
x4 = 1.5;
y4 = lagrange_interp(x,y,x4)
The results are:
y3 = -0.6218
y4 = 0.9972