       Re: Using NIntegrate in a function

• To: mathgroup at smc.vnet.net
• Subject: [mg130071] Re: Using NIntegrate in a function
• From: Bob Hanlon <hanlonr357 at gmail.com>
• Date: Thu, 7 Mar 2013 22:50:22 -0500 (EST)
• 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: <20130307085836.EF08A6468@smc.vnet.net>

```The function f can only be evaluated if its argument is numeric since
it uses a numerical technique (NIntegrate); consequently, restrict its
definition to numeric arguments.

f[a_?NumericQ] :=
NIntegrate[Exp[-((a - 1/3)^2 + 1)*x^4], {x, -1, 1}]

FindMaximum[{f[y], -1 <= y <= 1}, {y, 1/2}]

{1.68968, {y -> 0.33333}}

Whereas,

f2[a_] = Integrate[Exp[-((a - 1/3)^2 + 1)*x^4], {x, -1, 1}];

FindMaximum[{f2[y], -1 <= y <= 1}, {y, 1/2}]

{1.68968, {y -> 0.333333}}

Bob Hanlon

On Thu, Mar 7, 2013 at 3:58 AM,  <michele.castellana at gmail.com> wrote:
> Dear all,
> I am struggling with the following problem, I will explain the problem to you with this simple toy example: I define a function f of a variable a through a numerical integration
>
> f[a_] := NIntegrate[Exp[-((a - 1/3)^2 + 1)*x^4], {x, -1, 1}]
>
> I want to find numerically the maximum of f with respect to a. If I use FindMaximum,
>
> FindMaximum[{f[y], -1 <= y <= 1}, {y, 1/2}]
>
> Then I have some error messages:
>
> NIntegrate::inumr: The integrand E^(x^4 (-1-(-(1/3)+y)^2)) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>
>
> NIntegrate::inumr: The integrand E^(x^4 (-1-(-(1/3)+y)^2)) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>
>
> NIntegrate::inumr: The integrand E^(x^4 (-1-(-(1/3)+y)^2)) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>
>
> General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>
>
> Still, in the end I have got the correct result {1.68968, {y -> 0.333333}}. NMaximize gives the same error messages.
>
> I have a more complicated example where I have a numerical routine (NDSolve) that needs some parameter q as an input and that is incorporated into a function g[q], just like in the toy example the numerical routine NIntegrate needs the parameter a, and NIntegrate is incorporated into the function f[a]. In this more complicated example, I have got the same kind of complaints, NDSolve::ndnl: "Endpoint q in {x,q,qp} is not a real number.", but in the end when I call NMaximize of FindMaximum to maximize g with respect to q, it crashes and I have got no useful output. Still, the function g[q] is well-defined, and when I call it for any numerical value of q I obtain a number and everything is fine.
>
> Do you have any ideas on how to fix this?
>
> Thanks!
> Best
> Michele
>
>
>

```

• Prev by Date: Equation Labels
• Next by Date: Re: Plot3D Axes Labels overlap plot axes and ticks?
• Previous by thread: Using NIntegrate in a function
• Next by thread: Re: Using NIntegrate in a function