The Gradient and Hessian, Explained

After reading this you can compute the gradient vector and Hessian matrix of a function of several variables by hand, find its stationary points, and classify each one as a minimum, maximum or saddle.

What the gradient and Hessian are

A function of several variables, such as f(x, y) = x^2 + xy + y^2, has a slope in every direction. Two objects collect that information. The gradient is the vector of first partial derivatives. It points in the direction of steepest increase and its length is the steepest rate. The Hessian is the matrix of second partial derivatives. It describes how the slope itself changes, that is, the curvature.

Here is the quick payoff. At the point (x, y) = (0, 0) the function above has gradient (0, 0), so it is flat there. The Hessian is constant and positive definite, so that flat point is a genuine minimum with value 0. Every other point of the surface sits higher. The rest of this article shows how to reach that conclusion mechanically.

When to use this, and when not

Reach for the gradient and Hessian when you have a smooth function of two or more variables and you want to find or classify its critical points: the peaks, valleys and passes. This is the standard toolkit for unconstrained optimization, least-squares fitting and the local shape of a surface.

Do not use it where the function is not twice differentiable. Corners, absolute values and jumps break the second derivatives. For a single variable, the ordinary second derivative test is enough, and the Derivative & Integral Calculator handles it directly. If your problem has equality constraints, the plain Hessian test does not apply; you need the bordered Hessian from the method of Lagrange multipliers, which this page does not compute.

The Hessian test classifies a point only when the gradient is already zero there. At a point where the gradient is nonzero, the function is still sloping, so it cannot be a local extremum no matter what the curvature looks like.

The formulas and the intuition

For a function f(x_1, \dots, x_n), the gradient is the column of first partials.

\nabla f = \left( \frac{\partial f}{\partial x_1}, \dots, \frac{\partial f}{\partial x_n} \right)

Each entry \partial f / \partial x_i is the ordinary derivative in the direction of x_i while the other variables are held fixed. A point where \nabla f = 0 is called a stationary point or critical point.

The Hessian collects the second partials into a square matrix.

H_{ij} = \frac{\partial^2 f}{\partial x_i \, \partial x_j}

Here H_{ij} is the derivative of \partial f / \partial x_i with respect to x_j. For functions with continuous second derivatives the order of differentiation does not matter (Clairaut's theorem), so H_{ij} = H_{ji} and the matrix is symmetric.

The classification uses the definiteness of the Hessian at the stationary point.

Positive definite
All eigenvalues are positive. The surface curves up in every direction: a local minimum.
Negative definite
All eigenvalues are negative. The surface curves down in every direction: a local maximum.
Indefinite
Some eigenvalues positive, some negative. Up one way, down another: a saddle point.
Semidefinite (a zero eigenvalue)
The test is inconclusive. Higher-order terms decide.

For a 2 by 2 Hessian you can skip eigenvalues and use the determinant. With H = \begin{pmatrix} A & B \\ B & C \end{pmatrix}, let D = AC - B^2. If D \gt 0 and A \gt 0 it is a minimum; if D \gt 0 and A \lt 0 it is a maximum; if D \lt 0 it is a saddle.

Worked example: the demo function

f(x, y) = x^2 + xy + y^2 at (1, 1)

This reproduces the demo button. The variables are x, y and the evaluation point is 1, 1.

  1. First partials. \partial f/\partial x = 2x + y and \partial f/\partial y = x + 2y. The gradient is \nabla f = (2x + y, \; x + 2y).
  2. Evaluate the gradient at (1, 1): \nabla f = (2 \cdot 1 + 1, \; 1 + 2 \cdot 1) = (3, 3). The gradient is not zero, so (1, 1) is not a stationary point. The function still slopes uphill there.
  3. Second partials. \partial^2 f/\partial x^2 = 2, \partial^2 f/\partial y^2 = 2, and the mixed partial \partial^2 f/\partial x \partial y = 1. The Hessian is constant: H = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}.
  4. Definiteness. D = AC - B^2 = 2 \cdot 2 - 1^2 = 3 \gt 0 and A = 2 \gt 0, so the Hessian is positive definite. The eigenvalues are 3 and 1, both positive, which agrees.
  5. Find the real minimum. Set the gradient to zero: 2x + y = 0 and x + 2y = 0 give (x, y) = (0, 0). Because the Hessian is positive definite everywhere, (0, 0) is the global minimum, with f(0,0) = 0.

