In my recent posts I have showed you how to write C programs that calculate the sum of a finite as well as an infinite series.
Now, that we have discussed a few examples like the Sine, Cosine and the Bessel series, we can try some harder problems.
In this post we will discuss three more series.
Q. Evaluate given by
correct to four decimal places, for , at intervals of 0.1.
Solution.
The first term of the series is:
and the ratio of the consecutive terms is:
PROGRAM:
/***************************** ******SERIES PROBLEM********* ****************************/ #include<stdio.h> #include<math.h> main(){ FILE *fp=NULL; fp=fopen("seriesProblem4.txt","w"); double t0,t1,R,sum,z,eps; printf("Enter the desired accuracy: "); scanf("%lf",&eps); for(z=0;z<=1;z=z+0.1){ int i=1; //Initialize First Term t0=z; //Make sum equal to the first term sum=t0; do{ //Find the ratio of the second term to the first term using already known relation R=-(M_PI*M_PI*pow(z,4))/((4*i+1)); //Calculate the second term t1=R*t0; //find the new sum sum=sum+t1; t0=t1; i++; //keep on summing terms until the required accuracy is reached }while(fabs(t1/sum)>eps); sum=sum*cos(M_PI*z*z/2); fprintf(fp,"%lf\t%lf\n",z,sum); } }
OUTPUT:
When you run the above program, it will just ask you to enter the desired accuracy, create a file called 'seriesProblem4.txt'
containing the data-points in the given range of x. You can then plot this file using GnuPlot by giving the command:
->plot 'seriesProblem4.txt' w l
The output looks something like this:
Q. Write a program to plot the sum of the following series:
for and in the range . You would require the following relations:
Solution.
The first term of the series is:
and the ratio of the consecutive terms is:
PROGRAM:
/***************************** ******SERIES PROBLEM********* ****************************/ #include<stdio.h> #include<math.h> main(){ FILE *fp=NULL; fp=fopen("seriesProblem5.txt","w"); double t0,t1,R,sum,z,eps; printf("Enter the desired accuracy: "); scanf("%lf",&eps); for(z=0;z<=5;z=z+0.01){ int k=2; //Initialize First Term t0=1/(2*sqrt(M_PI)); //Make sum equal to the first term sum=t0; do{ //Find the ratio of the second term to the first term using already known relation R=(4*z*z*(3-k))/(2*k*(k-1)); //Calculate the second term t1=R*t0; //find the new sum sum=sum+t1; t0=t1; k=k+2; //keep on summing terms until the required accuracy is reached }while(fabs(t1/sum)>eps); fprintf(fp,"%lf\t%lf\n",z,sum); } }
OUTPUT:
When you run the above program, it will just ask you to enter the desired accuracy, create a file called 'seriesProblem5.txt'
containing the data-points in the given range of x. You can then plot this file using GnuPlot by giving the command:
->plot 'seriesProblem5.txt' w l
The output looks something like this:
Q. Write a program to plot the following function:
where , for in the range , at intervals of 0.05.
Solution.
The first term of the series is:
and the ratio of the consecutive terms is:
PROGRAM:
/***************************** ******SERIES PROBLEM********* ****************************/ #include<stdio.h> #include<math.h> main(){ FILE *fp=NULL; fp=fopen("seriesProblem6.txt","w"); double t0,t1,R,sum,z,eps; printf("Enter the desired accuracy: "); scanf("%lf",&eps); for(z=-10;z<=0;z=z+0.05){ int i=1; //Initialize First Term t0=1; //Make sum equal to the first term sum=t0; do{ //Find the ratio of the second term to the first term using already known relation R=pow(z,3)/((3*i)*(3*i-1)); //Calculate the second term t1=R*t0; //find the new sum sum=sum+t1; t0=t1; i++; //keep on summing terms until the required accuracy is reached }while(fabs(t1/sum)>eps); sum=sum*0.35503; fprintf(fp,"%lf\t%lf\n",z,sum); } }
OUTPUT:
When you run the above program, it will just ask you to enter the desired accuracy, create a file called 'seriesProblem6.txt'
containing the data-points in the given range of x. You can then plot this file using GnuPlot by giving the command:
->plot 'seriesProblem6.txt' w l
The output looks something like this:
Write a program to evaluate the sum up to 20 terms of the series
for a given x, and compare your result with the analytic sum of the series.
Solution:
PROGRAM:
/******************************** ******FINITE SERIES SUM********** Series: S(x) = 1 + (1/x^2) + (1/x^3) + ..... + (1/x^n) ********************************/ #include<stdio.h> #include<math.h> main(){ FILE *fp=NULL; fp=fopen("seriesProblem1.txt","w"); int i,n; printf("Enter the number of terms to be summed(n): "); scanf("%d",&n); double x,xmin,xmax; printf("Enter the the range of x:\nxmin = "); scanf("%lf",&xmin); printf("xmax = "); scanf("%lf",&xmax); for(x=xmin;x<=xmax;x=x+0.1){ /*Initialize t0 with the value of the first term of the series */ double t0=1/(x*x); double t1,R,sum=1+t0; for(i=1;i<n-1;i++){ R=(double)1/x; t1=R*t0; t0=t1; sum=sum+t1; } fprintf(fp,"%lf\t%lf\n",x,sum); } }
REFERENCES:
The above problems have been taken from the Computer Programming & Numerical Analysis Manual by Dr. Shobhit Mahajan.
Ph.D. 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 software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.