From: Szabolcs Horvát on
guerom00 wrote:
> Thank you for your answers. So this IS indeed the right method.
> After some tests, it's just that my list contains 20'000 elements and
> the integration takes forever to finish...
> I thought Mathematica crashed because I thought it was a rather simple
> thing to do but it turns out I would need to run this integration on a
> powerful cluster or something :)

Hi,

Try using Integrate instead of NIntegrate. Integrate[] supports
InterpolatingFunction objects directly, so this will be much faster than
using NIntegrate[].

(I found out that Integrate can do this only because your message
prompted me to experiment, so thanks for this!)

Example:

In[1]:= f =
Interpolation(a)Table[{x, Sin[x^2]}, {x, 0., 20, 20/20000}];

In[2]:= Integrate[f[x], {x, 0, 20}] // Timing
Out[2]= {0.25, 0.639816}

Check result:

In[3]:= NIntegrate[Sin[x^2], {x, 0, 20}]
Out[3]= 0.639816

(Indeed, NIntegrating this takes a very long time. I haven't had the
patience to wait for it to finish.)

Szabolcs

From: Jean-Marc Gulliet on
guerom00 wrote:

> Thank you for your answers. So this IS indeed the right method.
> After some tests, it's just that my list contains 20'000 elements and
> the integration takes forever to finish...
> I thought Mathematica crashed because I thought it was a rather simple
> thing to do but it turns out I would need to run this integration on a
> powerful cluster or something :)

Before going to the extremes, like using a cluster, you should try
*Integrate[]* rather than *NIntegrate[]*. The symbolic integration tool
*Integrate[]* works very well over *InterpolatingFunction* object and it
is way faster (anything between about 40 and 100 times faster on my
system). For instance, it can integrate over 100,000 points in 3/4th of
a second.

In[1]:= data = RandomReal[{-1000, 1000}, {10, 2}];
f = Interpolation[data];
Timing(a)Integrate[f[x], {x, f[[1, 1, 1]], f[[1, 1, 2]]}]
Timing(a)NIntegrate[f[x], {x, f[[1, 1, 1]], f[[1, 1, 2]]}]
%[[1]]/%%[[1]]

Out[3]= {0.000163, -867887.}

Out[4]= {0.017414, -867887.}

Out[5]= 106.834

In[6]:= data = RandomReal[{-1000, 1000}, {100000, 2}];
f = Interpolation[data];
Timing(a)Integrate[f[x], {x, f[[1, 1, 1]], f[[1, 1, 2]]}]

Out[8]= {0.79024, -3.53364*10^7}

In[9]:= $Version

Out[9]= "6.0 for Mac OS X x86 (64-bit) (February 7, 2008)"

Regards,
-- Jean-Marc


From: guerom00 on
Thank you for your answers.
So... I must do something wrong because when I use Integrate instead of
NIntegrate, it returns an unevaluated results. Let me explain quickly
and I will also link to some snapshots of my code...

I have some data points :
data={{x1,y1},{x2,y2}...{xN,yN}}
f=Interpolation[data]
Integrate[f[r],{r,,x1,xN}] returns
Integrate[InterpolationFunction[{{x1,xN}}<>][r],{r,,x1,xN}]
NIntegrate[f[r],{r,,x1,xN}] returns the correct number...

It is more involved than this so here are couple of pictures. Hope
you'll be able to visualize them :

Using Integrate : http://img59.imageshack.us/img59/3074/usingintegrateas6.jpg
Using NIntegrate : http://img59.imageshack.us/img59/7663/usingnintegratepq0.jpg

Thank you again for your help.

From: antononcube on
Sometimes using Integrate can be faster than using NIntegrate. With
your example this is not true, if version 6.0 and higher is used:

In[1]:= $Version
Out[1]= "6.0 for Mac OS X x86 (32-bit) (June 19, 2007)"

In[2]:= f[x_] := Interpolation(a)Table[{x, Sin[x^2]}, {x, 0., 20,
20/20000}];

In[5]:= Integrate[f[x], {x, 0, 20}] ; // Timing
Out[5]= {0.081263, Null}

In[6]:= NIntegrate[Sin[x^2], {x, 0, 20}] ; // Timing
Out[6]= {0.022355, Null}

Anton Antonov
Wolfram Research, Inc.



On Apr 15, 6:50 am, Szabolcs Horv=E1t <szhor...(a)gmail.com> wrote:

>
> Hi,
>
> Try using Integrate instead ofNIntegrate. Integrate[] supports
> InterpolatingFunction objects directly, so this will be much faster than
> usingNIntegrate[].
>
> (I found out that Integrate can do this only because your message
> prompted me to experiment, so thanks for this!)
>
> Example:
>
> In[1]:= f =
> Interpolation(a)Table[{x, Sin[x^2]}, {x, 0., 20, 20/20000}];
>
> In[2]:= Integrate[f[x], {x, 0, 20}] // Timing
> Out[2]= {0.25, 0.639816}
>
> Check result:
>
> In[3]:=NIntegrate[Sin[x^2], {x, 0, 20}]
> Out[3]= 0.639816
>
> (Indeed, NIntegrating this takes a very long time. I haven't had the
> patience to wait for it to finish.)
>
> Szabolcs


From: guerom00 on
Nobody can help me ? Should I use Evaluate[] somewhere...

TIA

First  |  Prev  | 
Pages: 1 2 3
Prev: Exporting Sound
Next: remote Kernel