So the point you were asked about, (1, 1) with value 3, sits on the slope leading down to the true bottom at the origin.

The function along the line y = x. The minimum sits at t = 0, and the marker at t = 1 shows the demo point with value 3.

Explore how curvature sets the shape

Consider the quadratic f(x,y) = ax^2 + bxy + cy^2 with Hessian \begin{pmatrix} 2a & b \\ b & 2c \end{pmatrix}. With a = c = 1, b = 1 the determinant is 4 - 1 = 3 \gt 0, a bowl (minimum). Set b = 3 and the determinant becomes 4 - 9 = -5 \lt 0, a saddle. Set a = c = -1, b = 0 and it is a dome (maximum).

Reading and interpreting the output

Start with the gradient. If it is not the zero vector at your point, stop classifying: the point is on a slope, not at a peak, valley or pass. The gradient's direction tells you which way is uphill, and its magnitude, \|\nabla f\|, is how steep. At (1,1) above, \|\nabla f\| = \sqrt{3^2 + 3^2} = \sqrt{18} \approx 4.243.

When the gradient is zero, read the Hessian. Check the eigenvalues (or, in 2D, the determinant and the top-left entry). The signs give the verdict from the table below.

Classifying a stationary point from a 2 by 2 Hessian with entries A, B, C
Determinant D = AC - B^2Entry AEigenvalue signsVerdict
> 0> 0both +Local minimum
> 0< 0both -Local maximum
< 0anymixedSaddle point
= 0anya zeroInconclusive

The determinant equals the product of the eigenvalues, and the trace A + C equals their sum. For the demo Hessian, product is 3 and sum is 4, which forces eigenvalues 1 and 3.

Common mistakes

The biggest error is classifying a point where the gradient is nonzero. The Hessian test is only valid at stationary points. If you feed a random point, the definiteness of the Hessian tells you the local curvature but says nothing about a max or min there.

Watch the order of variables. The gradient and Hessian are ordered by the variable list you supply. If you list y, x instead of x, y, every entry shifts position, and a hand comparison will look wrong even though the math is right.

Do not read too much into a zero determinant. When D = 0 the test is genuinely inconclusive. Take f = x^4 + y^2 at the origin: the Hessian is \begin{pmatrix} 0 & 0 \\ 0 & 2 \end{pmatrix}, semidefinite, yet the origin is a true minimum. The quartic term settles it, not the second derivatives.

Finally, a Hessian test gives local information. A positive definite Hessian at one stationary point does not rule out a lower valley elsewhere. Only when the function is convex (positive semidefinite Hessian everywhere) does a local minimum become the global one.

Related tools

For a single-variable slope or a definite integral, use the Derivative & Integral Calculator. To see where a function of one variable has its extrema and inflection points with a graph, try the Function Analyzer & Grapher. To approximate a multivariable function near a point (the Hessian is the second-order piece of that approximation), the Taylor & Maclaurin Series tool builds the expansion. If you need to check a slope through a limit definition, the Limit Calculator is the direct route.

Frequently asked questions

What does it mean when the gradient is zero?

It means the function is flat in every direction at that point, so it is a candidate for a maximum, minimum or saddle. You then use the Hessian to decide which. A zero gradient alone does not tell you the type.

How is the Hessian different from the gradient?

The gradient is a vector of first derivatives that measures slope. The Hessian is a matrix of second derivatives that measures curvature, how fast that slope changes. For x^2 + xy + y^2 the gradient varies with position, but the Hessian is the constant matrix \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}.

Why is the Hessian symmetric?

Because mixed partials are equal when the second derivatives are continuous (Clairaut's theorem): \partial^2 f / \partial x \partial y = \partial^2 f / \partial y \partial x. So the entry above the diagonal matches the entry below it.

What happens when the determinant of the Hessian is zero?

The second derivative test is inconclusive. The point could be a minimum, a maximum or a saddle, and you have to look at higher-order terms. The example x^4 + y^2 at the origin is a minimum despite a zero determinant.

Can I use this for more than two variables?

Yes. The gradient is an n-vector and the Hessian is an n \times n matrix. Classification then uses the eigenvalues: all positive for a minimum, all negative for a maximum, mixed signs for a saddle. The 2D determinant shortcut only applies to the 2 by 2 case.