# Determining where a function is convex: convex

### 5.21.8 Determining where a function is convex: convex

The convex command determines where a function is convex.

- convex takes two mandatory arguments and one optional argument:
  - expr, an expression which is at least twice differentiable, which specifies a function f.
  - vars, the variable or list of variables in the expression. Some variables may depend on a common independent parameter, say t, when entered as e.g. x(t) instead of x. The first derivatives of such variables, when encountered in f, are treated as independent parameters of the function.
  - Optionally, simplify, which enables advanced simplification of the intermediate symbolic expressions and the final result.
- convex(expr,vars ⟨ ,simplify=bool⟩) returns:
  - true, if the function is convex on the entire domain.
  - false, if the function is nowhere convex.
  - otherwise, the list of inequalities specifying the domain on which the function is convex.
- The convex command operates by computing the Hessian Hf of f (see Section 5.21.3) and its LDL factorization. If the resulting block-diagonal matrix is positive semidefinite, then Hf is positive semidefinite and f is hence convex.
- Advanced simplification can be applied when generating convexity conditions by passing the simplify argument. By default, only basic simplification is applied by using the ratnormal command.
- The function f concave if and only if the function g=−f is convex.

#### Examples.

- Input:
  convex(3*exp(x)+5x^4-ln(x),x)
  Output:

  true

- Input:
  convex(x^2+y^2+3z^2-x*y+2x*z+y*z,[x,y,z])
  Output:

  true

- Input:
  convex(x1^3+2x1^2+2*x1*x2+x2^2/2-8x1-2x2-8,[x1,x2],simplify)
  Output:

  ⎡ ⎣ | x1≥ 0 | ⎤ ⎦

- In the example below, we find the values of a∈ℝ for which the function

  f(x,y,z)=x2+x z+a y z+z2

  is convex.
  Input:
  convex(x^2+x*z+a*y*z+z^2,[x,y,z])
  Output:

  false

  Note that the function is convex for a=0. However, the convex command does not support equalities as convexity constraints.
- For the next example, find all values a∈ℝ for which the function

  f(x,y,z)=x2+2 y2+a z2−2 x y+2 x z−6 y z

  is convex on ℝ3.
  Input:
  convex(x^2+2y^2+a*z^2-2x*y+2x*z-6y*z,[x,y,z],simplify)
  Output:

  ⎡ ⎣ | a≥ 5 | ⎤ ⎦

- Find the set S⊂ℝ2 on which the function f:ℝ2→ℝ defined by

  f(x1,x2)=exp(x1)+exp(x2)+x1 x2

  is convex.
  Input:
  condition:=convex(exp(x1)+exp(x2)+x1*x2,[x1,x2],simplify)
  Output:

  ⎡ ⎣ | ex1 ex2−1≥ 0 | ⎤ ⎦

  Input:
  lin(condition)
  (See Section 5.24.4.)
  Output:

  ⎡ ⎣ | ex1+x2−1≥ 0 | ⎤ ⎦

  From here you conclude that f is convex when x1+x2≥ 0. The set S is therefore the half-space defined by this inequality.

The algorithm respects the assumptions that may be set upon variables. Therefore, the convexity of a given function can be checked on a particular domain.

#### Examples.

- Input:

  assume(x1>0),assume(x2>0):;
  convex(exp(x1)+exp(x2)+x1*x2,[x1,x2])

  Output:

  true

- Input:

  assume(x>=0 and x<=pi/4):;
  convex(exp(y)*sec(x)^3-z,[x,y,z])

  Output:

  true