Chapter3InterpolationInterpolationistheprocessofdefiningafunctionthattakesonspecifiedvaluesatspecifiedpoints.Thischapterconcentratesontwocloselyrelatedinterpolants,thepiecewisecubicsplineandtheshape-preservingpiecewisecubicnamed“pchip”.3.1TheInterpolatingPolynomialWeallknowthattwopointsdetermineastraightline.Moreprecisely,anytwopointsintheplane,),(11yxand),(11yx,with)(21xx,determineauniquefirstdegreepolynomialinxwhosegraphpassesthroughthetwopoints.Therearemanydifferentformulasforthepolynomial,buttheyallleadtothesamestraightlinegraph.Thisgeneralizestomorethantwopoints.Givennpointsintheplane,),(kkyx,nk,,2,1,withdistinctkx’s,thereisauniquepolynomialinxofdegreelessthannwhosegraphpassesthroughthepoints.Itiseasiesttorememberthatn,thenumberofdatapoints,isalsothenumberofcoefficients,althoughsomeoftheleadingcoefficientsmightbezero,sothedegreemightactuallybelessthan1n.Again,therearemanydifferentformulasforthepolynomial,buttheyalldefinethesamefunction.Thispolynomialiscalledtheinterpolatingpolynomialbecauseitexactlyre-producesthegivendata.nkyxPkk,,2,1,)(,Later,weexamineotherpolynomials,oflowerdegree,thatonlyapproximatethedata.Theyarenotinterpolatingpolynomials.ThemostcompactrepresentationoftheinterpolatingpolynomialistheLa-grangeform.kkkjjkjyxxxxxP)(Therearentermsinthesumand1ntermsineachproduct,sothisexpressiondefinesapolynomialofdegreeatmost1n.If)(xPisevaluatedatkxx,alltheproductsexceptthektharezero.Furthermore,thekthproductisequaltoone,sothesumisequaltokyandtheinterpolationconditionsaresatisfied.Forexample,considerthefollowingdataset:x=0:3;y=[-5-6-116];Thecommanddisp([x;y])displays0123-5-6-116TheLagrangianformofthepolynomialinterpolatingthisdatais)16()6()2)(1()1()2()3)(1()6()2()3)(2()5()6()3)(2)(1()(xxxxxxxxxxxxxPWecanseethateachtermisofdegreethree,sotheentiresumhasdegreeatmostthree.Becausetheleadingtermdoesnotvanish,thedegreeisactuallythree.Moreover,ifweplugin2,1,0xor3,threeofthetermsvanishandthefourthproducesthecorrespondingvaluefromthedataset.PolynomialsareusuallynotrepresentedintheirLagrangianform.Morefre-quently,theyarewrittenassomethinglike523xxThesimplepowersofxarecalledmonomialsandthisformofapolynomialissaidtobeusingthepowerform.Thecoefficientsofaninterpolatingpolynomialusingitspowerform,nnnncxcxcxcxP12211)(can,inprinciple,becomputedbysolvingasystemofsimultaneouslinearequationsnnnnnnnnnnnyyycccxxxxxxxxx21212122212121111111ThematrixVofthislinearsystemisknownasaVandermondematrix.Itselementsarejnkjkxv,ThecolumnsofaVandermondematrixaresometimeswrittenintheoppositeorder,butpolynomialcoefficientvectorsinMatlabalwayshavethehighestpowerfirst.TheMatlabfunctionvandergeneratesVandermondematrices.Forourex-ampledataset,V=vander(x)generatesV=00011111842127931Thenc=V\y’computesthecoefficientsc=1.00000.0000-2.0000-5.0000Infact,theexampledatawasgeneratedfromthepolynomial523xx.OneoftheexercisesasksyoutoshowthatVandermondematricesarenonsin-gularifthepointskxaredistinct.ButanotheroneoftheexercisesasksyoutoshowthataVandermondematrixcanbeverybadlyconditioned.Consequently,usingthepowerformandtheVandermondematrixisasatisfactorytechniqueforproblemsinvolvingafewwell-spacedandwell-scaleddatapoints.Butasageneral-purposeapproach,itisdangerous.Inthischapter,wedescribeseveralMatlabfunctionsthatimplementvariousinterpolationalgorithms.Allofthemhavethecallingsequencev=interp(x,y,u)Thefirsttwoinputarguments,xandy,arevectorsofthesamelengththatdefinetheinterpolatingpoints.Thethirdinputargument,u,isavectorofpointswherethefunctionistobeevaluated.Theoutput,v,isthesamelengthasuandhaselements))(,,()(kuyxterpinkvOurfirstsuchinterpolationfunction,polyinterp,isbasedontheLagrangeform.ThecodeusesMatlabarrayoperationstoevaluatethepolynomialatallthecomponentsofusimultaneously.functionv=polyinterp(x,y,u)n=length(x);v=zeros(size(u));fork=1:nw=ones(size(u));forj=[1:k-1k+1:n]w=(u-x(j))./(x(k)-x(j)).*w;endendv=v+w*y(k);Toillustratepolyinterp,createavectorofdenselyspacedevaluationpoints.u=-.25:.01:3.25;Thenv=polyinterp(x,y,u);plot(x,y,’o’,u,v,’-’)createsfigure3.1.Figure3.1.polyinterpThepolyinterpfunctionalsoworkscorrectlywithsymbolicvariables.Forexample,createsymx=sym(’x’)ThenevaluateanddisplaythesymbolicformoftheinterpolatingpolynomialwithP=polyinterp(x,y,symx)pretty(P)produces-5(-1/3x+1)(-1/2x+1)(-x+1)-6(-1/2x+3/2)(-x+2)x-1/2(-x+3)(x-1)x+16/3(x-2)(1/2x-1/2)xThisexpressionisarearrangementoftheLagrangeformoftheinterpolatingpoly-nomial.SimplifyingtheLagrangeformwithP=simplify(P)changesPtothepowerformP=x^3-2*x-5Hereisanotherexample,withadatasetthatisusedbytheothermethodsinthischapter.x=1:6;y=[161821171512];disp([x;y])u=.75:.05:6.25;v=polyinterp(x,y,u);plot(x,y,’o’,u,v,’-’);produces123456161821171512createsfigure3.2.Figure3.2.FulldegreepolynomialinterpolationAlreadyinthisexample,withonlysixnicelyspacedpoints,wecanbegintoseetheprimarydifficultywithfull-degreepolynomialinterpolation.Inbetweenthedatapoints,especiallyinthefirstandlastsubintervals,thefunctions