Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2191

Re: How to use NIntegrate to integrate a purely numeric vector function?

From Andrew Moylan <amoylan@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to use NIntegrate to integrate a purely numeric vector function?
Date 2011-05-08 11:34 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iq5v3b$ph2$1@smc.vnet.net> (permalink)

Show all headers | View raw


Hi Andre,

When NIntegrate threads over a list such as

In[1]:= NIntegrate[{a, a^2}, {a, 2, 3}]

Out[1]= {2.5, 6.33333}

it does so by effectively calling NIntegrate separately for each element of the list.

It only does this when the input is explicitly a list, so in your example,

In[2]:= f[a_?NumberQ] := {a, a*a}

you need to thread NIntegrate manually over the list (the results will be exactly the same). One simple way is:

In[3]:= Table[NIntegrate[f[a][[i]], {a, 2, 3}], {i, 2}]

During evaluation of In[3]:= Part::partw: Part 2 of f[a] does not exist. >>

Out[3]= {2.5, 6.33333}

The Part::partw message is due to attempted symbolic evaluation of the expression f[a][[i]]. It is harmless and can be silenced with Quiet.

To avoid the message entirely, you could do something like this:

In[4]:= Block[{g},
 Table[g[a_?NumberQ, i_] := f[a][[i]];
  NIntegrate[g[a, i], {a, 2, 3}], {i, 2}]]

Out[4]= {2.5, 6.33333}

In either case, it may be advantageous to make f remember the value of previous evaluations:

In[5]:= f[a_?NumberQ] := f[a] = {a, a*a}

This is because NIntegrate is likely to sample the same values of a when integrating each Part of f[a].

Hope this helps you,

Andrew Moylan
Wolfram Research


On May 7, 2011, at 9:33 PM, andre hollstein wrote:

> Hi Mathgroup, I hope you can help me.
> I want to use NIntegrate to integrate a purely numeric vector
> function. I simplified this problem to an easy example code. In my
> application the function f involves some matrix products with an
> InterpolatedFunction object (which is a vector)...
>
> So this is my problem:
>
> Clear[f]
> f[a_?NumberQ] := {a, a*a} (*f is returning a list*)
> NIntegrate[f[a], {a, 2, 3}]
>
> During evaluation of (Local) In[2569]:= NIntegrate::inum: Integrand
> f[a] is not numerical at {a} = {2.00796}. >>
> During evaluation of (Local) In[2569]:= NIntegrate::inum: Integrand
> f[a] is not numerical at {a} = {2.00796}. >>
> Out[2571]= NIntegrate[f[a], {a, 2, 3}]
>
> When f returns a scalar it it working just fine:
>
> Clear[f]
> f[a_?NumberQ] := a (*f is returning a scalar*)
> NIntegrate[f[a], {a, 2, 3}]
> Out[2577]= 2.5
>
> There is hopefully a way to solve this issue...
>
> Thanks, Andr=E9
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to use NIntegrate to integrate a purely numeric vector function? Andrew Moylan <amoylan@wolfram.com> - 2011-05-08 11:34 +0000

csiph-web