# NIntegrate Issue: Changing integration limits increases calculation time

Posted 9 years ago
3335 Views
|
|
1 Total Likes
|
 After a long time developing some code in mathematica and finally getting it to work, I have unfortunately encountered an odd problem: When I change the limits of integration (and shift the function that I am integrating) NIntegrate takes extremely long (on the order of hours) to complete. Now, this would be understandable if I increased the limits of integration but in my case, its the opposite I am decreasing the limits of integration. The code is shown below but also attached for your convenience.Given the following constants and functions: sL[phi_] := (r*phi^3)/24 lambdaB[z_] := 1/(sigmaSbeg*Sqrt[2 Pi]) Exp[-(z^2/(2*sigmaSbeg^2))] lambdaE[z_] := 1/(sigmaSend*Sqrt[2 Pi]) Exp[-(z^2/(2*sigmaSend^2))] lambdaC[z_, phi_] := 1/(((sigmaSend - sigmaSbeg)/theta *phi + sigmaSbeg)*Sqrt[2 Pi])Exp[-(z^2/( 2*((sigmaSend - sigmaSbeg)/theta *phi + sigmaSbeg)^2))] deEntrance[s_, phi_?NumericQ] := With[{sL = sL[phi]}, ((lambdaC[s - sL, phi] - lambdaB[s - 4 sL])/sL^(1/3) + NIntegrate[(1/(s - sprime)^(1/3)* D[lambdaC[sprime, phi], sprime]), {sprime, s - sL, s}])] deEntranceRMS[phi_?NumericQ] := Sqrt[NIntegrate[ lambdaE[s]*(deEntrance[s, phi])^2, {s, -5 sigmaSend, 5 sigmaSend}] - (NIntegrate[ lambdaE[s]*(deEntrance[s, phi]), {s, -5 sigmaSend, 5 sigmaSend}])^2] NIntegrate[(eta1[r*phi])*deEntranceRMS[phi], {phi, 0.0,theta}] NIntegrate[(eta2[r*phi])*deEntranceRMS[phi], {phi, 0.0, theta}] The following works well and outputs an answer within a reasonable amount of time (few minutes): sigmaSbeg = 2.759153*10^-004; sigmaSend = 2.774392*10^-004; theta=0.06; r=9.15445; eta1[s_]:=-0.05461846 s^2 eta2[s_]:=-0.10923649 s But, for the following, Mathematica takes extraordinarily long (an hour or two). Clear[sigmaSbeg,sigmaSend,theta,r,eta1,eta2] sigmaSbeg = 8.280940*10^-006; sigmaSend = 8.308940*10^-006; theta=0.014; r=17.857143; eta1[s_]:=0.00059452375 + 0.028 (-0.25 + s)^2 eta2[s_]:=-0.014 + 0.056 s I cannot figure out why. I assume my eta1 and eta2 functions are a little more complex, but they're only shifted quadratics and linear functions. Any help would be extremely appreciated. At this point I am thinking of implementing another code :(. Attachments:
 When a Method isn't specified for NIntegrate, it's hard to guess what it might be doing. Yes, for simple numerical methods decreasing the range of integration is always going to make it run faster. If you used Method -> "TrapezoidalRule", then I would expect the behavior your mention. That's not the case for more complicated methods. NIntegrate also is likely doing some symbolic preprocessing. The first thing I'd try is to set the NIntegrate option SymbolicProcessing to 0. This prevents Mathematica from manipulating the integral symbolically. By default, NIntegrate symbolically preprocesses integrands. This allows NIntegrate to evaluate more difficult integrals. However, the symbolic processing slows some calculations. To numerically integrate a function called f from 0 to 1 without symbolic processing, evaluate:  NIntegrate[f[x], {x, 0, 1}, Method -> {Automatic, "SymbolicProcessing" -> 0}] You can replace Automatic with any other method specification that you would like to use.