Generate hermite polynomial coefficients

Click For Summary
SUMMARY

The discussion focuses on generating Hermite polynomial coefficients using recursion in C++. The user initially struggles with incorrect outputs due to array indexing errors and misunderstanding the relationship between the order of the polynomial (k_order) and the dimension of the array (NDMN). After several iterations and code adjustments, the user identifies issues with variable assignments and recursion logic, leading to incorrect coefficient values. The final goal is to correctly implement the recursion formula: a[n+1][k] = 2a[n][k-1] - 2na[n-1][k].

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of recursion and its application in algorithms
  • Familiarity with Hermite polynomials and their properties
  • Basic knowledge of array manipulation in C++
NEXT STEPS
  • Review the recursion concept in C++ to ensure proper implementation
  • Study the mathematical properties of Hermite polynomials for better understanding
  • Learn about debugging techniques in C++ to identify and fix logical errors
  • Explore advanced array handling techniques in C++ for more efficient code
USEFUL FOR

Students and professionals in computational physics, software developers working with mathematical algorithms, and anyone interested in implementing recursive functions in C++.

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:

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