       RE: Plot vs FindMinimum

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

```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:=
example=Plot[Cos[x],{x,-3.2,-3},PlotPoints->5,PlotDivision->2];

In:=
pnts=example[[1,1,1,1]]
Out=
{{-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:=
y1=Min[Part[Transpose[pnts],2]]
Out=
-0.999994

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

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

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

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

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

In:=
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[0,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:=
GlobalMinimum[Cos[Pi*x](1-3*Exp[-(x-2*Pi)^2]),{x,-30,10}]
Out=
{-1.8219,{x->6.0665}}

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

```

• Prev by Date: col-vector * row-vector = matrix, how ?
• Next by Date: Re: Re: O.D.E in Power Series
• Previous by thread: Re: col-vector * row-vector = matrix, how ?
• Next by thread: RE: Plot vs FindMinimum