DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

BesselFunc.cpp

02.01.2008
| 1000 views |
  • submit to reddit
        #include <iostream>
#include <iomanip>

using namespace std;

float zero (float x, int n)
{
 if (n==0)
   return 1;
 else
   return x*zero(x,n-1)/2/n;
}

float term(float x, int n)
{
      if (n==0)
        return 1;
      else
        return -x*term(x,n-1);
}

float bsum (int n, float x, int k, float a)
{
 float w;
 w = -x*x/4/(k+1)/(n+k+1);
 a *= w;
 if (fabs(a) < 0.0000001)
   return 1;
 else
   return (1 + w*bsum (n, x, k+1, a));
}

float bessel (int n, float x)
{
 return bsum (n, x, 0, 1)*zero(x,n);
}

void main (void)
{
 int n;
 float x;
 for (x=0; x<10; x++)
 {
   for (n=0; n<7; n++)
     cout << setiosflags(ios::fixed) << setprecision(4) << setw(10) << bessel (n, x);
   cout << endl;
 }
}