I have a problem involved spherical Bessel functions of order 0. I wrote my own spherical Bessel function:
function js = sphbesselj(nu,x)
js = sqrt(pi ./(2* x)) .* besselj(nu + 0.5, x);
end
which seems to agree with Mathematicas inbuilt one for all my test cases. The problem is at nu
or x =0
. Mathematica correctly returns 1, but my MATLAB scrip returns NaN. How could I correct my code, so that if I feed in an array of say x = 0:1:5
I get the 1 for my first value, instead of
>> sphbesselj(0,x)
ans =
NaN 0.8415 0.4546 0.0470 -0.1892 -0.1918
Is my custom function the best way to go about this?
Thanks
In fact, floating point values of
x
close to0
also returnNan
. You actually have three cases to possibly worry about. x = ±∞ results inNaN
as well. Here's a vectorized function that handles those:A useful resource when developing such functions is http://functions.wolfram.com. The page on the spherical Bessel function of the first kind has many useful relationships.