# Thread: Legendre polynomial in MatLab

1. ## Legendre polynomial in MatLab

Hello,

this is my first assignement in matlab and I'm not sure what I'm doing wrong. It's probably a small error but I can't find it. Here's my code so far:

Code:
k = n/2;
X = (((-1)^(n - k))/(2^n))*((factorial(2*k))/((factorial(2*k - n))*(factorial(k))*(factorial(n - k)))*(x^(2*k - n)));
ceil (x)
It's for calculating Legendre polynomials of grade n. Any hints on how to show this result as a vector would also be greatly appreciated.

2. Originally Posted by marty830
Hello,

this is my first assignement in matlab and I'm not sure what I'm doing wrong. It's probably a small error but I can't find it. Here's my code so far:

Code:
k = n/2;
X = (((-1)^(n - k))/(2^n))*((factorial(2*k))/((factorial(2*k - n))*(factorial(k))*(factorial(n - k)))*(x^(2*k - n)));
ceil (x)
It's for calculating Legendre polynomials of grade n. Any hints on how to show this result as a vector would also be greatly appreciated.
First of all type:

>help nchoosek

at the commant prompt.

Second; what is ceil(x) doing? Also x and X are different variables.

CB

3. Thanks, I will look at that help file immediatly. I used ceil (x) because we were asked to round our results up to the closest number larger or equal then x.

4. Ok don't know why I should use the binomium, maybe to create the vector? But I found another code which uses the rodrigues formula, which is derived from Newtons Binomium.

Code:
n = 2;
k = 2/n;
j = 2*k-n;
a = (((-1)^((n-j)/2))/(2^n))*((factorial(n + j))/((factorial (j))*(factorial((n+j)/2))*(factorial((n-j)/2))))
This doesn't give me a vector either, but this one will run (only for n = 2) and outputs -0.5000. The solution should be 1.5000 0 -0.5000. Am I using the right variables?

5. Originally Posted by marty830
Hello,

this is my first assignement in matlab and I'm not sure what I'm doing wrong. It's probably a small error but I can't find it. Here's my code so far:

Code:
k = n/2;
X = (((-1)^(n - k))/(2^n))*((factorial(2*k))/((factorial(2*k - n))*(factorial(k))*(factorial(n - k)))*(x^(2*k - n)));
ceil (x)
It's for calculating Legendre polynomials of grade n. Any hints on how to show this result as a vector would also be greatly appreciated.
Can you tell us exactly what your assignment is asking you to do.

CB

6. Hello,

I have done some more research and now I have this code=

Code:
function L = opdracht1(n)
if n == 0;
L = 1
elseif n == 1;
L = [1;
0]
else
x = zeros (n+1,1);
for j = 0:n;
if mod(n-j,2)==0;
a(j) = (((-1).^((n - j)/2))./(2^n).*(factorial(n + j))./((factorial (j)).*(factorial((n+j)/2)).*(factorial((n-j)/2))));
L = x(j) + a
end
end
end
The function of this code is to spit out the coefficients of the legendre polynomial Pn(x). This code works as it calculates the following vector for n= 2, if it starts counting from j=1:

0 1.5000

For some reason it wont calculate from j=0, so I don't get the full solution which is:

-0.5000 0 1.5000

That's the only problem left in this code. When I let it start counting from 0 it produces an error.

7. Originally Posted by marty830
Hello,

I have done some more research and now I have this code=

Code:
function L = opdracht1(n)
if n == 0;
L = 1
elseif n == 1;
L = [1;
0]
else
x = zeros (n+1,1);
for j = 0:n;
if mod(n-j,2)==0;
a(j) = (((-1).^((n - j)/2))./(2^n).*(factorial(n + j))./((factorial (j)).*(factorial((n+j)/2)).*(factorial((n-j)/2))));
L = x(j) + a
end
end
end
The function of this code is to spit out the coefficients of the legendre polynomial Pn(x). This code works as it calculates the following vector for n= 2, if it starts counting from j=1:

0 1.5000

For some reason it wont calculate from j=0, so I don't get the full solution which is:

-0.5000 0 1.5000

That's the only problem left in this code. When I let it start counting from 0 it produces an error.
Matlab array indexing starts from 1, you can't do a(1)=...

CB