Write a program to compute the Legendre polynomials of order at most n at a given point x. Then write a program to find all of the zeros of these Legendre polynomials. Make a table of the zeros for all orders between 1 and 10. HINT: the zeros of p_k are real, between -1 and 1, and interlace the zeros of p_{k-1}: the first is between -1 and the first zero of p_{k-1}, the next is between the first 2 zeros of p_{k-1},... ane the last is between the last zero of p_{k-1} and 1.
Solution:
We use the three-term recurrence relation
p_k(x) = (2k-1)/k * x * p_{k-1}(x) - (k-1)/k p_{k-2}(x), k = 1, 2, 3, ...
and p_{-1} = 0, p_{0} = 1.
"legendre.c" is written in C++ and compiled by "g++ -o legendre legendre.c". Run "./legendre". It asks input of the order "N" and variable "x" on screen and outputs P_{N}(x) on the screen. It then calculates P_{n}(x) of order n = 0, 1, 2, ..., N at x from -1 to 1 with increment dx=0.01, and outputs the results in "px.out". The results for Legendre polynomials of order up to 5 are shown in this figure draw by gnuplot script "plot"
"legendrezero.c" is compiled by "g++ -o legendrezero legendrezero.c". Run "./legendrezero", which asks input of polynomial order "N" and root accuracy "accuracy" on screen, will output zeros of Legendre polynomials of order n = 1, 2, ..., N in the file "zero.txt". Bisection algorithm is used for the root finding. The jth root of the nth order polynomial (n order Legendre polynomial has j = 0, 1, ..., n-1, in total n roots within [-1, 1]) is bracketed in a interval (a, b), where a is the (j-1)th root of the (n-1)th order polynomial and b is the jth root of the (n-1)th order polynomial (on the left end interval a = -1, on the right end interval b=1). The results of N=10 and accuracy=0.0000001 are shown in the table.