Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

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 smc.vnet.net
  • Subject: [mg127645] Re: Nested numerical integral - speed: Is it suppose to be so slow?
  • From: Sune Nørhøj Jespersen <sunenj at gmail.com>
  • Date: Fri, 10 Aug 2012 02:43:41 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20120809075343.71F12664F@smc.vnet.net> <88608B12-7C77-44FE-8D4C-1D6AFBF8F474@mit.edu>

Hi Craig and Bob.

Thank you very much for your replies. (Your answers are not appearing in the
group, so I hope I'm not doing anything wrong by e-mailing you directly)
Craigs suggestion resulted in roughly a factor of 10 speed up, so pretty
substantial! (I did try symbolic implementation at some point, but must've
done something wrong). With both of your suggestion (included below for
completeness), the execution time is now 0.7s, versus 13 seconds for the old
code.
Craig, t1 and t2 were replaced by combinations of g and a in the definition
of Fa.
Sune

The code is
In[17]:= trap[t_, t1_, t2_, a_] :=
 Piecewise[{{a t, 0 < t < t1}, {a t1, t1 < t < t2}, {a (t1 - t + t2),
    t2 < t < t2 + t1}}, 0]

In[18]:= Ga[t_, t1_, t2_, a_, \[CapitalDelta]_] :=
 trap[t, t1, t2, a] - trap[t - 2 \[CapitalDelta], t1, t2, a]

In[19]:= Fa[t_, g_, a_, \[CapitalDelta]_, \[Delta]_] :=
 Integrate[Ga[u, g/a, \[Delta] - g/a, a, \[CapitalDelta]], {u, 0, t},
  Assumptions -> Element[{t, g, a, \[Delta], \[CapitalDelta]}, Reals]]

In[20]:= ba[g_,
  a_, \[CapitalDelta]_, \[Delta]_] := (2.675222/10)^2 Integrate[
   Fa[x, g, a, \[CapitalDelta], \[Delta]]^2, {x,
    0, \[Delta] + 2 \[CapitalDelta]},
   Assumptions -> Element[{g, a, \[Delta], \[CapitalDelta]}, Reals]]

In[21]:= Timing[ba[5.5, 100000, 12, 0.25]]

Out[21]= {0.702, 3.23471}

--
Sune N=F8rh=F8j Jespersen
CFIN/MindLab and Dept. of Physics and Astronomy
Aarhus Universitet
N=F8rrebrogade 44, bygn 10G, 5. sal
8000 =C5rhus C
Denmark
Phone: +45 78463334
Cell: +45 60896642
E-mail: sune at cfin.au.dk
Web: http://www.cfin.au.dk/~sune


-----Original Message-----
From: W Craig Carter [mailto:ccarter at MIT.EDU]
Sent: 9. august 2012 12:26
To: Sune
Cc: mathgroup at smc.vnet.net
Subject: [mg127645] Re: Nested numerical integral - speed: Is it suppose to
be so slow?

Hello Sune,
I believe you can work symbolically:

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}}]

tplus = Integrate[trap[t, t1, t2, a], {u, 0, t},
  Assumptions -> Element[{g, a, \[Delta], \[CapitalDelta], t}, Reals]]

tminus = Integrate[trap[t - 2 \[CapitalDelta], t1, t2, a], {u, 0, t},
  Assumptions -> Element[{g, a, \[Delta], \[CapitalDelta], t}, Reals]]

inttminus =
 Integrate[tminus, {u, 0, t},
  Assumptions -> Element[{g, a, \[Delta], \[CapitalDelta], t}, Reals]]

inttplus =
 Integrate[tplus, {u, 0, t},
  Assumptions -> Element[{g, a, \[Delta], \[CapitalDelta], t}, Reals]]

baSym = (2.675222/10)^2 ( inttplus - inttminus)


baSym /. {t -> 5.5,
  a -> 100000, \[CapitalDelta] -> 12, \[Delta] -> 0.25} (*the result depends
on t1 and t2 which are undefined?*)



W Craig Carter
Professor of Materials Science, MIT



On Aug 9, , at Thu Aug 9, 12 @3:53 AM, Sune wrote:

> In[22]:= ba[g_,a_,\[CapitalDelta]_,\[Delta]_]:=(2.675222/10)^2
NIntegrate[Fa[x,g,a,\[CapitalDelta],\[Delta]]^2,{x,0,\[Delta]+2
\[CapitalDelta]}]





  • Prev by Date: Re: Nested numerical integral - speed: Is it suppose to be
  • Next by Date: Re: Nested numerical integral - speed: Is it suppose to be so slow?
  • Previous by thread: Re: Nested numerical integral - speed: Is it suppose to be
  • Next by thread: Re: Nested numerical integral - speed: Is it suppose to be so slow?