MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Nested numerical integral - speed: Is it suppose to be so slow?

  • To: mathgroup at
  • Subject: [mg127650] Re: Nested numerical integral - speed: Is it suppose to be so slow?
  • From: Alexei Boulbitch <Alexei.Boulbitch at>
  • Date: Sat, 11 Aug 2012 04:31:12 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:


Newbie here, working with Mathematica 8 on a Windows 7 64-bit system, Intel core i7-2600 @3.4Ghz and 16 GB of RAM. I'm doing a relatively simple double numerical integration of a function composed of trapezoids, but was surprised to see that the computation time was relatively high. The calculation that I need takes about 13 seconds as you can see below, and I need to evaluate it over many different parameters. Is there a way to significantly increase the speed of this execution?


In[19]:= trap[t_,t1_,t2_,a_]:=Piecewise[{{0,t<0||t>=t1+t2},{a t,t>=
0&&t<t1},{a t1,t>=t1&&t<t2},{a (-t+t1+t2),t>=t2&&t<t1+t2}}]
In[20]:= Ga[t_,t1_,t2_,a_,\[CapitalDelta]_]:=trap[t,t1,t2,a]-trap[t-2 \[CapitalDelta],t1,t2,a]
In[21]:= Fa[t_?NumericQ,g_,a_,\[CapitalDelta]_,\[Delta]_]:=NIntegrate[Ga[u,g/a,\[Delta]-g/a,a,\[CapitalDelta]],{u,0,t}]
In[22]:= ba[g_,a_,\[CapitalDelta]_,\[Delta]_]:=(2.675222/10)^2 NIntegrate[Fa[x,g,a,\[CapitalDelta],\[Delta]]^2,{x,0,\[Delta]+2 \[CapitalDelta]}]
In[23]:= Timing[ba[5.5,100000,12,0.25]]
Out[23]= {13.150999999999996,3.234694880849849}


Hi, Sune,

Judging from messages I guess, there is a kind of divergence somewhere in the integration domain or something resembling. It can be coped with by fixing the working precision. What else one can do is to parallelize the process. Just as  an idea:

m = 4;
f[g_, a_, \[CapitalDelta]_, \[Delta]_] :=
  Module[{v, u, aa, gg, t, t1, t2, deltaL, delta},
   trap[t_, t1_, t2_, aa_] :=
    Piecewise[{{0, t < 0 || t >= t1 + t2}, {aa t,
       t >= 0 && t < t1}, {aa t1,
       t >= t1 && t < t2}, {a (-t + t1 + t2),
       t >= t2 && t < t1 + t2}}];
   Ga[t_, t1_, t2_, aa_, deltaL_] :=
    trap[t, t1, t2, aa] - trap[t - 2 deltaL, t1, t2, aa];
   Fa[t_?NumericQ, gg_, aa_, deltaL_, delta_] :=
    NIntegrate[Ga[u, gg/aa, delta - gg/aa, aa, deltaL], {u, 0, t},
     WorkingPrecision -> 5];
       Fa[x, gg, aa, deltaL, delta]^2 /. {gg -> g, aa -> a,
         deltaL -> \[CapitalDelta],
         delta -> \[Delta]}, {x, (\[Delta] + 2 \[CapitalDelta])/
         m i, (\[Delta] + 2 \[CapitalDelta])/m (i + 1)},
       WorkingPrecision -> 5], {i, 0, m - 1}]

AbsoluteTiming[f[5.5, 100000, 12, 0.25]]

{1.2031435, 3.2305}

where m=4 is the number of kernels on my machine. I did it a bit clumsy, for sure you will do better.

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at

  • Prev by Date: Re: Simplify Binomial
  • Next by Date: compact notation for NonCommutativeMultiply that supports cut and paste
  • Previous by thread: Re: Nested numerical integral - speed: Is it suppose to be so slow?
  • Next by thread: How to increase evaluation speed for nested numerical integration