### 16.2.1 Constrained global optimization

##### Functions of one variable.

For univariate minimization and maximization you can use the fMin and fMax commands.

- fMin and fMax both take two arguments:
  - expr, a univariate differentiable expression.
  - var, which is either x, a variable, or x=a..b where a and b are two real numbers such that a<b (it is allowed to enter -inf for a or inf for b).
- fMin(expr,var) (or analogously with fMax) returns the sequence of all points of global minimum (maximum) of expr in the specified interval or segment. If infinity is returned, then expr is unbounded.
- If fMin or fMax fails to find exact points of minima or maxima, they will be computed numerically and returned as floating-point values.

##### Functions of several variables.

The minimize and maximize commands attempt to find, using analytical methods, the exact minimal and maximal value of a differentiable expression on a compact domain specified by (in)equality constraints.

- minimize and maximize both take two mandatory arguments and two optional arguments:
  - obj, a univariate or multivariate expression
  - Optionally, constr, list of constraints given by equalities, inequalities, and/or expressions assumed to be equal to 0. If there is only one constraint, it does not have to be a list.
  - vars, list of variables. If there is only one variable, it does not have to be a list. A variable can also include bounds, as in x=a..b.

    Variables can also be given as x=x0, y=y0 and so on, in which case the optimum is computed numerically by performing a local search from the specified point (x0,y0,…).

  - Optionally, point.
- minimize(obj ⟨,constr ⟩,vars ⟨,point ⟩) (or analogously with maximize) returns the minimum (maximum) value of obj on the domain specified by constraints and/or bounding variables, or (if point is given) a list consisting of the minimum value and the list of points where the minimum is achieved.
- If the domain is not compact, the final result may be incorrect or meaningless. If the minimal value could not be found, then undef is returned. In some cases, unboundeness of the objective function can be detected.
- Note that minimize and maximize respect the bound constraints set to variables by the assume command.

#### Examples

Univariate optimization:

fMax(x*exp(-2 x^3),x=0..1)

| ⎛ ⎜ ⎜ ⎝ |
1
6
| ⎞ ⎟ ⎟ ⎠ |
1
3

fMin(sinc(x),x=0..20)

4.49340945791 |  |  |  |  |  |  |  |  |  |

minimize(sin(x),x=0..4)

sin | ⎛ ⎝ | 4 | ⎞ ⎠

minimize(x^4-x^2,x=-3..3,point)

| ⎡ ⎢ ⎢ ⎣ | − |
1
4
| , | ⎡ ⎢ ⎢ ⎣ |
√ |
2
2
| ,− |
√ |
2
2
| ⎤ ⎥ ⎥ ⎦ | ⎤ ⎥ ⎥ ⎦

minimize and maximize can handle absolute values and piecewise expressions in one or more variables. For example, we find global minimum and global maximum of the function

f(x,y)= |
|2+xy−y2|+|2x−x2+xy+y2|
1+x2+y2

on the square [−1,1]×[−1,1].

f(x,y):=(abs(2+x*y-y^2)+abs(2 x-x^2+x*y+y^2))/(1+x^2+y^2):; minimize(f(x,y),[x=-1..1,y=-1..1]); maximize(f(x,y),[x=-1..1,y=-1..1]);

1
3
| , |
√ |
5
| +3
2

As another example, input:

obj:=piecewise(x<=-2,x+6,x<=1,x^2,3/2-x/2):; maximize(obj,x=-3..2)

4 |  |  |  |  |  |  |  |  |  |

Each constraint can be either an equality or a non-strict inequality.

obj:=sqrt(x^2+y^2)-z:; constr:=[x^2+y^2<=16,x+y+z=10]:; minimize(obj,constr,[x,y,z])

−4 | √ |
2
| −6

minimize(x^2*(y+1)-2 y,[y<=2,sqrt(1+x^2)<=y],[x,y])

−4 |  |  |  |  |  |  |  |  |  |

minimize and maximize are aware of implied constraints that restrict the natural domain of the objective function. In the following example, the constraint x2+y2≤ 1 is implied, and the minimum corresponds to any point on the unit circle.

minimize(sqrt(1-x^2-y^2),[x,y],point)

| ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ | 0, |
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
√ |
−y2+1
| y
− | √ |
−y2+1
| y
x |
√ |
−x2+1
x |
− | √ |
−x2+1
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

As another example, it is known that the natural domain of arc sine is the segment [−1,1].

minimize(asin(x),x)

− |
π
2

Constraints can be built from simpler ones by using logical conjunction and disjunction. The latter is suitable for specifying disconnected domains, as in the following example.

minimize(x*y,[x^2+y^2<=1,x<=-3/4 or x>=5/6],[x,y],point)

| ⎡ ⎢ ⎢ ⎣ | − |
3 | √ |
7
16
| , |
⎡ ⎢ ⎢ ⎣ |
− |
3
4
√ |
7
4
| ⎤ ⎥ ⎥ ⎦
| ⎤ ⎥ ⎥ ⎦

Symbolic constants are allowed in the objective and constraints. Note that they have to be either implied or assumed real numbers.

assume(a>0):; maximize(x^2*y^2*z^2,x^2+y^2+z^2=a^2,[x,y,z])

a6
27

In the following example, we use minimize to obtain the formula for computing the distance d between the point P=(a,b,c) and the plane π given by the equation Ax+By+Cz+D=0. We accomplish this by finding a point (x0,y0,z0)∈π which is closest to P. Since we are going to minimize d2=(x−a)2+(y−b)2+(z−c)2, the distance is equal to the square root of the minimum.

d:=minimize((x-a)^2+(y-b)^2+(z-c)^2,A*x+B*y+C*z+D=0,[x,y,z]):; simplify(sqrt(d))

|A a+B b+C c+D|
√ |
A2+B2+C2

Note that the coordinates x0, y0 and z0 can be obtained by passing point as the fourth argument in minimize.