### 5.19.1 Determining whether a function is convex : convex

convex takes two mandatory arguments, an at least twice differentiable function(al) f:ℝn→ℝ and a variable or list of variables. 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 f.

The command returns a condition or list of conditions under which f is convex. If f is convex on the entire domain, the return value is true. If it is nowhere convex, the return value is false. Otherwise, the conditions are returned as inequalities which depend on the parameters of f. The returned inequalities are not necessarily independent.

An optional third argument simplify=false or simplify=true may be given. By default is simplify=true, which means that simplification is applied when generating convexity conditions. If simplify=false, only rational normalization is performed (using the ratnormal command).

The command operates by computing the Hessian Hf of f 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.

The function f is said to be concave if the function g=−f is convex.

For example, 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 :

[(3*x1+2)>=0,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∈ℝ :

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

Output :

false

In the next example we 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 :

cond:=convex(x`^`2+2y`^`2+a*z`^`2-2x*y+2x*z-6y*z,[x,y,z])

Output :

[a>=0,(a-1)>=0,(2*a-9)>=0,(a-5)>=0]

The returned inequalities are simplified by solve :

solve(cond,a)

Output :

list[a>=5]

Therefore f is convex for a≥ 5.

Let’s 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 :

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

Output :

(exp(x1)*exp(x2)-1)>=0

Input :

lin(cond)

Output :

(exp(x1+x2)-1)>=0

From here we conclude that f is convex when x1+x2≥ 0. The sought 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 only on a particular domain. For example, 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

##### The Brachistochrone Problem.

We want to minimize the objective functional

T(y)= | ∫ |
x1
0
| L(t,y(t),y′(t)) d t

where the Lagrangian L is defined by

L(t,y(t),y′(t))= |
√
1+y′(t)2
2 g y(t)

for y:[0,x1]→ℝ such that y(0)=y0 and y(x1)=0 where x1>0 and y0>0 are fixed (the constant g is the gravitational acceleration). This is called the brachistochrone problem (the problem of shortest travel by own weight from the point (0,y0) to (x1,0)). By solving Euler-Lagrange equation one obtains a cycloid y(t) as the only stationary function for L. The problem is to prove that it minimizes T, which would be easy if the integrand L was convex. However, it’s not the case here :

assume(y>=0):; assume(g>0):; convex(sqrt((1+y’`^`2)/(2*g*y)),y(t))

Output :

(-diff(y(t),t)`^`2+3)>=0

This is equivalent to |y′(t)|≤√3, which is certainly not satisfied by the cycloid y near the point x=0.

Using the substitution y(t)=z(t)2/2 we obtain y′(t)=z′(t) z(t) and

L(t,y(t),y′(t))=P(t,z(t),z′(t))= |
√
z(t)−2+z′(t)2
g
| .

The function P is convex :

assume(z>=0):; convex(sqrt((z`^`-2+z’`^`2)/g),z(t))

Output :

true

Hence the function z(t)=√2 y(t), stationary for P (which is verified directly), minimizes the objective functional

U(z)= | ∫ |
x1
0
| P(t,z(t),z′(t)) d t.

From here and U(z)=T(y) it easily follows that y minimizes T and therefore the brachistochrone. For details see John L. Troutman, Variational Calculus and Optimal Control (second edition), page 257.