Docsity

Mathematica: Unconstrained Minimization with Steepest Descent and Conjugate Gradient, Study notes of Mathematics

Toggle sidebar
bg1
Unconstrained minimization
problems
Using the package provided in the textbook
You can copy the directory "OptimizationToolbox" in the CD into you Mathematica installation directory, under "AddOns ->
ExtraPackages". In some operating system (for example, linux), the directory "OptimizationToolbox" is shown as
"optimizationtoolbox" (some characters in lower case instead of upper case). So are the files under this directory. you will need to
manually rename them to the upper case name, so that Mathematica can read it correctly.
After copied the directory, you will be able to use the package. It is possible that there will be some warnings sinc ethe package
was written for an older version of Mathematica.
Example 1
Use the steepest descent method to find (x, y) that minimizes f(x,y) =
Hx+yL2+I2 Ix2+y2-1M-13M2
. The starting point is
(-1.25, 0.25).
In[1]:=
Needs@"OptimizationToolbox‘Unconstrained‘"D;
General::obspkg :
LinearAlgebra‘MatrixManipulation‘ is now obsolete. The legacy version being
loaded may conflict with current Mathematica functionality.
See the Compatibility Guide for updating information.
General::newpkg :
NumericalMath‘NLimit‘ is now available as the Numerical Calculus Package.
See the Compatibility Guide for updating information.
SetDelayed::write : Tag Norm in Norm@v_Dis Protected.
SetDelayed::write : Tag Hessian in Hessian@f_List, vars_Dis Protected.
SetDelayed::write : Tag Hessian in Hessian@f_, vars_Dis Protected.
General::stop :
Further output of SetDelayed::write will be suppressed during this calculation.
In[2]:=
? SteepestDescent
pf3
pf4
pf5

Partial preview of the text

Download Mathematica: Unconstrained Minimization with Steepest Descent and Conjugate Gradient and more Study notes Mathematics in PDF only on Docsity!

Unconstrained minimization

problems

Using the package provided in the textbook

You can copy the directory "OptimizationToolbox" in the CD into you Mathematica installation directory, under "AddOns ->

ExtraPackages". In some operating system (for example, linux), the directory "OptimizationToolbox" is shown as

"optimizationtoolbox" (some characters in lower case instead of upper case). So are the files under this directory. you will need to

manually rename them to the upper case name, so that Mathematica can read it correctly.

After copied the directory, you will be able to use the package. It is possible that there will be some warnings sinc ethe package

was written for an older version of Mathematica.

ü Example 1

Use the steepest descent method to find (x, y) that minimizes f(x,y) = H x + y L^2 + I 2 I x^2 + y^2 - 1 M - 1 ê 3 M

2

. The starting point is

In[1]:= Needs@"OptimizationToolbox‘Unconstrained‘"D;

General::obspkg : LinearAlgebra‘MatrixManipulation‘ is now obsolete. The legacy version being loaded may conflict with current Mathematica functionality. See the Compatibility Guide for updating information. á

General::newpkg : NumericalMath‘NLimit‘ is now available as the Numerical Calculus Package. See the Compatibility Guide for updating information. á

SetDelayed::write : Tag Norm in Norm@v_D is Protected. á

SetDelayed::write : Tag Hessian in Hessian@f_List, vars_D is Protected. á

SetDelayed::write : Tag Hessian in Hessian@f_, vars_D is Protected. á

General::stop : Further output of SetDelayed::write will be suppressed during this calculation. á

In[2]:=? SteepestDescent

SteepestDescent@f, vars, x0, optsD. Computes a minimum of fHvarsL starting from x0 using the steepest descent method. The step length is computed using analytical line search. See Options@SteepestDescentD to see a list of options for the function. The function returns 8 x, hist<. x is either the optimum point or the next point after MaxIterations. hist contains history of values tried at different iterations.

In[3]:=? PlotSearchPath

PlotSearchPath@f, 8 x1, x1min, x1max<, 8 x2, x2min, x2max<, hist, optsD shows complete search path superimposed on a contour plot of the function f over the specified range. hist is assumed to be of the form 8 pt1, pt2, º<, where pt1 = 8 x1,x2< is the first point, etc. The function accepts all relevant options of the standard ContourPlot and the Graphics functions.

