3
$\begingroup$

Consider this numerical integration of a Bessel function:

Do[NIntegrate[BesselJ[2, x], {x, 0, 10000}], {i, 1, 100}] // AbsoluteTiming

{4.033403, Null}

This is the similar code in Matlab:

clc
clear all;
f = @(x) besselj(2,x);

tic
for i=1:100
integral(f,0,10000);
end
toc

Elapsed time is 0.860757 seconds.

ans: 0.9964

How can I make the numerical integration in Mathematica as fast as Matlab? I want to do some minimization which involves the numerical integration of a complicated function of Bessel functions and its zeros. Mathematica calculates the zeros and the integrate slower than Matlab. I think this considerably will affect the computation speed because if Matlab is fast about 1 second then the many times of evaluation of these Bessel zeros and integrals accumulate a lot of time.

Could it be that the speed difference is due to being for faster in Matlab than Do in Mathematica?

But if someone calculates just one integral and use BesselJ[200,x] then there is a difference, or if Someone uses:

NIntegrate[BesselJ[200, x] + Sin[x], {x, 0, 30000}] // AbsoluteTiming

{2.340000, 2.593083412014634}

clc
clear all;
f = @(x)  besselj(200,x)+sin(x);
tic
integral(f,0,30000)
toc

Elapsed time is 0.264900 seconds.

ans :2.5931

I use Mathematica 9 and Matlab 2014a on Windows7.

$\endgroup$
8
  • 1
    $\begingroup$ If one omits the "Do", via NIntegrate[BesselJ[2, x], {x, 0, #}] & /@ ConstantArray[10000, 100] it is not really faster, so I do not think it is the "Do"-Loop what makes it slow. $\endgroup$
    – mgamer
    Commented Mar 13, 2015 at 10:34
  • $\begingroup$ do they get the same result? $\endgroup$
    – george2079
    Commented Mar 14, 2015 at 3:24
  • $\begingroup$ @george2079 Yes, they do. $\endgroup$
    – MOON
    Commented Mar 14, 2015 at 21:37
  • $\begingroup$ I wonder what's the output of matlab? Is it still reliable? $\endgroup$
    – xzczd
    Commented Mar 18, 2015 at 11:31
  • 1
    $\begingroup$ This speed might be influenced by Method option settings. $\endgroup$ Commented Mar 18, 2015 at 15:14

2 Answers 2

4
$\begingroup$
Parallelize[
  Do[NIntegrate[BesselJ[2, x], {x, 0, 10000}], {i, 1, 100}]
  ] // AbsoluteTiming

Mathematica graphics

on same PC

clc
clear all;
f = @(x) besselj(2,x);

tic
for i=1:100
integral(f,0,10000);
end
toc

 %Elapsed time is 0.924171 seconds.

Just to note, tic/toc and AbsoluteTiming measure elapsed time, not cpu time. On mutlicore, it is possible that elapsed time is smaller than CPU time. Using CPU time, the result is

Timing[Do[NIntegrate[BesselJ[2, x], {x, 0, 10000}], {i, 1, 100}]]

Mathematica graphics

While Matlab 2015a

clc
clear all;
f = @(x) besselj(2,x);

t=cputime; 
for i=1:100
integral(f,0,10000);
end
cputime-t

%ans =
   %5.5848

So Mathematica actually used less total CPU time than Matlab. But its elapsed time was larger. It looks like Matlab used more cores (I have 8 cores). So by forcing Parallelize, Mathematica now used cores more efficiently than otherwise. That what it appears what happened, but I am not an expert on this so I could be wrong.

$\endgroup$
5
  • $\begingroup$ so are you saying that matlab parallelisms for you? $\endgroup$
    – chris
    Commented Mar 13, 2015 at 10:39
  • $\begingroup$ What if you would use parallelization for Matlab too? I think both cases must be either parallel or not parallel. The other thing is that if someone calculates just one integral and use BesselJ[200,x] then there is a lot of difference. $\endgroup$
    – MOON
    Commented Mar 13, 2015 at 10:39
  • $\begingroup$ On my system if I use parallelization for Matlab, then it is faster. $\endgroup$
    – MOON
    Commented Mar 13, 2015 at 20:06
  • $\begingroup$ @Nasser Could you use parallelization for Matlab too? I think the answer is not that Matlab uses more cores. If you use parallelization for Matlab and get the same performance as Mathematica then you're right. $\endgroup$
    – MOON
    Commented Mar 17, 2015 at 13:43
  • $\begingroup$ @yashar Have you checked your task manager? If matlab doesn't use multi-core then the CPU utilization rate should be quite low. $\endgroup$
    – xzczd
    Commented Mar 18, 2015 at 12:01
2
$\begingroup$

According to Shampine, Vectorized adaptive quadrature in MATLAB, J. Comp. Appl. Math. (2008), MATLAB makes vectorized calls to the integrand, which supports @Nasser's inference.

Also, Bessel J on single reals seems faster on MATLAB R2023a than on Mma V14.0.0 Mac ARM:

Do[BesselJ[2, i], {i, 1., 100000.}] // AbsoluteTiming
(* {0.244902, Null} *)

tic
for i=1:100000
besselj(2,i);
end
toc
Elapsed time is 0.065162 seconds.

I think NIntegrate chooses the Levin Rule for this integral, which uses much fewer function calls but has considerable processing overhead. (The Levin Rule evaluates the integrand 953 times, Gauss-Kronrod 83395 times; MATLAB 59520 times. One can reduce the integrand evaluations in GK to 12000+ using 101 Gauss points, but it's still not competitive with the Levin Rule or MATLAB for speed.)

In sum, it seems to be a combination of vectorization as @Nasser observed and a somewhat faster implementation of the Bessel $J$ function in MATLAB. The default methods are different, which makes a head-to-head comparison a little awkward.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.