Generate hermite polynomial coefficients

Click For Summary

Discussion Overview

The discussion revolves around generating coefficients of Hermite polynomials up to a specified order using recursion. Participants are sharing their code implementations and seeking assistance with debugging and understanding the recursion process.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code and expresses confusion over the output, specifically mentioning that the results are unexpectedly small (on the order of 10^-30).
  • Another participant points out a potential issue in the code where negative indices may be accessed, suggesting that the initialization of the array might be problematic.
  • A participant reflects on the difference between the variables k_order and NDMN, questioning their intended use in the context of the problem.
  • One participant theorizes that k_order represents the number of recursion steps needed to generate the polynomial coefficients and modifies their code accordingly.
  • Another participant notes that their output does not match expected values and suspects that variable assignments may need to be adjusted.
  • One participant mentions that they have made corrections to their output variables, but the recursion still appears to be incorrect and seeks further input from peers.

Areas of Agreement / Disagreement

Participants express various uncertainties regarding the implementation details and the behavior of their code. There is no consensus on the correct approach or final output values, and multiple competing views on the recursion process and variable usage remain unresolved.

Contextual Notes

Participants are working with specific equations and recursion methods as outlined in a textbook, but there are limitations in their understanding of the relationships between the variables and the initialization of the array.

khemist
Messages
248
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K