Lagrange Polynomials

They make fitting points easy!

Lagrange linear functions are linear functions which are 0 at x1, and 1 at x2:

lagrange[x_,x1_,x2_]:=(x-x2)/(x1-x2)
{x0,x1}={50,75}
{y0,y1}={180,230}
Plot[{lagrange[x,x0,x1],lagrange[x,x1,x0]},{x,x0,x1}]

Let’s use these Lagrange linear functions  fit two points: (50,180), (75, 230):

p1=ListPlot[{{x0,y0},{x1,y1}}];
p2=Plot[y0 lagrange[x,x0,x1]+y1 lagrange[x,x1,x0],{x,x0,x1}];
Show[p1,p2]

Lagrange quadratics are just products of Lagrange linear functions:

lagrange2[x_,x1_,x2_,x3_]:=lagrange[x,x1,x2]*lagrange[x,x1,x3]

Let’s fit three points: (50,180), (75, 190), (60,210)

{x0,x1,x2}={50,75,60}
{y0,y1,y2}={180,230,210}
p1=ListPlot[{{x0,y0},{x1,y1},{x2,y2}}];
p2=Plot[y0 lagrange2[x,x0,x1,x2]+y1 lagrange2[x,x1,x2,x0]+y2 lagrange2[x,x2,x0,x1],{x,x0,x1}];
Show[p1,p2]

Lagrange cubics are also just products of Lagrange linear functions:

lagrange3[x_,x1_,x2_,x3_,x4_]:=lagrange[x,x1,x2]*lagrange[x,x1,x3]*lagrange[x,x1,x4]

Let’s fit four points: (50,180), (75, 230), (60,210), (70,215)

{x0,x1,x2,x3}={50,75,60,70}
{y0,y1,y2,y3}={180,230,210,215}
p1=ListPlot[{{x0,y0},{x1,y1},{x2,y2},{x3,y3}}];
p2=Plot[y0 lagrange3[x,x0,x1,x2,x3]+y1 lagrange3[x,x1,x2,x0,x3]+y2 lagrange3[x,x2,x0,x1,x3]+y3 lagrange3[x,x3,x2,x0,x1],{x,x0,x1}];
Show[p1,p2]