Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have attached below a piece of code that has been running successfully but takes extremely long for mathematica to compute. I believe it is the last "rms" calculation and the fact that I am using very small numbers that is really pushing mathematica. Any suggestions or help would be greatly appreciated.

c = 3*10^8;
e = 1.6*10^-19;
m = 9.1*10^-31;
rc = 2.8*10^-15;
gamma = 1600/0.511;
n = (10^-10)/e;
sbeg = 9.6*10^-5;
send = 1.1*10^-5;
lm = 0.5;
theta = 0.04;
r = lm/theta;

sL[phi_?NumericQ] := (r*phi^3)/24

lambda[z_, phi_] := n/(((send - sbeg)/theta phi + sbeg)*Sqrt[2 Pi])
   Exp[-(z^2/(2*((send - sbeg)/theta phi + sbeg)^2))]

de[s_, phi_?NumericQ] :=  With[{sL = sL[phi]}, 
(-((2*n*rc*m*c^2)/(3^(1/3) r^(2/3)))) 
*((lambda[s - sL, phi] - lambda[s - 4 sL, phi])/sL^(1/3) 
 + NIntegrate[(1/(s - sprime)^(1/3)*D[lambda[sprime, phi], sprime]),
       {sprime, s - sL,s}])]

total[s_?NumericQ] := NIntegrate[de[s, phi], {phi, 0, theta}]

rms = Sqrt[
    1/(sbeg*Sqrt[2 Pi])
      Exp[-(s^2/(2*(sbeg)^2))]*(total[s])^2, {s, -10 sbeg, 
     10 sbeg}] - (NIntegrate[
     1/(sbeg*Sqrt[2 Pi])
       Exp[-(s^2/(2*(sbeg)^2))]*(total[s]), {s, -10 sbeg, 
      10 sbeg}])^2]
share|improve this question
The basic reason for the slowness is probably that you have NIntegrate calls nested three deep. Each NIntegrate might evaluate its integrand several hundred times, which means the interior integrands might be evaluated a million up to tens of millions of times. – Michael E2 Jun 6 '14 at 16:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.