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 H_{f} of f and its principal minors (in total 2^{n} of them where n is the number of parameters) and checks their signs. If all minors are nonnegative, then H_{f} 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 :
^
4ln(x),x)
Output :
Input :
^
2+y^
2+3z^
2x*y+2x*z+y*z,[x,y,z])
Output :
Input :
^
3+2x1^
2+2*x1*x2+x2^
2/28x12x28,[x1,x2])
Output :
In the example below, the function f(x,y,z)=x^{2}+x z+a y z+z^{2} is not convex regardless of the value a∈ℝ :
^
2+x*z+a*y*z+z^
2,[x,y,z])
Output :
In the next example we find all values a∈ℝ for which the function
f(x,y,z)=x^{2}+2 y^{2}+a z^{2}−2 x y+2 x z−6 y z 
is convex on ℝ^{3}. Input :
^
2+2y^
2+a*z^
22x*y+2x*z6y*z,[x,y,z])
Output :
The returned inequalities are simplified by solve :
Output :
Therefore f is convex for a≥ 5.
Let’s find the set S⊂ℝ^{2} on which the function f:ℝ^{2}→ℝ defined by
f(x_{1},x_{2})=exp(x_{1})+exp(x_{2})+x_{1} x_{2} 
is convex. Input :
Output :
Input :
Output :
From here we conclude that f is convex when x_{1}+x_{2}≥ 0. The sought set S is therefore the halfspace 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 :
Output :
Input :
^
3z,[x,y,z])
Output :
We want to minimize the objective functional
T(y)=  ∫ 
 L(t,y(t),y′(t)) d t 
where the Lagrangian L is defined by
L(t,y(t),y′(t))= 


for y:[0,x_{1}]→ℝ such that y(0)=y_{0} and y(x_{1})=0 where x_{1}>0 and y_{0}>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,y_{0}) to (x_{1},0)). By solving EulerLagrange 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 :
^
2)/(2*g*y)),y(t))
Output :
^
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))= 

 . 
The function P is convex :
^
2+z’^
2)/g),z(t))
Output :
Hence the function z(t)=√2 y(t), stationary for P (which is verified directly), minimizes the objective functional
U(z)=  ∫ 
 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.