Bessel Function Series – C PROGRAM

In this post we will write a C program to calculate the Bessel Functions of the first kind.

We will use the series representation of Bessel functions, and use the concept of previous posts to calculate the sum of infinite series, and thus find the values.

The series is given as:

As you can notice, we would need to calculate factorials for the series sum evaluation.
We already wrote a program for that here.
So we will just use the function to calculate and return the factorial.

If you haven’t already checked out my past posts on Series Summation then you can read the following articles, before you move on with this post.
Sine Series(Finite) – C Program
Sine Series(Infinite) – C Program
Cosine Series(Infinite) – C Program

If we remove the \left(\frac{x}{2}\right)^n of the definition given above, then we get the series:

So we can simply calculate the sum of this series and then multiply the result by \left(\frac{x}{2}\right)^n , to get the value of the Bessel Function.

The first term of the new series is:

and the ratio of consecutive terms is:
Note: k starts from 1 for our ratio to work.

Now, that you have the above information, the Bessel Function for a given value of n and, correct upto a certain number of decimal places can be easily calculated. These values can be written down to a file and then they can be plotted using a software like Gnuplot.


 ******BESSEL FUNCTION********
double factorial(int n){
  int i;
  double fact=1;
  return fact;
	double t0,t1,R,sum,x,eps;
	int n;
	printf("Enter the value of n: ");
	printf("Enter the desired accuracy: ");
		int k=1;
		//Initialize First Term
		//Make sum equal to the first term
			//Find the ratio of the second term to the first term using already known relation
			//Calculate the second term
			//find the new sum
			//keep on summing terms until the required accuracy is reached

The above program will prompt the user to enter the value of n and the accuracy desired, and then it will write down the data-points to the text file 'besselSeriesPlotn0.txt'.
Then this file can be plotted using Gnuplot command:
->plot 'besselSeriesPlotn0.txt' w l
Programmer, can change the name of the file to be written for different values of n and then plot and see the results.


The following are the output results for a few sample runs of the program:

for n=0:

Gnuplot Output:

for n=1:

Gnuplot Output:

for n=2:

Gnuplot Output:

In the following image, I have plotted many Bessel functions simultaneously, by compiling the outputs for different n :

Hope, you found this post helpful.
If you have any questions/doubts leave a comment down below.


Sine Series(Finite) – C Program
Sine Series(Infinite) – C Program
Cosine Series(Infinite) – C Program
Problems on Series Sum – C


[wpedon id="7041" align="center"]

4 thoughts on “Bessel Function Series – C PROGRAM

  1. what’s the command to plot using different colors??
    btw,thankyou very much.was very helpful 😀

  2. sir, I want to know about Inokutti-Hirayama model (IH) fitting for non-exponential curve.

  3. Hii, this code does not work for higher values ok x (> 500) if we increase x then J should converge but it goes to infinite instead, can you explain why?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.