Mathematica: Speed up NIntegrate

In summary, the speaker is describing their problem with finding eigenvalues for a large matrix in Mathematica. They have simplified the problem to a 2x2 matrix and have tried to use the Compile function and specify precision for the numbers, but it did not help with the slowness. They are looking for a solution to speed up the calculation, and have found that removing the WorkingPrecision->13 option results in a faster solution.
  • #1
sirenayka
2
0
I have simplified my problem here, in my actual problem the matrix is much bigger which makes it impossible to find eigenvalues analytically. So, I chose standard BCS problem (2x2 matrix) to demonstrate my problem with Mathematica. But, with even this simple case, it takes forever to integrate. My code is the following:

hamiltonian[k_,mu_,delta_]:={{k^2- mu, delta},{delta,-k^2+ mu}}
eigens[k_,mu_,delta_]:=Eigenvalues[hamiltonian[k, mu, delta]]
fermitotal[beta_,k_,mu_,delta_]:=Block[{ee = eigens[k, mu, delta]},1/(1+Exp[beta ee[[1]]])+1/(1+Exp[beta ee[[2]]])]
nTotal[beta_,mu_,delta_]:=NIntegrate[fermitotal[beta, k, mu, delta] k^2,{k,0,20},Method->{"LocalAdaptive","SymbolicProcessing"->False},AccuracyGoal->4,PrecisionGoal->4,MinRecursion->10,MaxRecursion->300,WorkingPrecision->13]
nTotal[50,1,1/10]//AbsoluteTiming

Any idea how to speed up the calculation?

I have tried, using Compile for the hamiltonian matrix. Specifying the precision for the numbers,

nTotal[N[50,30],N[1,30],N[1/10,30]]//AbsoluteTiming

Did not help. This calculation is supposed to be fast, but I am completely stuck here and I don't know what causes this slowness. Any help would be very much appreciated. Thank you.
 
Physics news on Phys.org
  • #2
It's the WorkingPrecision->13 that is causing a slowdown. If you remove it, you'll get a solution quite fast.
 

1. How can I speed up the NIntegrate function in Mathematica?

There are a few different ways to potentially speed up the NIntegrate function in Mathematica. One option is to use the "LocalAdaptive" method, which can sometimes be faster than the default "GlobalAdaptive" method. Another option is to use the "ParallelTable" command to evaluate the function over multiple processors simultaneously. Additionally, you can try specifying the option "Method -> "ClenshawCurtisRule" which may be more efficient for certain types of integrands.

2. What is the best way to handle large or complex integrals in Mathematica?

For large or complex integrals, it may be helpful to first break the integral into smaller pieces and then use the "NIntegrate" function with the "Method -> "MultiPeriodic" option. This can help improve the accuracy and speed of the integration. Additionally, using the "Method -> "GlobalAdaptive" option may also be beneficial for handling more complicated integrands.

3. Is there a way to speed up NIntegrate for functions with singularities?

Yes, there are a few strategies you can try to speed up NIntegrate for functions with singularities. One approach is to use the "Exclusions" option to specify the location of the singularity, which can help improve the accuracy and efficiency of the integration. Another option is to use the "Method -> "DoubleExponential" option, which is specifically designed for integrals with singularities and can often be faster than the default method.

4. Can changing the integration limits affect the speed of NIntegrate?

Yes, changing the integration limits can have an impact on the speed of NIntegrate. In general, it is recommended to choose integration limits that are as small as possible while still encompassing the desired area. This can help reduce the computational time and improve the accuracy of the integration. Additionally, using the "Method -> "GaussKronrodRule" option may also be beneficial for certain types of integration limits.

5. Are there any general tips for improving the speed of NIntegrate in Mathematica?

Yes, there are a few general tips for improving the speed of NIntegrate in Mathematica. One is to use the "Compiled" function to compile the integrand, which can significantly speed up the integration process. Another tip is to use the "PrecisionGoal" and "AccuracyGoal" options to specify the desired level of precision and accuracy, which can help reduce the computational time. Additionally, using the "Method -> "TrapezoidalRule" option may also be beneficial for certain types of integrals.

Back
Top