Determining where a function is convex: convex

### 6.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=bool, where bool can be true or false.
- 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 region where the function is convex is returned as inequalities (not necessarily independent) involving the variables.

The command operates by computing the Hessian Hf of f (see Section 6.21.3) and its principal minors (in total 2n of them where n is the number of parameters) and checks their signs. If all minors are nonnegative, then Hf is positive semidefinite and f is therefore convex. Simplification is by default applied when generating convexity conditions. With a third argument of simplify=false, only rational normalization is performed (using the ratnormal command). simplify=true is the same as the default.

The function f is said to be concave 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])
  Output:

  ⎡ ⎣ | x1≥ 0 | ⎤ ⎦

- In the example below, the function f(x,y,z)=x2+x z+a y z+z2 is not convex regardless of the value a∈ℝ:
  Input:
  convex(x^2+x*z+a*y*z+z^2,[x,y,z])
  Output:

  false

- 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])
  Output:

  ⎡ ⎣ | a≥ 5 | ⎤ ⎦

  Therefore f is convex for 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])
  Output:

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

  Input:
  lin(condition)
  (See Section 6.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