# Thread: Legendre Polynomial question - matlab

1. ## Legendre Polynomial question - matlab

Hi am trying to prove using the recursive formula that the legendre polynomial P3(x)=(1/2)*(5x^3 - 3x) .......(1)

This is the recursive formula

Pk+1=(1/k+1)*((2*k+1)*x*P2-k*P1)
where P2=(1/2)(3x^2 - 1) and P1=x

we have chosen k=2 so it gives Pk+1 = P3.

Everytime I try to evaluate this by hand, I dont get equation (1), which is what I need, can someone help me on this.

I even tried matlab which gave an ans = 45/4*x^3-27/4*x, which is still not (1).

Hi am trying to prove using the recursive formula that the legendre polynomial P3(x)=(1/2)*(5x^3 - 3x) .......(1)

This is the recursive formula

Pk+1=(1/k+1)*((2*k+1)*x*P2-k*P1)
where P2=(1/2)(3x^2 - 1) and P1=x

we have chosen k=2 so it gives Pk+1 = P3.

Everytime I try to evaluate this by hand, I dont get equation (1), which is what I need, can someone help me on this.

I even tried matlab which gave an ans = 45/4*x^3-27/4*x, which is still not (1).

The recurrence is:

$\displaystyle (n+1)P_{n+1}(x)=(2n+1)x P_n(x) - nP_{n-1}(x)$

Put $\displaystyle n=2$:

$\displaystyle 3P_3(x)=5xP_2(x)-2P_1(x)$

So:

$\displaystyle 6P_3(x)=5x(3x^2-1)-4x=15x^3-5x-4x=15x^3-9x$

hence:

$\displaystyle P_3(x)=\frac{5x^3-3x}{2}$

RonL

3. Hi, I am having trouble getting this legendre function to work in Matlab now. I am suppose to create a for-loop containing the recursion equation to create a vector called P, containing simplified symbolic formulae for the legendre polynomials from degree 1 up to degree 10. firstly I included the first two degree

here is my matlab code of I tried to do:

syms x k
x=linspace(-1,1,5);
P(1)=x;
P(2)=0.5*(3*x^2-1);
for k=1:9
P(k+1)=(1/k+1)*((2*k+1)*x*P(k)-k*P(k-1));
end

I keep getting this error from Matlab:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> legendrepoly at 4
P(1)=x;

Hi, I am having trouble getting this legendre function to work in Matlab now. I am suppose to create a for-loop containing the recursion equation to create a vector called P, containing simplified symbolic formulae for the legendre polynomials from degree 1 up to degree 10. firstly I included the first two degree

here is my matlab code of I tried to do:

syms x k
x=linspace(-1,1,5);
P(1)=x;
P(2)=0.5*(3*x^2-1);
for k=1:9
P(k+1)=(1/k+1)*((2*k+1)*x*P(k)-k*P(k-1));
end

I keep getting this error from Matlab:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> legendrepoly at 4
P(1)=x;
I don't know anything about Matlab, but it looks to me as though the loop should go from k=2 to 9, not 1 to 9. If you put k=1 then the program will look for P(k-1) and not find it.

Hi, I am having trouble getting this legendre function to work in Matlab now. I am suppose to create a for-loop containing the recursion equation to create a vector called P, containing simplified symbolic formulae for the legendre polynomials from degree 1 up to degree 10. firstly I included the first two degree

here is my matlab code of I tried to do:

syms x k
x=linspace(-1,1,5);
P(1)=x;
P(2)=0.5*(3*x^2-1);
for k=1:9
P(k+1)=(1/k+1)*((2*k+1)*x*P(k)-k*P(k-1));
end

I keep getting this error from Matlab:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> legendrepoly at 4
P(1)=x;
Well I don't have the symbolic tool boxed, so I don't know much about symbolic operations in Matlab.

But: you want x to be a symbolic variable, but you have assigned it to a numeric array (which may be arbitrary precision or floating point; I don't know) which I'm pretty sure is nonsense.

(and note opalg's comment about loop indices)

RonL