## Section21.3Legendre Series: Worked Example

You are encouraged to explore the Legendre basis functions using the applet in Figure 21.2 before working through the example here.

The unrenormalized Legendre polynomials can be defined as

\begin{align} P_0(x) \amp= 1,\tag{21.3.1}\\ P_1(x) \amp= x,\tag{21.3.2}\\ (n+1)P_{n+1}(x) \amp= (2n+1)x P_n(x) - nP_{n-1}(x)\tag{21.3.3} \end{align}

and satisfy the orthogonality condition

$$\int_{-1}^1 P_m(x)P_n(x) = \frac{2}{2n+1} \delta_{mn} .\tag{21.3.4}$$

We can expand any function $$f(x)$$ on the interval $$-1\le x\le1$$ in terms of these Legendre polynomials as

$$f(x) = \sum_0^\infty a_m P_m(x)\tag{21.3.5}$$

and use (21.3.4) to determine the coefficients as

$$a_n = \frac{2n+1}{2} \int_{-1}^1 f(x) P_n(x) .\tag{21.3.6}$$

You can use the Sage code below to display the Legendre polynomial $$P_n(x)$$ for any integer value of $$n\text{,}$$ and then its graph.

pretty_print_default(True)
n=2
legendre_P(n,x)

plot(legendre_P(n,x),x,-1,1)


Now we will use the integral expressions (21.3.6) for the coefficients in a Legendre expansion to work out an example, the Legendre series for the function $$f(x)=\frac32(x^2+x^3)\text{.}$$ Then you will plot the individual terms in the Legendre series and their partial sums using an applet.

You can use the Sage code below to calculate the value of the integral for the first coefficient, $$a_0\text{.}$$

m=0
f=3/2*(x^2+x^3)
(2*m+1)/2*integrate(f*legendre_P(m,x),x,-1,1)


Using the applet in Figure 21.5, set the $$a_0$$ slider to correspond to the value you just calcuated.  1  Compare your approximate Legendre series, containing just one term (shown in green), to the actual function (shown in blue).

Now you can alter the Sage code above to compute the other coefficients. After each calculation, move the corresponding slider to the value you obtain, and compare the approximation to the given function.

If you move the sliders one by one, resetting the others to zero, you will see how much each individual term contributes to the Legendre series.

If you don't reset the other sliders, but instead combine the contributions from each slider, the applet plots the sum of the corresponding terms (in green), representing an approximation to the actual function (in blue). In the given example, you should obtain an exact match when you include all of the terms with $$m=0,1,2,3\text{.}$$ In general, there are an infinite number of nonzero terms in the Legendre series; your approximation will get better and better as you include more terms.

You can use the right and left arrow keys to move the selected slider in increments of 0.5; holding down the shift key as well changes the increment to 0.05.