### 13.7.8 Determining where a function is 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 13.7.3) and its LDL factorization. If the resulting block-diagonal matrix is positive semidefinite, then Hf is positive semidefinite and f is hence convex.
- 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.
- 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

Verify that f(x)=3ex+5x4−ln(x) is convex on ℝ:

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

true |  |  |  |  |  |  |  |  |  |

Verify that f(x,y,z)=x2+y2+3z2−x y+2x z +y z is convex on ℝ3:

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

true |  |  |  |  |  |  |  |  |  |

The function f(x1,x2)=x13+2x12+2x1 x2+x22/2−8x1−2x2−8 is convex on [0,+∞⟩×ℝ:

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

| ⎡ ⎣ | x1≥ 0 | ⎤ ⎦

Find the values of a∈ℝ for which the function f(x,y,z)=x2+x z+a y z+z2 is convex:

convex(x^2+x*z+a*y*z+z^2,[x,y,z])

false |  |  |  |  |  |  |  |  |  |

Note that the function is convex for a=0. However, the convex command does not support equalities as convexity constraints.

Find all values a∈ℝ for which the function f(x,y,z)=x2+2y2+a z2−2x y+2x z−6y z is convex on ℝ3:

convex(x^2+2 y^2+a*z^2-2 x*y+2 x*z-6 y*z,[x,y,z],simplify)

| ⎡ ⎣ | a≥ 5 | ⎤ ⎦

Find the set S⊂ℝ2 on which the function f:ℝ2→ℝ defined by f(x1,x2)=ex1+ ex2+x1 x2 is convex:

condition:=convex(exp(x1)+exp(x2)+x1*x2,[x1,x2],simplify)

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

lin(condition)

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

(See Section 10.3.4.) 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 domain of the input function can be restricted by setting assumptions on variables:

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

true |  |  |  |  |  |  |  |  |  |

which was already verified in the previous example.