Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Using NIntegrate in a function
Replies: 2   Last Post: Mar 8, 2013 6:21 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Alexei Boulbitch

Posts: 479
Registered: 2/28/08
Re: Using NIntegrate in a function
Posted: Mar 8, 2013 6:21 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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


Hi, Michele

I checked your operators and they worked for me (PC WinXP, Math 9.0.1.0):

f[a_] := 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.333333}}

Without error messages. However, you might try also this:

lst = Table[f[a], {a, -1, 1, 0.01}];
Max[lst]

1.68967

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
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@iee.lu








Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.