dobessel
Overview
dobessel (code/tests/dobessel.c) is a standalone numerical test utility that computes the cylindrical Bessel function of the first kind $J_n(x)$ of integer order $n$ across the domain $x \in [0.0, 10.0]$ in increments of $\Delta x = 0.1$ using an iterative power-series ratio expansion. It prompts the user interactively for the integer order $n$ and convergence tolerance $\epsilon$ (eps), and writes the resulting $(x, J_n(x))$ curve to besselSeriesPlotn0.txt.
In the OMG codebase, Bessel functions are fundamental to both circular planar transducer directivity modeling ($J_1(x)$ in mkbp and bpdemo) and spherical elastic target strength modeling ($j_\ell(x) = \sqrt{\frac{\pi}{2x}} J_{\ell+1/2}(x)$ in testSphereTS).
Usage & Interactive Inputs
dobessel
The program takes no command-line flags and prompts on stdin for two parameters:
Enter the value of n:(int n) â Non-negative integer order of the Bessel function $J_n(x)$ (e.g.,0for $J_0(x)$ or1for $J_1(x)$).-
Enter the desired accuracy:(double eps) â Relative convergence threshold $\left\frac{t_k}{\text{sum}}\right \le \epsilon$ used to terminate the infinite series at each $x$ step (e.g., 1e-8).
Example Session
dobessel <<EOF
0
0.000001
EOF
This writes 101 tab-delimited lines (%lf\t%lf\n) to besselSeriesPlotn0.txt in the current working directory.
Mathematical Algorithm
The cylindrical Bessel function of the first kind of integer order $n$ is defined by the Frobenius power series:
\[J_n(x) = \left(\frac{x}{2}\right)^n \sum_{k=0}^{\infty} \frac{(-1)^k}{k!\,(n+k)!} \left(\frac{x}{2}\right)^{2k}\]Rather than re-evaluating factorials and high powers of $x$ at every iteration $k$, dobessel factors out $(x/2)^n$ and computes the inner sum recurrence using the exact term-to-term ratio $R_k$:
- Initialization ($k = 0$): \(t_0 = \frac{1}{n!}, \qquad S_0 = t_0\) where $n!$ is computed by
double factorial(int n). - Iterative Recurrence ($k = 1, 2, \dots$): \(R_k = \frac{t_k}{t_{k-1}} = -\frac{x^2 / 4}{k(n + k)}, \qquad t_k = R_k \, t_{k-1}, \qquad S_k = S_{k-1} + t_k\)
- Convergence Check: The loop continues while $\left|\frac{t_k}{S_k}\right| > \epsilon$.
- Final Scaling: Once converged, the sum is multiplied by $(x/2)^n$: \(J_n(x) = S_k \cdot \left(\frac{x}{2}\right)^n\) and written as
x \t J_n(x)tobesselSeriesPlotn0.txt.
Output File Format
-
besselSeriesPlotn0.txt: Two-column tab-separated ASCII file containing 101 rows for $x = 0.0, 0.1, 0.2, \dots, 10.0$:0.000000 1.000000 0.100000 0.997502 0.200000 0.990025 ...