Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find the minimum of a function $f(t) = \int_0^1\!g(t,x)\,dx$. What I did in mathematica is as follows:

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

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

Any way to get around it? Thanks!

share|cite|improve this question
What's g[t,x]? If you have an explicit expression, we might manage to be more helpful. – J. M. Aug 13 '10 at 8:18
Also, it should be f[t_]:=... ; you need delayed evaluation instead of immediate evaluation of the RHS. Also changing the LHS of the definition to f[t_?NumericQ] might help. This is because FindMinimum[], unlike Plot[], does not have the HoldAll attribute. – J. M. Aug 13 '10 at 8:19
Bah, I don't really hang around in StackOverflow... XD too bad my comment was late! – J. M. Aug 13 '10 at 8:40
thank you guys!. next time i will ask on SO instead. – mr.gondolier Aug 13 '10 at 8:50

OP's equivalent question on Stack Overflow was already answered. This is the accepted answer by Andrew Moylan.

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).

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.