Вы находитесь на странице: 1из 8

Lagrange Interpolation in MATLAB

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.

Derivation of Lagrange Interpolation:


Consider a given set of k+1 points, (x 0, y0) , (x1, y1), ( x2, y2).. (xk, yk) where each points are
distinct.
Lets assume a function L(xj) such that L(xj) = yj , j = 0, 1 , 3 , 3 . .. k

Observing the following points

Lj(x) contains k factors in product and each factor has x

Now, consider what happens when this product is expanded.


Since the product skips m = j, when i = j then all terms are [xj xm ] / [xj xm] =1

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

Zeroing the entire product,

where, ij is Kroneckers delta.

So, it can be written that:


Therefore, the considered function L(x) is a polynomial with degree at most k and where L(xj) = yj

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

By using the second property of divided difference, it can be written that

Simplifying this equation, we get

This can be represented as:

Lagrange Interpolation in MATLAB Code:


% 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

1 % Lagrange Interpolation MATLAB Program


2
3
4 function [P,R,S] = lagrangepoly(X,Y,XX)
5 X = [1 2 3 4 5 6 7 8]; % inputting the values
6 of given x
7 Y = [0 1 0 1 0 1 0 1]; % inputting the values
8
9
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,spli
ne(X,Y,xx),'--g')

%grid

%axis([0.5 8.5 -5 5])


10
11 if size(X,1) > 1; X = X'; end % checking for
12 parameters
13 if size(Y,1) > 1; Y = Y'; end
14 if size(X,1) > 1 || size(Y,1) > 1 || size(X,2) ~=
15
16 size(Y,2)
17 error('both inputs must be equal-length
18 vectors') % displaying error
19 end % end of scope of if
20
21
N = length(X);
22 pvals = zeros(N,N);
23 % for evaluating the polynomial weights for
24 each order
25
26
27 for i = 1:N
28 % the polynomial with roots may be values
29 of X other than this one
30 pp = poly(X( (1:N) ~= i));
31
32
pvals(i,:) = pp ./ polyval(pp, X(i));
33 end
34 P = Y*pvals;
35 if nargin==3
36
37
YY = polyval(P,XX); % output is YY with
38 given XX
39 P = YY; % assigning to output
40 end
41 % end of scope of if
42

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.
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:

Numerical Example in Lagrange Interpolation:


Now, lets analyze Lagrange Interpolation and its Matlab code mathematically using a different set
of parameters. The question here is:
From the following sets of data, find the value of x corresponding to y=15 by using Lagrange
Interpolation.
(5,12), (6,13), (9,14), 11,16)
Solution
Given value of x and y are:
X: 5 6 9 11
Y: 12 13 14 16
By using the Lagrange Interpolation Formula:
x(y)=(y-13)(y-14)(y-16)*5/(12-13)(12-14)(12-16) + (y-12)(y-14)(y-16)*6/(13-12)(13-14)(13-16) +
(y-12)(y-13)(y-16)*9/(14-12)(14-13)(14-16) + (y-12)(y-13)(y-14)*11/(16-12)(16-13)(16-14)
By substituting y= 15, we get x = 11.5; which is the required answer.

MATLAB Code for Lagrange Interpolation Formula


%Created by myclassbook.org
%Created on 26 May 2013
%lagrange interpolation formula

% Question: Given set of values of x and y (5,12),(6,13),(9,14),(11,16)


% Find the value of x corresponding to y=15 using lagrange interpolation

clc;
clear all;
close all;
y=[12 13 14 16]; %Change here for different function
x=[5 6 9 11];
a=15;

%Applying Lagrange's Interpolation:


ans1=((a-y(2))*(a-y(3))*(a-y(4)))*x(1)/((y(1)-y(2))*(y(1)-y(3))*(y(1)-y(4)));
ans2=((a-y(1))*(a-y(3))*(a-y(4)))*x(2)/((y(2)-y(1))*(y(2)-y(3))*(y(2)-y(4)));
ans3=((a-y(1))*(a-y(2))*(a-y(4)))*x(3)/((y(3)-y(1))*(y(3)-y(2))*(y(3)-y(4)));
ans4=((a-y(1))*(a-y(2))*(a-y(3)))*x(4)/((y(4)-y(1))*(y(4)-y(2))*(y(4)-y(3)));

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.

This is the simple function:


function y0 = lagrange_interp(x, y, x0)
% x is the vector of abscissas.
% y is the matching vector of ordinates.
% x0 represents the target to be interpolated
% y0 represents the solution from the Lagrange interpolation
y0 = 0;
n = length(x);
for j = 1 : n
t = 1;
for i = 1 : n
if i~=j
t = t * (x0-x(i))/(x(j)-x(i));
end
end
y0 = y0 + t*y(j);
end

Example 1 - Interpolate a cubic function


Consider the curve y = x3 - 3x + 3. We now that points
x = [-3 -2 -1 0 1 2 3];
y = [-15 1 5 3 1 5 21];
are on the curve. What are the values of y when x = -1.65 and 0.2?
x1 = -1.65;
y1 = lagrange_interp(x,y,x1)
x2 = .2;
y2 = lagrange_interp(x,y,x2)
The results are:
y1 = 3.4579
y2 = 2.4080
Lets plot our approach:
plot(x, y, 'bo', x1, y1, 'ro', x2, y2, 'ro')
axis([-4 4 -17 23])
title(y = x^3 3x + 3)
xlabel(x)
ylabel(y)

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

And our plot is:


plot (x, y, 'bo', x3, y3, 'ro', x4, y4, 'ro')
title('sin(x)')
xlabel('x')
ylabel('y')

The approximation is not bad, right? In fact, it seems to be quite accurate!

Вам также может понравиться