Generate hermite polynomial coefficients

AI Thread Summary
The discussion focuses on generating Hermite polynomial coefficients using recursion, specifically addressing issues in the implementation of the code. The user initially struggles with incorrect outputs, particularly with coefficients being in the order of 10^-30, and identifies that the array indices may be incorrectly accessed. After some iterations, they clarify the roles of the variables k_order and NDMN, realizing that k_order indicates the recursion depth needed for generating the polynomials. Despite making adjustments to the code, the user continues to face challenges with the recursion logic and output values, seeking further advice from peers and the professor. The conversation highlights the complexities of implementing recursive algorithms in programming for mathematical computations.
khemist
Messages
247
Reaction score
0

Homework Statement



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

Homework Equations



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

The Attempt at a Solution



Its not pretty, but here is my code.

Code:
#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!
 
Physics news on Phys.org
NARY[x][y+1] = 2 * NARY[x-1][y] - 2 * y * NARY[x][y-1];
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.
 
NascentOxygen said:
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.

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
 
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:
#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 doesn't 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...
 
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 isn't 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:
Back
Top