MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Plot vs FindMinimum

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18398] RE: [mg18314] Plot vs FindMinimum
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Wed, 7 Jul 1999 00:11:01 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

I had a mistake in my previous response:
Instead of   LeftPnt=Max[0,posn-1];
use    LeftPnt=Max[1,posn-1];
I fixed it in the code below.


Joaquin Gonzalez de Echavarri  wrote:
-------------------------------------

I'm trying to find the minimum of a function practically flat in a very
large intervall, after that it falls suddenly, reaches the minimun and rises
very fast again.

I'm able to see the function and the minimun with Plot in a fraction of
second but is very dificult and it takes a lot of time to find it with
FindMinimum, my question is:

Is it not possible to use the same algorithm that Plot uses for finding the
value of the minimun?

What's that algorithm?
-------------------

The Plot sampling algorithm for version 2.2 is described in 
The Mathematica Graphics Guidebook by
Cameron Smith, Nancy Blachman
See section 4.1.3

I think the algorithm has only changed a little since then.
-------------------

Below I show how you can get the needed information from the sampling
algorithm.

Below "example" is the result of Plot, and "pnts" is the list of samples.
The minimum function value of all the sample points is y1.  Variable "posn"
indicates which sample has the minimum function value.  We suspect the
global minimum is between x0 and x1 (the x values to the left and to the
right of y1).  The values (x0, x1) are use to start the FindMinimum routine.
Note FindMinimum can be used with only one starting point, but for that
method the kernel must be able to compute the derivative, and I think it's
less robust with that method.

------------------------

In[1]:=
example=Plot[Cos[x],{x,-3.2,-3},PlotPoints->5,PlotDivision->2];


In[2]:=
pnts=example[[1,1,1,1]]
Out[2]=
{{-3.2,-0.998295},{-3.1763,-0.999398},
{-3.15132,-0.999953},{-3.1382,-0.999994},
{-3.12598,-0.999878},{-3.11257,-0.999579},
{-3.09823,-0.99906},{-3.08536,-0.998419},
{-3.07138,-0.997536},{-3.04601,-0.995435},
{-3.02368,-0.993056},{-3.,-0.989993}}


In[3]:=
y1=Min[Part[Transpose[pnts],2]]
Out[3]=
-0.999994


In[4]:=
posn=Position[pnts,Min[Part[Transpose[pnts],2]]]  [[1,1]]
Out[4]=
4


In[5]:=
pnts[[4]]
Out[5]=
{-3.1382,-0.999994}


In[6]:=
LeftPnt=Max[1,posn-1];
RightPnt=Min[Length[pnts],posn+1];
{x0,x1}={pnts[[LeftPnt,1]],pnts[[RightPnt,1]]}
Out[8]=
{-3.15132,-3.12598}


In[9]:=
FindMinimum[Cos[x],{x,{x0,x1}}]
Out[9]=
{-1.,{x->-3.14159}}

----------------------
All the above is used in the GlobalMinimum function below.
 

In[10]:=
Options[GlobalMinimum]={Compiled->True,MaxBend->10.,PlotDivision->30,PlotPoi
nts->25};
 
GlobalMinimum[f_,{x_,xmin_,xmax_},opts___?OptionQ]:=
 Module[{gr,pnts,y1,posn,LeftPnt,RightPnt,x0,x1},
  gr=Plot[f,{x,xmin,xmax},DisplayFunction->Identity, opts];
  pnts=gr[[1,1,1,1]];
  y1=Min[Part[Transpose[pnts],2]];
  posn=Position[pnts,Min[Part[Transpose[pnts],2]]]  [[1,1]];
  LeftPnt=Max[1,posn-1];
  RightPnt=Min[Length[pnts],posn+1];
  {x0,x1}={pnts[[LeftPnt,1]],pnts[[RightPnt,1]]};
  FindMinimum[f,{x,{x0,x1}}]
 ]


Next I show that it works on a more interesting example.


In[12]:=
GlobalMinimum[Cos[Pi*x](1-3*Exp[-(x-2*Pi)^2]),{x,-30,10}]
Out[12]=
{-1.8219,{x->6.0665}}

----------------------
Regards,
Ted Ersek


  • Prev by Date: Integration of Oscillating Function
  • Next by Date: Re: [Q] 2D graphic routines and data sets in 2D lists
  • Previous by thread: RE: Plot vs FindMinimum
  • Next by thread: Re: Re: O.D.E in Power Series