Plotting the Chebyshev Polynomials of the Second Kind using C and Gnuplot

In this post I will show you how to calculate and plot the Chebyshev Polynomials of the second kind using a C Program and Gnuplot.

We will be using the following information:
and the recurrence relation:
U_{n+1}(x)=2xU_n(x)-U_{n-1}(x) where, n starts from 1.

Modifying the recurrence relation a little, so that n starts from 2, we get:

We will create a program that calculates the values of the Chebyshev Polynomials at various x values and for different n and store these values in a txt file. Then just plot it using Gnuplot.
We will create two functions called ‘U0’ and ‘U1’, that contain the definition of respectively.
Then we will create a function ‘Un’ that will use the first two functions and recursion to find the value of Chebyshev Polynomials for different x,n.


***********Chebyshev Polynomials***********
double U0(double x){
	return 1;
double U1(double x){
	return 2*x;
//General form of Chebyshev polynomial of second for a given value of n and x
double Un(double x, int n){
		return U0(x);
	else if(n==1){
		return U1(x);
		//using the recurrence relation
		return 2*x*Un(x,n-1)-Un(x,n-2);
	double x;
	//Write down the values to a file

When you run the above C, it will generate a file called ‘cheby.txt’ which would contain 6 columns of data-points.
The first column contains the ‘x’ values and the rest of them are for U0(x), U1(x), U2(x)...
These can be easily plotted using Gnuplot by using the following commands:

GnuPlot Command:

->set xlabel 'x'
->plot 'cheby.txt' u 1:2 w l t "U0(x)", '' u 1:3 w l t "U1(x)", '' u 1:4 w l t "U2(x)", '' u 1:5 w l t "U3(x)", '' u 1:6 w l t "U4(x)"



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"]

Leave a Reply

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