# Maximum and minimum of an expression: fMax fMin

### 5.19.3 Maximum and minimum of an expression: fMax fMin

The fMax and fMin commands find where maxima and minima occur. They can do this for expressions of one variable or for expressions of several variables subject to a set of constraints, either equalities or inequalities.

The find the maximum and minimum of an expression with one variable:

- fMax and fMin take two arguments:
  - expr, an expression involving one variable.
  - Optionally, x, the name of the variable (by default x=x).
- fMax(expr ⟨,x⟩) returns the value of x that maximizes the expression.
- fMin(expr ⟨ ,x⟩) returns the value of x that minimizes the expression.

#### Examples.

- Input:
  fMax(sin(x),x)
  or:
  fMax(sin(x))
  or:
  fMax(sin(y),y)
  Output:

  π
  2

- Input:
  fMin(sin(x),x)
  or:
  fMin(sin(x))
  or:
  fMin(sin(y),y)
  Output:

  −

  π
  2

The find the maximum and minimum of an expression with several variables subject to constraints:

- fMax and fMin take four mandatory and two optional arguments:
  - expr, an expression with several variables.
  - constr, a list of constraints (equalities and inequalities).
  - vars, a list of the variables.
  - init, an initial guess (which must be a list of nonzero reals representing a feasible point).
  - Optionally, є, the precision. If this isn’t given, the default epsilon value is used (see Section 2.5.7, item 9).
  - Optionally, N, the maximum number of iterations.
  The expression expr does not need to be differentiable.
- fMax(expr, constr ,vars ,init ⟨ ,є⟩ ⟨,N ⟩) returns the vector of values that maximizes expr subject to the constraints constr.
- fMin(expr, constr ,vars ,init ⟨ ,є⟩ ⟨,N ⟩) returns the vector of values that minimizes expr subject to the constraints constr.

#### Examples.

- Input:
  fMax((x-2)^2+(y-1)^2,[-.25x^2-y^2+1>=0,x-2y+1=0],[x,y],[.5,.75])
  Output:

  | ⎡ ⎣ | −1.82287565553,−0.411437827766 | ⎤ ⎦

- Input:
  fMin((x-5)^2+y^2-25,[y>=x^2],[x,y],[1,1])
  Output:

  | ⎡ ⎣ | 1.2347728625,1.52466402196 | ⎤ ⎦

Although the initial point is required to be feasible, the algorithm will sometimes succeed even with a poor choice of initial point. Note that the initial value of a variable must not be zero.