legendre0 edit page

Evaluate all Legendre polynomials up to degree N in x and returns a matrix of the function values 1st dimension -> degree 2nd dimension -> x Use the recurrence formula (n+1)*P_{n+1} = (2n+1)*x*P_n - n*P_{n-1}

Syntax

l = legendre0(N,x)

Input

N degree
x input nodes

Output

l function evaluations

Example

x = -1:0.2:1
l = legendre0(10,x)
x =
  Columns 1 through 7
   -1.0000   -0.8000   -0.6000   -0.4000   -0.2000         0    0.2000
  Columns 8 through 11
    0.4000    0.6000    0.8000    1.0000
l =
  Columns 1 through 7
    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000
   -1.0000   -0.8000   -0.6000   -0.4000   -0.2000         0    0.2000
    1.0000    0.4600    0.0400   -0.2600   -0.4400   -0.5000   -0.4400
   -1.0000   -0.0800    0.3600    0.4400    0.2800         0   -0.2800
    1.0000   -0.2330   -0.4080   -0.1130    0.2320    0.3750    0.2320
   -1.0000    0.3995    0.1526   -0.2706   -0.3075         0    0.3075
    1.0000   -0.3918    0.1721    0.2926   -0.0806   -0.3125   -0.0806
   -1.0000    0.2397   -0.3226    0.0146    0.2935         0   -0.2935
    1.0000   -0.0167    0.2123   -0.2670   -0.0396    0.2734   -0.0396
   -1.0000   -0.1879    0.0461    0.1888   -0.2460         0    0.2460
    1.0000    0.3005   -0.2437    0.0968    0.1291   -0.2461    0.1291
  Columns 8 through 11
    1.0000    1.0000    1.0000    1.0000
    0.4000    0.6000    0.8000    1.0000
   -0.2600    0.0400    0.4600    1.0000
   -0.4400   -0.3600    0.0800    1.0000
   -0.1130   -0.4080   -0.2330    1.0000
    0.2706   -0.1526   -0.3995    1.0000
    0.2926    0.1721   -0.3918    1.0000
   -0.0146    0.3226   -0.2397    1.0000
   -0.2670    0.2123   -0.0167    1.0000
   -0.1888   -0.0461    0.1879    1.0000
    0.0968   -0.2437    0.3005    1.0000

compare with MATLABs legendre, whose first row is the Legendre polynomial of degree k

err = 0;
for k = 0:10
P = legendre(k,x);
err = max(err,max(abs(P(1,:) - l(k+1,:))));
end
err
err =
   3.8858e-16