Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find minimum of a function defined by integration in Mathematica

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!

like image 624
gondolier Avatar asked Aug 13 '10 07:08

gondolier


People also ask

How do you find the minimum value in Mathematica?

FindMinimum[f,{x,x0,x1}] searches for a local minimum in f using x0 and x1 as the first two values of x, avoiding the use of derivatives. FindMinimum[f,{x,x0,xmin,xmax}] searches for a local minimum, stopping the search if x ever gets outside the range xmin to xmax.


1 Answers

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

like image 153
Andrew Moylan Avatar answered Sep 22 '22 04:09

Andrew Moylan