Lab 6: Legendre Polynomials Sample Solutions
As usual, reset things and add the orthopoly routines for efficient calculation of Legendre polynomials;
| > |
restart; with(plots): with(orthopoly): |
1. I am first asked to verify that the Legendre polynomials are in fact othogonal to one another usng the values of l and m that differ from one another. I could try to test this in general, but Maple won't simplify it.
| > |
assume(l,integer); assume(m, integer); |
| > |
inner_product := int(P(m,x)*P(l,x),x=-1..1); |
 |
(1) |
So I will test a few specific cases:
| > |
inner_product := int(P(m,x)*P(l,x),x=-1..1); |
 |
|
 |
(2) |
| > |
inner_product := int(P(m,x)*P(l,x),x=-1..1); |
 |
(3) |
2. Next we are asked to verify expression 2 which leads to the normalization constant for the Legendre Polynomials.
| > |
l := 0;inner_product2 := int(P(l,x)*P(l,x),x=-1..1);predicted := 2/(2*l+1); |
| > |
l := 1;inner_product2 := int(P(l,x)*P(l,x),x=-1..1);predicted := 2/(2*l+1); |
| > |
l := 5;inner_product2 := int(P(l,x)*P(l,x),x=-1..1);predicted := 2/(2*l+1); |
And since for l=5, l/(2*l+1) = 2/11, we have verified the suggested relationship for the integrals.
3. a. Now I am asked to write a procedure/routine to solve for the n'th coefficient of the legendre series of a function of f.
| > |
legcoeff := proc (f,x,n) |
| > |
a := ((2*n+1)/2)*int(f*P(n,x),x=-1..1); |
And I am supposed to test it on the Heaviside function:
| > |
lcoef0 := legcoeff(Heaviside(x),x,0); |
| > |
lcoef1 := legcoeff(Heaviside(x),x,1); |
 |
(6) |
| > |
lcoef2 := legcoeff(Heaviside(x),x,2); |
 |
(7) |
| > |
lcoef3 :=legcoeff(Heaviside(x),x,3); |
 |
(8) |
| > |
lcoef4 := legcoeff(Heaviside(x),x,4); |
 |
(9) |
| > |
lcoef5 := legcoeff(Heaviside(x),x,5); |
 |
(10) |
Done. And I compared the Heaviside(x) function (0 for x<0, 1 for x>0) to the solution on p. 580-581 of Boas. My legendre coefficients seem to be working out the same.
4. Now we are asked to use this legcoeff procedure along with the "sum" function to write a procedure called legseries to compute the legendre series for a function to the nmax'th order. We are then asked to plot the 0th,1st,3rd,5th,10th,20th, and 40th order (10th order first) legendre series fits to heaviside(x) versus the original function.
| > |
legseries := proc(f,x,nmax) |
| > |
s := sum('legcoeff(f,x,n)*P(n,x)','n'=0..nmax); |
The 10th order Legendre series fit to the Heaviside(x) function is:
| > |
Hser10 := legseries(Heaviside(x),x,10); |
| > |
plot({Heaviside(x),Hser10},x=-1..1,color=[red,blue]); |
| > |
Hser00 := legseries(Heaviside(x),x,0); Hser01 := legseries(Heaviside(x),x,1);Hser03 := legseries(Heaviside(x),x,3); |
| > |
Hser05 := legseries(Heaviside(x),x,5);Hser20 := legseries(Heaviside(x),x,20);Hser40 := legseries(Heaviside(x),x,40); |
| > |
plot([Heaviside(x),Hser00,Hser01,Hser03,Hser05],x=-1..1,color=[red,yellow,green,blue,maroon]); |
| > |
plot([Heaviside(x),Hser20,Hser40],x=-1..1,color=[red,blue,maroon]); |
And just for humor's sake, let's do some a higher-order polynomials and see where numerical noise starts hurting us.
| > |
Hser80 := legseries(Heaviside(x),x,80): |
| > |
plot([Heaviside(x),Hser80],x=-1..1,color=[red,blue]);plot([Heaviside(x),Hser80],x=-0.5..0.5,color=[red,blue]); |