Defin the function, variables, and the starting point:

In[4]:= f^ =^ Hx^ +^ yL^ ^ 2^ +^ H^2 *****^ Hx ^ 2^ +^ y ^ 2^ -^1 L^ -^1 ê^3 L^ ^ 2; vars = 8 x, y<; x0 = 8 - 1.25, 0.25<;

Use SteepestDescent to find the local minimum:

In[6]:=^8 opt,^ hist<^ =^ SteepestDescent@f,^ vars,^ x0D;

f Æ Hx + yL^2 + I- ÄÄÄ^13 + 2 I- 1 + x^2 + y^2 MM

2

—f Æ

  • ÄÄÄÄÄÄÄÄ^ 50 x 3 + 16 x^3 + 2 y + 16 x y^2

2 x - ÄÄÄÄÄÄÄÄ^ 50 y 3 + 16 x^2 y + 16 y^3

Optimum: 8 - 0.764703, 0.762588< after 27 iterations

In[7]:= xhist = Drop@Transpose@histD@@ 1 DD, 1D; PlotSearchPath@f, 8 x, - 1.4, - 0.5<, 8 y, 0, 1<, xhistD

Out[8]=

  • 1.4 - 1.2 - 1.0 - 0.8 - 0.

However, if we plot the graph in a larger region, we can see that this problem may have another local minimum:

f Æ Hx + yL^2 + I- ÄÄÄ 1 3 +^2 I-^1 +^ x

(^2) + y (^2) MM^2

—f Æ

- ÄÄÄÄÄÄÄÄ

50 x 3 +^ 16 x

(^3) + 2 y + 16 x y 2

2 x - ÄÄÄÄÄÄÄÄ 50 y 3 +^ 16 x

(^2) y + 16 y 3

Optimum: 8 0.764703, - 0.762588< after 27 iterations

Out[13]=

  • 2 - 1 0 1 2
  • 2
  • 1

0

1

2

x

y

ü Example 2

Use the conjugate gradient method to find (x,y) that minimized f(x,y)=H x + y L

2

+ I 2 I x^2 + y^2 - 1 M - 1 ê 3 M

2

. The starting point is

In[14]:= f^ =^ Hx^ +^ yL^ ^ 2^ +^ H^2 *****^ Hx ^ 2^ +^ y ^ 2^ -^1 L^ -^1 ê^3 L^ ^ 2; vars = 8 x, y<; x0 = 8 - 1.25, 0.25<; 8 opt, hist< = ConjugateGradient@f, vars, x0D; xhist = Drop@Transpose@histD@@ 1 DD, 1D; PlotSearchPath@f, 8 x, - 1.4, - 0.5<, 8 y, 0, 1<, xhistD

f Æ Hx + yL^2 + I- ÄÄÄ 1 3 +^2 I-^1 +^ x

(^2) + y (^2) MM^2

—f Æ

- ÄÄÄÄÄÄÄÄ

50 x 3 +^ 16 x

(^3) + 2 y + 16 x y 2

2 x - ÄÄÄÄÄÄÄÄ 50 y 3 +^ 16 x

(^2) y + 16 y 3

Using the PolakRibiere method with Analytical line search

Optimum: 8 - 0.763759, 0.763766< after 6 iterations

Out[18]=

  • 1.4 - 1.2 - 1.0 - 0.8 - 0.

Remark: comparing the iterations for convergence, we can see that for the example problem, SteepestDescent converges in 27

steps and ConjugateGradient converges in 6 iterations.

Using the Package provided by Mathematica

Before we start, let’s first clean the entire working space. This will remove the previously loaded package OptimizationToolbox.

In[19]:= << Utilities‘CleanSlate‘ CleanSlate@D;

HCleanSlateL Contexts purged: 8 Global‘<

HCleanSlateL Approximate kernel memory recovered: 472 Kb

Mathematica contains an Optimization package which provides solvers for UnconstrainedProblems. To load this package, use the

following command: