Lagrange Interpolation in MATLAB 0 3482 Share on Facebook Tweet on Twitter Named after Joseph Louis Lagrange Lagrange !nterpo"ation is a popu"ar techni#ue of numerica" ana"$ ana"$sis sis for interp interpo"a o"atio tion n of po"$nom po"$nomia" ia"s% s% !n a set of distin distinct ct point point and number numberss & ' and $ ' resp respec ecti ti(e (e"$ "$ this this meth method od is the the po"$ po"$no nomi mia" a" of the the "eas "eastt degr degree ee at each each & ' b$ assuming assuming corresponding (a"ue at $ '% Lagrange )o"$nomia" !nterpo"ation is usefu" in Newton*+otes ,ethod of numerica" integration and in Shamir-s secret sharing scheme sharing scheme in +r$ptograph
%$MATLAB and go !n this tutoria" we-re going to write a program for Lagrange Interpolation in MATLAB through its mathematica" deri(ation a"ong with a numerica" e&"e% .ou can a"so check out our ear"ier tutoria" where we discussed a + program for this interpo"ation techni#ue%
Derivation of Lagrange Interpolation: +onsider a gi(en set of k/ points 1& 0 $0 1& $ 1 &2 $2%% 1&k $k where each points are distinct% Let-s Let-s assume a function L1& ' such that L1& ' $ ' ' 0 3 3 % %% k 5bser(ing the fo""owing points •
L '1& contains k factors in product and each factor has &
•
Now consider what happens when this product is e&panded% Since the product skips m ' when i ' then a"" terms are 6& ' 7 &m 9 6& ' 7 &m :"so when i ; ' m ; ' does not produce it and one term in the product wi"" be for m ! that is 6& 7 &i96& ' 7 &i 0
where =i' is >ronecker>ronecker-ss de"ta de"ta%% So it can be written that?
i
Therefore the considered function L1& is a po"$nomia" with degree at most k and where L1& ' $ ' So for a"" i ; ' " '1& inc"udes the term 1 & 7 & i in the numerator therefore the entire product wi"" be found to be @ero at & & '
This is the re#uired formu"a which wi"" a"so be used in the program code for Lagrange !nterpo"ation in ,:TL:A%
NOTE: The Lagrange !nterpo"ation formu"a can a"so be deri(ed from Newton-s di(ided difference formu"a% Bhen a po"$nomia" function f1& is be appro&imated with an n th degree po"$nomia" nth di(ided difference of f1& is constant and the 1n/ th di(ided difference is @ero% ,athematica""$ f6&0 & &2 &3 % % % % % %% % & n 0 A$ using the second propert$ of di(ided difference it can be written that
Simp"if$ing this e#uation we get
This can be represented as?
Lagrange Interpolation in MATLAB Code:
C Lagrange !nterpo"ation ,:TL:A )rogram
function 6)DS "agrangepo"$1E.EE E 6 2 3 4 G H 8I C inputting the (a"ues of gi(en & . 60 0 0 0 I C inputting the (a"ues of gi(en $ C6)DS "agrangepo"$1E.I && 0% ? 0%0 ? 8%I Cp"ot1&&po"$(a"1)&&E.orDS%b&&sp"ine1E.&&**g Cgrid Ca&is160% 8% * if si@e1E K I E EI end C checking for parameters if si@e1. K I . .I end if si@e1E K si@e1. K si@e1E2 M si@e1.2 error1both inputs must be e#ua"*"ength (ectors C disp"a$ing error end C end of scope of if N "ength1EI p(a"s @eros1NNI C for e(a"uating the po"$nomia" weights for each order for i ?N C the po"$nomia" with roots ma$ be (a"ues of E other than this one pp po"$1E1 1?N M iI p(a"s1i? pp %9 po"$(a"1pp E1iI end ) .p(a"sI if nargin3 .. po"$(a"1)EEI C output is .. with gi(en EE ) ..I C assigning to output end C end of scope of if if nargout K C checking for conndtions D roots1 11N*?*? % )1?1N* I if nargout K 2 C the e(a"ustion of acua" (a"ue at the poins of @ero deri(ati(e S po"$(a"1)DI end end
2 3 4 G H 8 O
C Lagrange !nterpo"ation ,:TL:A )rogram function 6)DS "agrangepo"$1E.EE E 6 2 3 4 G H 8I C inputting the (a"ues of gi(en & . 60 0 0 0 I C inputting the (a"ues of gi(en $
C6)DS "agrangepo"$1E.I && 0% ? 0%0 ? 8%I Cp"ot1&&po"$(a"1)&&E.orDS%b&&sp"i ne1E.&&**g Cgrid Ca&is160% 8% * 0 2 3 4 G H 8 O 20 2 22 23 24 2 2G 2H 28 2O 30 3 32 33 34 3 3G 3H 38 3O 40 4 42
if si@e1E K I E EI end C checking for parameters if si@e1. K I . .I end if si@e1E K si@e1. K si@e1E2 M si@e1.2 error1both inputs must be e#ua"*"ength (ectors C disp"a$ing error end C end of scope of if N "ength1EI p(a"s @eros1NNI C for e(a"uating the po"$nomia" weights for each order for i ?N C the po"$nomia" with roots ma$ be (a"ues of E other than this one pp po"$1E1 1?N M iI p(a"s1i? pp %9 po"$(a"1pp E1iI end ) .p(a"sI if nargin3 .. po"$(a"1)EEI C output is .. with gi(en EE ) ..I C assigning to output end C end of scope of if if nargout K C checking for conndtions D roots1 11N*?*? % )1?1N* I if nargout K 2 C the e(a"ustion of acua" (a"ue at the poins of @ero deri(ati(e S po"$(a"1)DI end end
The abo(e ,at"ab code for Lagrange method is written for interpo"ation of po"$nomia"s fitting a set of points% The program uses a user*defined function named L:PD:NPQ1E . with two input parameters which are re#uired to be row (ectors% The row (ectors E and . define a set of n points which are used in Lagrange method for the determination of 1n* th order po"$nomia" in E which passes through these points% The function of
) in the program is to return the n coefficients which define the po"$nomia" in the same order as used b$ )5L. and )5L.R:L% Simi"ar"$ D and S are defined to return &*coordinates and $*(a"ues at n* e&treme of the resu"ting po"$nomia"% .. returns the (a"ue of the po"$nomia" samp"ed at the points which are specified in EE% :"" the inputs which are re#uired to gi(e to the program are embedded in the source code% The (a"ues of E and . initia""$ set in the program are? E 6 2 3 4 G H 8 and . 60 0 0 0 : samp"e output of this ,:TL:A program is gi(en be"ow?
Numerical Example in Lagrange Interpolation: Now "et-s ana"$@e Lagrange !nterpo"ation and its ,at"ab code mathematica""$ using a different set of parameters% The #uestion here is? From the following sets of data, find the value of x corresponding to y=15 by using Lagrange Interpolation. 12 1G3 1O4 G
Solution Pi(en (a"ue of & and $ are? E?
G
O
.? 2 3 4 G A$ using the Lagrange !nterpo"ation Formu"a? &1$1$*31$*41$*G912*312*412*G / 1$*21$*41$*GG913*213*413*G / 1$*21$*31$*GO914*214*314*G / 1$*21$*31$*491G*21G*31G*4 A$ substituting $ we get & %I which is the re#uired answer%
MATLAB Code for Lagrange Interpolation ormula %Created by myclassbook.org %Created on 26 May 2013 %lagrange interpolation formula % Question: i!en set of !alues of " and y #$12ɥ&#'1(ќ& % )ind t*e !alue of " corresponding to y+1$ using lagrange interpolation clc, clear all, close all,
y+-12 13 1( 16, %C*ange *ere for different function "+-$ 6 ' 11, a+1$, %/pplying agranges nterpolation: ans1+##ay#2&&4#ay#3&&4#ay#(&&&4"#1&5##y#1&y#2&&4#y#1&y#3&&4#y#1&y#(&&&, ans2+##ay#1&&4#ay#3&&4#ay#(&&&4"#2&5##y#2&y#1&&4#y#2&y#3&&4#y#2&y#(&&&, ans3+##ay#1&&4#ay#2&&4#ay#(&&&4"#3&5##y#3&y#1&&4#y#3&y#2&&4#y#3&y#(&&&, ans(+##ay#1&&4#ay#2&&4#ay#3&&&4"#(&5##y#(&y#1&&4#y#(&y#2&&4#y#(&y#3&&&, m+ans1ans2ans3ans(, y " fprintf#t*e !alue of " corresponding to y+1$ is %fm&,
Lagrange Interpolation !curvilinear interpolation" The computations in this sma"" artic"e show the Lagrange interpolation% The code computes y* coordinates of points on a cur(e gi(en their x*coordinates% .ou must enter coordinates of #no$n point% on t&e curve no two ha(ing the same abscissa%
This is the simp"e 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 ! = " # n t = " for i = " # n if i$=! t = t (x0&x(i))'(x(!)&x(i)) end end y0 = y0 ty(!) end
Example ' ( Interpolate a cu)ic function +onsider the cur(e y x3 * 3 x / 3% Be now that points
x = &* &+ &" 0 " + * y = &"- " - * " - +" are on the cur(e% Bhat are the (a"ues of y when x *%G and 0%2
x" = &".- y" = lagrange_interp(x,y,x") x+ = .+ y+ = lagrange_interp(x,y,x+) The resu"ts are?
y" = *./-1 y+ = +./020 Let-s p"ot our approach?
plot(x, y, axis(&/ title(4y xlabel(4x7) ylabel(4y7)
3bo3, =
x", / x5*
y", 6
3ro3, x+, &" *x
y+,
3ro3) +*) *7)
Example * Pi(en the fo""owing points from a sine cur(e what are the y*(a"ues for x *24H and x %
x = &- &/ &* &+ &" 0 " + * / - y = .1-2 .- &."/" &.101 &.2/" 0 .2/" .101 ."/" &.- &.1-1 x* = &+./ y* = lagrange_interp(x,y,x*)
x/ = ".- y/ = lagrange_interp(x,y,x/) The resu"ts are?
y* = &0.+"2 y/ = 0.11+ :nd our p"ot is?
plot (x, y, 3bo3, x*, y*, 3ro3, x/, y/, 3ro3) title(3sin(x)3) xlabel(3x3) ylabel(3y3)
The appro&imation is not bad right !n fact it seems to be #uite accurate