How to improve bessel function speed

10 views (last 30 days)
kuy
kuy on 3 Aug 2016
Answered: Anurag Ojha on 12 Jun 2024
Hi, im writing a programm for a fitting routine and am currently optimizing the code for faster calculations. The weakes point is a part, where i have to calculate a big amount of bessel functions, which takes around 0.7 s. In my case q has 177 entries, th 100 and R 400.
Js = zeros(numel(th),numel(q)); tR=sin(th')*R;
for k = 1:numel(q)
Js(:,k) = sum(tn.*besselj(0,q(k)*tR),2);
end
I also tried to make a 3D-Matrix, but it takes slightly longer to calculate.
[Q,T,RR]=meshgrid(q,sin(th),R);
Js1 = besselj(0,Q.*T.*RR);
So, i'm wondering, is there a way to calculate these besselfunctions faster? thanks in advance, Moritz

Answers (1)

Anurag Ojha
Anurag Ojha on 12 Jun 2024
Hi Kuy
To calculate Bessel functions faster in MATLAB. You can use the besseli function instead of besselj to calculate the modified Bessel functions of the first kind. The besseli function is generally faster than besselj for large arguments.
The reason why besseli is faster than besselj for large arguments is because besseli calculates the modified Bessel functions of the first kind, which have a different mathematical formulation compared to the Bessel functions of the first kind calculated by besselj. The modified Bessel functions have a different asymptotic behavior for large arguments, which allows for more efficient computation.
Here's an example of how you can modify your code to use besseli instead:
Js = zeros(numel(th), numel(q));
tR = sin(th') * R;
for k = 1:numel(q)
Js(:, k) = sum(tn .* besseli(0, q(k) * tR), 2);
end
Adding MATLAB documentation for your reference:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!