Qurak

Numerical nonlinear global optimization examples finding multiple optima method 2

Tutorial 3 stappen, op volgorde uitgevoerd in één sessie. Elke stap is tegen de engine uitgevoerd en de uitvoer hieronder is wat hij opleverde.

Stap 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}]
Uitvoer
-Graphics3D-
Stap 2
{sol, pts} = Reap[
	NMinimize[f[x, y], {{x, -5, 5}, {y, -5, 5}}, Method -> "DifferentialEvolution", EvaluationMonitor :> Sow[{x, y}, "VisitedPoint"]],
	"VisitedPoint"];
sol
Uitvoer
NMinimize[(-16 + x^2 + y^2)^2, {{x, -5, 5}, {y, -5, 5}}, Method -> DifferentialEvolution, EvaluationMonitor :> Sow[{x, y}, VisitedPoint]]
Stap 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]
Uitvoer
-Graphics-

Gebruikte functies

Gerelateerde recepten

Alle recepten · Functiereferentie · Gebruik dit vanuit een MCP-client