Qurak

Numerical nonlinear global optimization examples finding multiple optima method 2

Tutorial 3 steps, run in order in one session. Every step was executed against the engine and the output below is what it produced.

Step 1
Clear[f, pts, dots, cplot];
f[x_, y_] := (x^2 + y^2 - 16)^2;
Plot3D[f[x, y], {x, -5, 5}, {y, -5, 5}]
Output
-Graphics3D-
Step 2
{sol, pts} = Reap[
	NMinimize[f[x, y], {{x, -5, 5}, {y, -5, 5}}, Method -> "DifferentialEvolution", EvaluationMonitor :> Sow[{x, y}, "VisitedPoint"]],
	"VisitedPoint"];
sol
Output
NMinimize[(-16 + x^2 + y^2)^2, {{x, -5, 5}, {y, -5, 5}}, Method -> DifferentialEvolution, EvaluationMonitor :> Sow[{x, y}, VisitedPoint]]
Step 3
newpts = Cases[First[pts], x_ /; Abs[f@@x - First[sol]] ≤ .05];
rainbow[a_] := Hue[0.75 a, .5, 1];
dots = Graphics[Join[{PointSize[0.02], RGBColor[1, 1, 1], Point[#]& /@ newpts}, {{PointSize[0.02], RGBColor[0, 0, 0], Point[{x, y} /. Last[sol]]}}]];
cplot = ContourPlot[f[x, y], {x, -5, 5}, {y, -5, 5}, ColorFunction -> rainbow, Contours -> 4Range[0, 10] ^ 2, PlotPoints -> 50];
Show[cplot, dots]
Output
-Graphics-

Functions used

Related recipes

All recipes · Function reference · Use this from an MCP client