Physics 350:
Computational Methods
for the Physical Sciences

Spring Semester 2009

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);
int(`*`(P(m, x), `*`(P(l, x))), x = -1 .. 1) (1)

So I will test a few specific cases: 

> l := 5; m := 7;

 

5

7
> inner_product := int(P(m,x)*P(l,x),x=-1..1);
0
> l := 0; m := 1;

 

0

1 (2)
> inner_product := int(P(m,x)*P(l,x),x=-1..1);
0 (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);

 

 

0

2

2 (4)
> l := 1;inner_product2 := int(P(l,x)*P(l,x),x=-1..1);predicted := 2/(2*l+1);

 

 

1

`/`(2, 3)

`/`(2, 3)
> l := 5;inner_product2 := int(P(l,x)*P(l,x),x=-1..1);predicted := 2/(2*l+1);

 

 

5

`/`(2, 11)

`/`(2, 11) (5)

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)
> local a;
> a := ((2*n+1)/2)*int(f*P(n,x),x=-1..1);
> return(a);
> end;

And I am supposed to test it on the Heaviside function: 

> lcoef0 := legcoeff(Heaviside(x),x,0);
`/`(1, 2)
> lcoef1 := legcoeff(Heaviside(x),x,1);
`/`(3, 4) (6)
> lcoef2 := legcoeff(Heaviside(x),x,2);
0 (7)
> lcoef3 :=legcoeff(Heaviside(x),x,3);
-`/`(7, 16) (8)
> lcoef4 := legcoeff(Heaviside(x),x,4);
0 (9)
> lcoef5 := legcoeff(Heaviside(x),x,5);
`/`(11, 32) (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)
> local s;
> s := sum('legcoeff(f,x,n)*P(n,x)','n'=0..nmax);
> return(collect(s,x));
> end;


The 10th order Legendre series fit to the Heaviside(x) function is: 

> Hser10 := legseries(Heaviside(x),x,10);
`+`(`/`(1, 2), `*`(`/`(218295, 65536), `*`(x)), `-`(`*`(`/`(315315, 16384), `*`(`^`(x, 3)))), `*`(`/`(1702701, 32768), `*`(`^`(x, 5))), `-`(`*`(`/`(984555, 16384), `*`(`^`(x, 7)))), `*`(`/`(1616615, 6...
> plot({Heaviside(x),Hser10},x=-1..1,color=[red,blue]);
Plot_2d
> Hser00 := legseries(Heaviside(x),x,0); Hser01 := legseries(Heaviside(x),x,1);Hser03 := legseries(Heaviside(x),x,3);

 

 

`/`(1, 2)

`+`(`/`(1, 2), `*`(`/`(3, 4), `*`(x)))

`+`(`/`(1, 2), `*`(`/`(45, 32), `*`(x)), `-`(`*`(`/`(35, 32), `*`(`^`(x, 3)))))
> Hser05 := legseries(Heaviside(x),x,5);Hser20 := legseries(Heaviside(x),x,20);Hser40 := legseries(Heaviside(x),x,40);

 

 

`+`(`/`(1, 2), `*`(`/`(525, 256), `*`(x)), `-`(`*`(`/`(525, 128), `*`(`^`(x, 3)))), `*`(`/`(693, 256), `*`(`^`(x, 5))))

`+`(`/`(1, 2), `*`(`/`(224009490705, 34359738368), `*`(x)), `-`(`*`(`/`(418881139451775, 34359738368), `*`(`^`(x, 19)))), `*`(`/`(2052707122290825, 34359738368), `*`(`^`(x, 17))), `*`(`/`(123333130632...
`+`(`/`(1, 2), `*`(`/`(224009490705, 34359738368), `*`(x)), `-`(`*`(`/`(418881139451775, 34359738368), `*`(`^`(x, 19)))), `*`(`/`(2052707122290825, 34359738368), `*`(`^`(x, 17))), `*`(`/`(123333130632...
`+`(`/`(1, 2), `*`(`/`(224009490705, 34359738368), `*`(x)), `-`(`*`(`/`(418881139451775, 34359738368), `*`(`^`(x, 19)))), `*`(`/`(2052707122290825, 34359738368), `*`(`^`(x, 17))), `*`(`/`(123333130632...

`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
`+`(`/`(1, 2), `*`(`/`(243458839317702098215125, 18889465931478580854784), `*`(x)), `-`(`*`(`/`(2013999639636949265890437631657875, 9444732965739290427392), `*`(`^`(x, 19)))), `*`(`/`(5930830081737014...
> plot([Heaviside(x),Hser00,Hser01,Hser03,Hser05],x=-1..1,color=[red,yellow,green,blue,maroon]);
Plot_2d
> plot([Heaviside(x),Hser20,Hser40],x=-1..1,color=[red,blue,maroon]);
Plot_2d

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]);

 

Plot_2d

Plot_2d