How to check the convexity of a function? I recently started studying optimization, but I'm struggling with mathematics.
\begin{align}
\text{1.} \:\: f(x_0, x_1) &= x_0^2 + 3x_1^2 + x_0x_1 + 2x_0 \\
\text{2.} \:\: g(x_0, x_1) &= x_0^2 + 3x_1^2 + 4x_0x_1
\end{align}
I checked with a QP solver that the first function is convex and the rest is not convex.
Can I ask for a little help with mathematical derivation to check the convexity of the functions and can you give me some intuition that I could recognize a convex function with?
ps. thank you a lot in advance!!
Here are the following code lines for the functions, just in case!
cost7 = prog.AddQuadraticCost(x[0]**2 + 3 * x[1]**2 + x[0] * x[1] + 2 * x[0])
cost8 = prog.AddQuadraticCost(x[0]**2 + 3 * x[1]**2 + 4*x[0]*x[1])

 A: With functions of one variable, you would check for convexity by looking at the second derivative. Suppose you have $f(x)$: the function is convex on an interval $I$ if and only if $f''(x) \geq 0 \quad \forall x \in I$.
For multivariate functions (like the bivariate ones you have here), the principle is the same: the property of convexity is tied to the second derivative, which in this case takes the form of the Hessian matrix. The Hessian matrix is the matrix of second partial derivatives.
In particular, if the Hessian matrix is positive semidefinite, then the function is convex. In your case:
$$ H_f = \left[\begin{matrix}
\frac{\partial^2x_0}{\partial x_0^2} & \frac{\partial x_0x_1}{\partial x_0x_1}\\
\frac{\partial x_1x_0}{\partial x_1x_0} & \frac{\partial^2x_1}{\partial x_1^2}
\end{matrix}\right] = \left[\begin{matrix}
2 & 1 \\ 1 & 6
\end{matrix}\right]$$
$$ H_g = \ldots = \left[\begin{matrix}
2 & 4 \\ 4 & 6
\end{matrix}\right]$$
Now, how to check the positive semidefiniteness of these matrices? Since they are simmetric, you can look at the signs of their eigenvalues. In fact a if a matrix $H$ is symmetric and all of its eigenvalues are real and non-negative, $H$ is positive semidefinite. In your case:
$$ \lambda_{1f} \approx 1.76 > 0; \quad \lambda_{2f} \approx 6.24 > 0$$
Therefore $H_f$ is positive definite, which implies $f(x_0,x_1)$ is convex. On the other hand
$$ \lambda_{1g} \approx -0.47 < 0; \quad \lambda_{2g} \approx 8.47 > 0$$
and the negative eigenvalue implies $H_g$ is not positive definite nor positive semidefinite, therefore $g(x_0, x_1)$ is not convex.
A: Convexity is not always easy to see or to verify in general. One of the most simple and mechanical tests is to check if the second derivative, which is the Hessian matrix, is positive-definite. This can be verified using Sylvester's Criterion. While this method is one of the easiest to teach, I won't pretend it's intuitive. Intuition will take a lot more work!
The method is:

*

*You compute the Hessian of the function at an arbitrary point in the domain. In your two examples,
$$\begin{aligned}H_f &= \begin{pmatrix} \dfrac{\partial^2f}{\partial x_0^2} & \dfrac{\partial^2 f}{\partial x_0\partial x_1} \\ \dfrac{\partial^2f}{\partial x_1\partial x_0} & \dfrac{\partial^2 f}{\partial x_1^2} \end{pmatrix} \\ &= \begin{pmatrix} 2 & 1 \\ 1 & 6 \end{pmatrix}\end{aligned} \qquad \qquad  \begin{aligned} H_g &= \begin{pmatrix} 2 & 4 \\ 4 & 6 \end{pmatrix}\end{aligned}.$$
(Note: due to these being quadratics, the Hessians are constant, in much the same way that the second derivative of a parabola is constant. In general, these Hessians will contain variables like $x_0$ and $x_1$.)


*You apply Sylvester's criterion. This involves the computation of multiple determinants, making this a long step to do by hand for large matrices. You compute the determinants of the $k \times k$ submatrices up the top left of the matrix, for $k = 1, \ldots, n$ (where the matrix is $n \times n$, i.e. when the function takes $n$ variables). In other words, you ignore the bottom $n - k$ rows and rightmost $n - k$ columns, and compue the determinant of the given matrices. If each of these $n$ determinants is strictly positive, the function is positive definite.In our case, we have $2 \times 2$ matrices. We compute the determinant of the whole matrix, as well as the "determinant" of the top left $1 \times 1$ submatrix (which is just the top-left entry). For a general function $H(x_0, y_0)$, this comes down to showing:
$$\dfrac{\partial^2 f}{\partial x_0^2} \cdot \dfrac{\partial^2 f}{\partial x_1^2} - \left(\dfrac{\partial^2 f}{\partial x_0\partial x_1}\right)^2 > 0 \text{ and } \dfrac{\partial^2 f}{\partial x_0^2} > 0.$$
In our $f$, we have $2 \times 6 - 1 \times 1 = 11 > 0$ and $2 > 0$, so it is positive definite (at all points, since the matrix is constant) and so the function is convex.


*If one of the determinants you computed is strictly negative (not $0$) at some point, then you can conclude that the function is not convex. If, on the other hand, one or more of the determinants is $0$ (and the rest strictly positive), then the test is inconclusive; the function may or may not be convex.In the case of $g$, we compute $2 \times 6 - 4 \times 4 = -4 < 0$. This is sufficient (without checking that $2 > 0$) to show that the function is not convex.
That's the method. It gets longer and longer, the more variables you need to deal with. There's also the issue that it requires the function to be twice-differentiable, whereas convex functions need not even be once differentiable everywhere.
