5

I need to find the minimum of a function f(t) = int g(t,x) dx over [0,1]. What I did in mathematica is as follows:

f[t_] = NIntegrate[g[t,x],{x,-1,1}]
FindMinimum[f[t],{t,t0}]

However mathematica halts at the first try, because NIntegrate does not work with the symbolic t. It needs a specific value to evaluate. Although Plot[f[t],{t,0,1}] works perferctly, FindMinimum stops at the initial point.

I cannot replace NIntegrate by Integrate, because the function g is a bit complicated and if you type Integrate, mathematica just keep running...

Any way to get around it? Thanks!

gondolier
  • 235
  • 2
  • 5
  • See [this](http://stackoverflow.com/questions/6990285/mathematica-evaluation-order-during-numerical-optimisation-of-black-box-function/7020539#7020539) answer for pointer to the Documentation page with explanation of this behavior. – Alexey Popkov Aug 17 '11 at 08:53

3 Answers3

11

Try this:

In[58]:= g[t_, x_] := t^3 - t + x^2

In[59]:= f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]

In[60]:= FindMinimum[f[t], {t, 1}]

Out[60]= {-0.103134, {t -> 0.57735}}

In[61]:= Plot[f[t], {t, 0, 1}]

Two relevant changes I made to your code:

  1. Define f with := instead of with =. This effectively gives a definition for f "later", when the user of f has supplied the values of the arguments. See SetDelayed.

  2. Define f with t_?NumericQ instead of t_. This says, t can be anything numeric (Pi, 7, 0, etc). But not anything non-numeric (t, x, "foo", etc).

Andrew Moylan
  • 2,893
  • 18
  • 20
0

An ounce of analysis...

You can get an exact answer and completely avoid the heavy lifting of the numerical integration, as long as Mathematica can do symbolic integration of g[t,x] w.r.t x and then symbolic differentiation w.r.t. t. A less trivial example with a more complicated g[t,x] including polynomial products in x and t:

g[t_, x_] := t^2 + (7*t*x - (x^3)/13)^2;
xMax = 1; xMin = -1; f[t_?NumericQ] := NIntegrate[g[t, x], {x, xMin, xMax}];
tMin = 0; tMax = 1;Plot[f[t], {t, tMin, tMax}];
tNumericAtMin = t /. FindMinimum[f[t], {t, tMax}][[2]];
dig[t_, x_] := D[Integrate[g[t, x], x], t];
Print["Differentiated integral is ", dig[t, x]];
digAtXMax = dig[t, x] /. x -> xMax; digAtXMin = dig[t, x] /. x -> xMin;
tSymbolicAtMin = Resolve[digAtXMax - digAtXMin == 0 && tMin ≤ t ≤ tMax, {t}];
Print["Exact: ", tSymbolicAtMin[[2]]];
Print["Numeric: ", tNumericAtMin];
Print["Difference: ", tSymbolicAtMin [[2]] - tNumericAtMin // N];

with the result:

⁃Graphics⁃
Differentiated integral is 2 t x + 98 t x^3 / 3 - 14 x^5 / 65
Exact: 21/3380
Numeric: 0.00621302
Difference: -3.01143 x 10^-9
-1

Minimum of the function can be only at zero-points of it's derivate, so why to integrate in the first place?

  • You can use FindRoot or Solve to find roots of g
  • Then you can verify that points are really local minimums by checking derivates of g (it should be positive at that point).
  • Then you can NIntegrate to find minimum value of f - only one numerical integration!
phadej
  • 11,947
  • 41
  • 78
  • -1, @phadej, my apologies for this late comment, but I just ran across this. Unfortunately, your mathematics are incorrect as `g[x,t]==0` most likely will not occur where `f[t]==0`. A simple counter example is `Sin[x+t]`, and plotting `ContourPlot[Evaluate[{# == 0, D[Integrate[#, {x, 0, 1}], t]==0}], {x, 0, 1}, {t, -5, 5}] & @ Sin[x + t]` shows that there are regions in {x,t} space where `g[t,x]!= D[Integrate[g[t,x]],t]`. So, while it may work in special circumstances, e.g. `g[x,t]==T[t]X[x]` or `g[x,t]==T[t]+X[x]`, it cannot be generally applied. – rcollyer Nov 10 '10 at 15:30