Thread: A programme for computing Legendre polynomials...

  1. #1
    Registered User Aoife :)'s Avatar
    Join Date
    Oct 2010

    A programme for computing Legendre polynomials...

    Greetings to all

    I'm pretty much new to programming in C, so please excuse my mistakes. Since I'm a physics student, I mostly do applications of mathematical formulas and such. I've written a short code which should compute any Legendre polynomial for any l>0, using the formula P_l= ((2l+1)/l)*x*P_1- ((l-1)/l)*P_0.

    I've developed the code in Dev-C++, and I can compile and run it. But whenever I type in the numbers needed, the program just closes abruptly! I honestly don't know what's the problem here, and that's why I'm posting this. If anyone could give me a hint, I would be most grateful. Thanks in advance!

    #include <stdio.h>
    int main(void){
        int l, i;
        float x, P[l];
        printf("Type in l i x\n");
        scanf("%n %f", &l, &x);
               printf("P = 1\n");
        else if(l=1)
             printf("P = x\n");
        else if(l>1)
        {for(i=1; i<l; i++)
        printf("The polynomial is: %f", P[l]);}
        return 0;}

  2. #2
    Registered User
    Join Date
    Jun 2009
    Does that even compile? If it does, it must at least give you warnings or something. What output does your compiler give you? If nothing, turn warnings on.

        int l, i;
        float x, P[l];
    use = for assignment. == is for comparison, which in this case does nothing.

    you have not initilized l before you declare an array of length l - this is a problem, as the array could have any length, whatever happens to be in memory at the time. Most likely this will be 0, but not always.

    Also note that saying P[1]=x is not going to assign the character x to that array position, it is going to assign whatever value happens to be in x to that array position. Since x is not initialized at that point either, your array is just going to contain garbage.

    Here is where you want ==. You are trying to compare two values for equality. What you are actually doing is setting l to be 0.

    If you really do want to use an array to store the legendre polynomials, I suggest you make an array of the coefficients of each power of x. So make a 2D array, of size M by M, where M-1 is the highest legendre polynomial you will need. Then store in position P[i][j] the coefficient of x^j of the ith lengedre polynomial.

    For example, if you wanted the polynomials up to P2, the declare P[3][3], and then the array would be


    Then you can just read off the array to get your polynomial, ie, P_2(x) = P[2][0]+P[2][1]*x+P[2][2]*x^2 = -(1/2)+(3/2)x^2

    What you need is a way to extract the coefficients from the formula you have. I'll leave that to you.
    Last edited by KBriggs; 10-29-2010 at 05:33 PM.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    United Kingdom
    First of all, I just want to say how bad your code is. lol. To be completely honest, you are lucky that your code even compiles, let alone just disappears when you try and run it!

    Here are a few problems:

    1) You declare an array with a variable you haven't assigned so the size of the array could be anything.

    2) You declare:
    float x;
    and then say:
    P[1] = x;
    even though you have absolutely no idea what value the variable x is. You get the user to input the value of x, but only after you have assign an unknown variable to P[1] (assuming ofcourse that P[1] even exists because of problem (1) ).

    3) You use ' %n ' in your scanf statement when ' %d ' would be far more appropriate.

    4) Your method of working out the Legendre Polynomial of degree 'l' is wrong. For one, the formula you are using is wrong (assuming you mean these Legendre Polynomials). If you try working out the l = 2 case using your formula you get: 2.5*x^2 - 0.5 when you should get 1.5*x^2 - 0.5. Since the problem is with the formula you are using this error would then be present in higher polynomials too. Combine that with problem 2, your function has more in common with a random number generator than a Legendre Polynomial Calculator.

    5) Not so much an error, but an observation. The way you have indented your code and placed your curly brace pairs makes it difficult to see the flow of your program. In particular I am refering to your 'else if' statement and 'for' loop.

    6) At the beginning of the program you ask the user for the value of x at which you want to evaluate the Legendre Polynomial, which would imply that you are not interested in saving all the polynomials to degree 'l', but rather are just interested in a single number. Oddly, you then calculate the polynomial evaluated at point 'x' and save it in array P at position 'l'. If you are only interested in the value of the polynomial why not store it as a float or alternative in P[ 2 ]? (i.e. you only need an array of three floats, not an array storing l floats).

    Briggs has already mentioned the stuff about the assignment operator so I won't talk about that.

    Lastly, you do realise that in the case of l = 1, your program will just print out a statement saying "The Legendre Polynomial is x" rather than an actual number, right? Just seems a bit odd that you would consider calculating a proper value for all other cases except that one, but oh well.

  4. #4
    Registered User Aoife :)'s Avatar
    Join Date
    Oct 2010
    Thank you for your help! I'm sorry for posting such nebulous things, but it's because I have to learn this on the go, without a course for programming.
    After I've corrected the basic syntax stuff I've messed up, defined the arrays normally, the program started working! So thank you both again for the remarks

    Btw, I wasn't clear enough about the main formula, I used the Rodrigues formula, so it's okay.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me with ma c programme..
    By eddie19 in forum C Programming
    Replies: 19
    Last Post: 08-12-2009, 07:53 AM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. Polynomials with linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-26-2001, 11:10 AM