1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Generate hermite polynomial coefficients

  1. Mar 8, 2012 #1
    1. The problem statement, all variables and given/known data

    I need to generate coefficients of hermite polynomials up to order k. Recursion will be used.

    2. Relevant equations

    a[n+1][k] = 2a[n][k-1] - 2na[n-1][k]

    3. The attempt at a solution

    Its not pretty, but here is my code.

    Code (Text):
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <math.h>
    using namespace std;

    int k_order;
    int NDMN;
    int x=0,y=0;

    int main()
    {      
           
        cout << "How many polynomials would you like to generate?";
        cin >> NDMN;
       
        float NARY[2][NDMN];
       
        while(y < (NDMN - 1))
        {
                while(x !=2)
                {
                        NARY[x][y] = 0;
                        x++;
                        }
                y++;
                x=0;
                }
        y = 0;
               
        NARY[0][0] = 1;
        NARY[0][1] = 0;
        NARY[1][1] = 2;

        while(y < (NDMN - 1))
        {
                while(x < 2)
                {
                        NARY[x][y+1] = 2 * NARY[x-1][y] - 2 * y * NARY[x][y-1];
                        x++;
                        }
                y++;
                x = 0;
                }
        y = 0;
        while(y < (NDMN - 1))
        {
                while(x !=2)
                {
                        cout << NARY[x][y] << " ";
                        x++;
                        }
                        cout<<endl;
                        x = 0;
                        y++;
                        }
        system("pause");
        return 0;
    }
               
     
    The cout keeps giving me numbers on the order of 10^-30. To be honest, I cannot even really figure out what should be done. We are using Computational Methods in Physics an Engineering by Samuel Wong. The program we are writing is Box 4-5 in chapter 4. Any hints or tips on where I should start going? Thanks!
     
  2. jcsd
  3. Mar 9, 2012 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    You initialize and assign elements in your NARY array with indices 0..upwards. But this line when executed with x=0 and y=0 calls upon elements NARY[-1][0] etc.
     
  4. Mar 11, 2012 #3
    Alright thanks for the heads up.

    In the box in our text, it claims we should use arguments K_Order - order of the hermite polynomial, and NDMN - dimension of the array NARY. I was wondering if anyone knew what the difference in this number would be. NDMN should either be the size of the order or should just be 2, so I am a little confused on what they mean by the two variables. Thanks
     
  5. Mar 11, 2012 #4
    Alright so I think I am getting closer. I figured that the k_order is the amount of times the recursion must be done. So if I want order 3, I have to do the recursion for order one, than order 2, than order 3, and output the array after doing order 3. So far, my code has been altered to this:

    Code (Text):
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <math.h>
    using namespace std;

    int k_order;
    int NDMN;
    int x=0,y=0;

    int main()
    {      
           
        cout << "What order polynomial would you like to generate?";
        cin >> k_order;
       
        NDMN = k_order;
       
        int NARY[NDMN][2];
       
        while(y < (NDMN - 1)) // This puts zeros in the array
        {
                while(x !=2)
                {
                        NARY[y][x] = 0;
                        x++;
                        }
                y++;
                x=0;
                }
                 
        NARY[0][0] = 1; // Initialize the first few coefficients
        NARY[0][1] = 0;
        NARY[1][1] = 2;
       
        int q = 0;
       
        while(q < k_order)
        {
                x = 0;
                y = 2;
                while(y < (NDMN - 1))
                {
                        while(x < 2)
                        {
                                NARY[y][x] = 2 * x * NARY[y-1][x] - 2 * (y-1) * NARY[y-2][x];
                                x++;
                                cout << NARY[2][0];
                                }
                        y++;
                        x = 0;
                        }
                q++;
                }
               
        y = 0;
        x = 0;
        while(y < (NDMN))
        {
                while(x !=2)
                {
                        cout << NARY[x][y] << " ";
                        x++;
                        }
                        cout<<endl;
                        x = 0;
                        y++;
                        }
        system("pause");
        return 0;
    }
               
     
    However, it doesnt seem to give the right values. The slot NARY[2][0] should be -2 after the first recursion, but even when I put cout << NARY[2][0] inside the x while loop the command is not printed. However, my x is less than 2, so it should be printed...
     
  6. Mar 11, 2012 #5
    Well I guess it actually does print the value. I am finding that when I get to higher order, the array should read:
    -2 0
    4 2

    but it actually reads
    0 -2
    2 4

    I have a feeling it just requires a switch of variables, but I am a little unsure...

    edit: I realized the variables I had in the output were incorrect. I switched them and it seems to fix it, though the recursion still isnt quite correct. I am going to print my code out and see if my professor has any ideas, but I am always listening to any ideas you guys have. Thanks!!
     
    Last edited: Mar 11, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Generate hermite polynomial coefficients
  1. Fourier coefficients (Replies: 9)

Loading...