Hermite Polynomial – C PROGRAM

In this post I’m gonna show you how to calculate Hermite polynomials using three different techniques: using recurrence relations, series representations, and numerical integration.
The programs will calculate and plot the first few Hermite polynomials.

Using Recurrence Relation

We will be using the following recurrence relation:
H_{n+1}(x)=2x H_n (x) - 2n H_{n-1}(x)
We would need two more relations, that is the relations for 0th and 1st order Legendre polynomials:
We will create a program that calculates the values of the Legendre polynomial at various x values and for different l and store these values in a txt file. Then just plot it using Gnuplot.
We will create two functions called ‘h0’ and ‘h1’, that contain the definition of respectively.
Then we will create a function ‘hn’ that will use the first two functions and recursion to find the value of Legendre polynomial for different x,n.
NOTE: I am using a slightly modified form of the recurrence relation. To get the form I am using, just replace n by n-1.



double h0(double x){
	return 1;

double h1(double x){
	return 2*x;
//The following is a general function that returns the value of the Hermite Polynomial for any given x and n=0,1,2,3,...
double hn(double x,int n){
		return h0(x);
	else if(n==1){
		return h1(x);
		return 2*x*hn(x,n-1)-2*(n-1)*hn(x,n-2); 	
	//We will create a data-file and store the values of first few Hermite polynomials for -1<x<5
	//create data-file
	double x;
	//write the values of first 5 Hermite  polynomials to data-file


The above program will create a data-file called legendre1.txt and store the values of the first 5 Hermite polynomials for -2\leq x \leq 2 . Now, you can just open the file and select the data and plot it using Excel, GnuPlot, Origin, etc.
For GnuPlot, the command is:

First few Hermite polynomials using recurrence relation

Using Series Representation

Using Numerical Integration



PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT and Machine Learning related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

One thought on “Hermite Polynomial – C PROGRAM

  1. can you please tell me how can we plot the coherent state wavefunctions or probabilities using the above given function? I am not getting a proper Gaussian curve for the probability density of harmonic oscillator coherent states.

Leave a Reply

Your email address will not be published. Required fields are marked *