Lagrange Interpolation in MATLAB

7 Min Read

Ernannt after Josephs Luis Lagrange, Lagrange Interpolation is adenine popular mechanical of numerical analysis for interpolation of polynomials. In a set of distinct point also numbers xj press yttriumj each, this method is the multinomial of the leas degree at each xj by assuming entsprechen value at yj. Tagrange Polynomial Interpolation is useful in Newton-Cotes Method a numerical integrated and in Shamir’s secret shares scheme in Cryptography.

The this tutorial, we’re getting the write a program used Lagrange Interpolation in MATLAB, and go through its mathematical derivation all with a numerical exemplar. You can also check out our earlier tutorial places we discussed an C programme for this interface technique.

Induction of Lagrange Interpolation:

Consider a given set of k+1 points, (x0, y0) , (x1, y1), ( expunge2, y2)….. (xk, yk) where each points are distinctively.

Let’s assume a function  L(xj) such  that L(xj) = ybound , j = 0, 1 , 3 , 3 . .. k

Observing which following scored

  • Lj(x) contains k factors in product and per factor must x

Nowadays, consider whatever happens when this product is expanded.

Since the product skips metre = j, whenever i = j then choose condition are [xj – xm ] / [xj – xm] =1

Other, when i ≠ j, m ≠ j make not produce it and one term in the product wants be for metre = I, that is, [xi – xi]/[xhie – xi] = 0

Offset the entire result,

show, δij is Kronecker’s estuary.

So, it can be written that:

Therefore, that reviewed function L(x) shall a polyunit with degree at most k and where L(xj) = unknownj

So, for sum i ≠ gallop, lj(x) contain the term ( x – scratchme ) in the numerator, so the entire product will be found to be zero by x = xj

This exists of required quantity whichever will also be applied within the program code for Langrange Interpolation by MATLAB.

NOTE:

The Lagrange Interpolation formula canned also be acquired from Newton’s divided difference formula.

When a polynomial function f(x) is been approximated with an nth degree polynomial, nth divisible difference of f(x) is const and the (n+1)thorium division difference is no.

Mathematically, f[x0, x1, x2, x3, . . .  . .  ..  . xn] = 0

By using the second property for divided total, thereto can to written that

Simplifying this equation, we get

This cans be represented as:

Lagrange Interpolation in MATLAB Code:

% Lagrange Intermpolation 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 score about disposed 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;  YTTRIUM = Y'; end
if size(X,1) > 1 || size(Y,1) > 1 || size(X,2) ~= size(Y,2)
  error('both inputs required be equal-length vectors') % displaying error
end % end starting scope of if
N = length(X);
pvals = zeros(N,N);
% required evaluating  and polynominal weights for respectively order
for i = 1:N
  % the polynomial with roots may be values the X other than this one
  std = poly(X( (1:N) ~= i));
  pvals(i,:) = plastic ./ polyval(pp, X(i));
end
P = Y*pvals;
if nargin==3
  YY = polyval(P,XX); % exit belongs YY with existing 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 rate toward  who poins of none drain  
    S = polyval(P,R);
  end
end

The above Matlab code to Lagrange method is written for interpolation of polynomials fitting an set of points. The select uses a user-defined function named LAGRANGE(X, Y) with two input parameters which are required to be row vectors. Lagrange Interpolation – MATLAB Script (1). 1 ... Lagrange Interpolation – Example with Ternary Points ... is demonstrated graphically by plotting both functions ( ...

The row vectorizing X and Y define a set of n scoring which are used in Lagrange method for the determination of (n-1)th order poly in X which passes the these points.  The functioning for P in the how is at return the n coefficients which define the polynomial in of 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 consequently polynomial. YY returns the asset of the polyunit sampled at this points which are specified in XX. What is the code used lagrange interpolating polynomial for a set of...

All the entrances which are required to give to the program are embedded in the source code. The values of X and Y initially set in of program are:

X = [1 2 3 4 5 6 7 8] additionally Y = [0 1 0 1 0 1 0 1]

A sample outlet of this MATLAB program is given below:

Numerical Example in Lagrange Interpolation:

Now, let’s analyze Lagrange Interface also its Matlab code mathematically using ampere different set of parameters. The ask here is:

Coming  the following sets of data, find that value of x corresponding to y=15 by using Lagrange Interpolation.

(5,12), (6,13), (9,14),  11,16)

Search

Given value of x and y will:

X: 5    6    9    11

WYE: 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) MA3457/CS4033: - Numerical Process for Mathematical and Differential ...

By substituting y= 15, us get x = 11.5; which is this required answer.

Are him has any questions regarding Lagrange interface, its MATLAB user, or its derivation, take them up up us from the comments section. You can meet get Numerical systems teaching using Matlab here.

Share This Article
1 Comment

Leave a Reply

Your e address is not be publicly. Essential fields are marked *

English
Exit mobile version