Wolfram Optimization | Things to Try

Make edits and run any piece of code by clicking inside the code and pressing
+
.
Model, Solve & Analyze Designs. Wolfram Optimization provides a comprehensive set of tools to find the best design or make the best decision given constraints, fully integrated with highly automated machine learning, statistics, immediately computable built-in data and more.

Find Local or Global Extrema

Plot a function that has both global and local extrema:
Run
In[]:=
Plot[
4
x
+
3
x
-5
2
x
,{x,-3,3}]
Search for a local minimum near
x1
using numeric methods:
Run
In[]:=
FindMinimum[
4
x
+
3
x
-5
2
x
,{x,1}]
Now search for a local minimum near
x-1.8
using numeric methods:
Run
In[]:=
FindMinimum[
4
x
+
3
x
-5
2
x
,{x,-1.8}]
Minimize
finds the exact global minimum, while
FindMinimum
returns local minima encountered through search:
Run
In[]:=
Minimize[
4
x
+
3
x
-5
2
x
,x]
Constraints can also be included to find the minimum value in a restricted domain:
Run
In[]:=
Minimize[{
4
x
+
3
x
-5
2
x
,0<x<2},x]
NMinimize
can be used with the same syntax as
Minimize
if numeric results are desired. Notice that the minimum value on a restricted domain corresponds to the local minimum found through previous search:
Run
In[]:=
NMinimize[{
4
x
+
3
x
-5
2
x
,0<x<2},x]
Without a restricted domain, it aims to find global minima and return numeric results:
Run
In[]:=
NMinimize[
4
x
+
3
x
-5
2
x
,x]
Visualize the global and local minima that were found:
Run
In[]:=
Plot
4
x
+
3
x
-5
2
x
,{x,-3,3},Epilog->
Epilog for global and local minima

Find Extrema in Higher Dimensions

The same functionality can be used in higher dimensions. Define an objective function and constraints that can be visualized in three dimensions:
Run
In[]:=
objective=3Sin[
2
x
+
2
(y-1)
]-x-2y;constraints=Norm[{x,y}]>=1&&
4
x
+
4
y
<=10;
Visualize the objective function over the constrained region:
Run
regionandobjective=ShowPlot3Dobjective,{x,-2,2},{y,-2,2},AxesLabel{x,y,z},
Options settings
,DiscretizeRegionImplicitRegion[constraints&&z-9,{{x,-2,2},{y,-2,2},z}],
Options settings
Find the local minimum of the objective function near
(0,1)
:
Run
In[]:=
{localmin3D,localminimizer3D}=FindMinimum[objective,{{x,0},{y,1}}]
Find the global minimum of the objective function in the constrained region:
Run
In[]:=
{globalmin3D,globalminimizer3D}=NMinimize[{objective,constraints},{x,y}]
Visualize the global minimum (shown in red) and the local minimum (shown in blue) that were found:
Run
In[]:=
Show[regionandobjective,Graphics3D[{PointSize[Large],Blue,Point[{x,y,localmin3D}]/.localminimizer3D,Red,Point[{x,y,globalmin3D}]/.globalminimizer3D}]]

Minimize an Objective Function Subject to Constraints

Define regions in terms of constraints, intersections and built-in shapes:
Run
In[]:=
1
=RegionIntersection
,ImplicitRegion[2x-y>=3,{x,y}];
2
=RegionIntersection
,ImplicitRegion[2x-y<3,{x,y}];
Visualize an objective function over the previously defined regions:
Run
In[]:=
ShowPlot3D
3
(x+1)
+
2
(y-1)
,{x,y}
1
,
Options settings
,Plot3D
3
(x+1)
+
2
(y-1)
,{x,y}
2
,
Options settings
Use
NMinimize
to numerically find the minimum of the objective function over the first of the two regions defined above:
Run
In[]:=
NMinimize[
3
(x+1)
+
2
(y-1)
,{x,y}
1
]
Use
Minimize
to find the exact minimum of the objective function over the region of interest:
Run
In[]:=
{minvalue,minimizer}=Minimize[
3
(x+1)
+
2
(y-1)
,{x,y}
1
]
Show the constrained minimum found along with the objective function:
Run
In[]:=
ShowPlot3D
3
(x+1)
+
2
(y-1)
,{x,y}
1
,
Options settings
,Plot3D
3
(x+1)
+
2
(y-1)
,{x,y}
2
,
Options settings
,Graphics3D[{Red,PointSize[Large],Point[{x,y,minvalue}/.minimizer]}]

Solve Convex Optimization Problems

Define two implicit regions through constraints:
Run
In[]:=
1
=
2
x
+
2
y
<=1;
2
=
2
(x-2)
+
2
(y-1)
<=1;
Visualize the regions:
Run
In[]:=
RegionPlot[{
1
,
2
},{x,-1,3},{y,-1,3},PlotTheme->"Minimal",Axes->False]
Find the pair of points (one from each region) that minimizes the distance between them:
Run
In[]:=
closest2D=ConvexOptimization[Norm[p1-p2],{},{p1ImplicitRegion[
1
,{x,y}],p2ImplicitRegion[
2
,{x,y}]}]
Visualize the pair of points found and the distance between them:
Run
In[]:=
RegionPlot{
1
,
2
},{x,-1,3},{y,-1,3},PlotTheme->"Minimal",Axes->False,Epilog->
Epilog for points and line
Quickly define 3D regions using built-in shapes:
Run
In[]:=
1
=Dodecahedron[{0,0,0},{0,0}];
2
=Ellipsoid[{4,0,0},DiagonalMatrix[{2,3,1}]];
Visualize the shapes:
Run
In[]:=
Graphics3D[{
1
,
2
}]
Find the pair of points (one from each region) that minimizes the distance between the two:
Run
In[]:=
closest3D=ConvexOptimization[Norm[p1-p2],{},{p1
1
,p2
2
}]
Visualize the points found with a line connecting them:
Run
In[]:=
Graphics3D[{
1
,
2
,{Red,Thick,Line[{p1,p2}/.closest3D]}}]
Solve and visualize a dynamic version of the previous example:
Run
In[]:=
Manipulate[shape1=Dodecahedron[{0,0,0},{theta,phi}];shape2=Ellipsoid[{4,0,0},DiagonalMatrix[{2,3,1}]];dynamic3D=ConvexOptimization[Norm[p1-p2],{},{p1shape1,p2shape2}];Graphics3D[{shape1,shape2,{Red,Thick,Line[{p1,p2}/.dynamic3D]}},PlotRange->{{-1.5,5.5},{-2,2},{-1.4,1.4}}],{{theta,0,"Rotation about Z"},0,2Pi},{{phi,0,"Rotation about Y"},0,2Pi},ControlPlacement->Top,SaveDefinitions->True]

Use Optimal Fitting to Smooth Data

Find the Shortest Tour

Obtain Exact Solutions to Symbolic Optimization Problems

Obtain Parametric Solutions to Symbolic Optimization Problems

Wolfram Cloud

You are using a browser not supported by the Wolfram Cloud

Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


I understand and wish to continue anyway »

You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.