Legendre Polynomials

Derivation

Just like in the dot product we have an inner product for the space of continuous functions on the interval [a,b], namely

⟨f,g⟩=(∫_a^b)f(x)g(x)dx.

Also, just like the dot product we can define an angle between two functions by

cos(θ)=((⟨f,g⟩)/([[f]][[g]])).

Then we can define the component of f in the direction of g by

(Subscript[comp, g])(f)=((⟨f,g⟩)/([[g]]))

and the projection of f onto g by

(Subscript[proj, g])(f)=(Subscript[comp, g])(f)·(g/([[g]])).

Now if {(Subscript[h, 1]),(Subscript[h, 2]),...,(Subscript[h, n])} are a linearly independent set of functions that span S, the the following Gram-Schmidt process can be used to generate a set of pair-wise orthogonal functions that also span S.

Set (Subscript[y, 1])=(Subscript[h, 1]) and (Subscript[y, k])=(Subscript[h, k])-(∑_(j=1)^(k-1))(Subscript[proj, (Subscript[y, j])])((Subscript[h, k])) for k=2, 3, ..., n.

Legendre Polynomials

Using the above process on the set {1,x,(x^2),(x^3),(x^4),(x^5)} for the interval [-1,1] gives

proj[f_,g_]:=Integrate[f[x]*g[x],{x,-1,1}]* g[x]/Integrate[g[x]^2,{x,-1,1}]

l0[x_]=1

h1[x_]=x;
l1[x_]=x-proj[h1,l0]

h2[x_]=x^2;
l2[x_]=x^2-proj[h2,l0]-proj[h2,l1]

h3[x_]=x^3;
l3[x_]=x^3-proj[h3,l0]-proj[h3,l1]-proj[h3,l2]

h4[x_]=x^4;
l4[x_]=x^4-proj[h4,l0]-proj[h4,l1]-proj[h4,l2]-proj[h4,l3]

Simplify[%]

h5[x_]=x^5;
l5[x_]=Simplify[x^5-proj[h5,l0]-proj[h5,l1]-proj[h5,l2]-proj[h5,l3]-proj[h5,l4]]

Example

Approximate (e^x) with a third degree polynomial (linear least squares).

The polynomial has the form g(x)=a0+a1 x+a2 (x^2)+a3 (x^3)=b0 l0[x]+b1 l1[x]+...

bob[f_,g_]:=Integrate[f[x]*g[x],{x,-1,1}]

q[x_]=Exp[x]

b0=bob[q,l0]/bob[l0,l0]

b1=bob[q,l1]/bob[l1,l1]

b2=bob[q,l2]/bob[l2,l2]

b3=bob[q,l3]/bob[l3,l3]

g[x_]=b0*l0[x]+b1*l1[x]+b2*l2[x]+b3*l3[x]

Simplify[N[g[x]]]

Plot[{q[x],g[x]},{x,-1,1},PlotStyle->{Blue,Red}]

Plot[Abs[q[x]-g[x]],{x,-1,1}]

dif[x_]=q[x]-g[x];

error=bob[dif,dif]

N[%]