Results 1 to 7 of 7

Math Help - Legendre polynomial in MatLab

  1. #1
    Newbie
    Joined
    Apr 2009
    Posts
    9

    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by marty830 View Post
    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
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2009
    Posts
    9
    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.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Apr 2009
    Posts
    9
    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?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by marty830 View Post
    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
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Newbie
    Joined
    Apr 2009
    Posts
    9
    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.
    Last edited by CaptainBlack; April 5th 2009 at 08:07 PM.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by marty830 View Post
    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. legendre's polynomial...
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: April 12th 2011, 12:05 AM
  2. [SOLVED] Legendre Polynomial
    Posted in the Differential Equations Forum
    Replies: 0
    Last Post: September 26th 2010, 09:27 AM
  3. Legendre Type polynomial
    Posted in the Calculus Forum
    Replies: 4
    Last Post: December 21st 2008, 02:29 PM
  4. Legendre Polynomial question - matlab
    Posted in the Math Software Forum
    Replies: 4
    Last Post: October 2nd 2008, 06:15 AM
  5. Legendre polynomial property
    Posted in the Advanced Applied Math Forum
    Replies: 0
    Last Post: May 5th 2007, 08:53 AM

Search Tags


/mathhelpforum @mathhelpforum