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.
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$Method
option settings. $\endgroup$