/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // bessel.c: Spherical Bessel functions via up and down recursion #include #include #define xmax 40.0 // max of x #define xmin 0.25 // min of x >0 #define step 0.1 // delta x #define order 10 // order of Bessel function #define start 50 // used for downward algorithm main() { double x; double down(double x, int n, int m); // downward algorithm double up(double x, int n); // upward algorithm FILE *out; out = fopen("bessel.dat","w"); for (x=xmin; x<=xmax; x+=step) fprintf(out, "%f\t%f\t%f\n", x, down(x,order,start), up(x,order)); printf("data stored in bessel.dat.\n"); fclose(out); } double down (double x, int n, int m) { // downward recursion double scale, j[start+2]; int k; j[m+1] = j[m] = 1.; // start with "something" for (k=m; k>0 ; k--) j[k-1] = ((2.*(double)k + 1.)/x)*j[k] - j[k+1]; // recur scale = ((sin(x))/x)/j[0]; // scale the result return(j[n]*scale); } double up (double x, int n) { // function using upward recursion double one, two, thr; int k; one = (sin(x))/x; // start with lowest order two = (sin(x) - x*cos(x))/(x*x); // loop for order of function for (k = 1 ; k