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:
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.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)
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.
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?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))))
I have done some more research and now I have this code=
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: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
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.