What the gradient is and why it is a vector
A scalar field attaches one number to every point — a temperature, an elevation, a potential, a loss value. Its gradient attaches a vector to every point, assembled from the partial derivatives. The vector points in the direction in which the scalar increases fastest, and its length is the rate of that increase per unit distance.
It is worth being clear about why a vector is the right object here. The rate of change of f depends on which way you move, so you might expect to need a separate number for every direction — infinitely many. Differentiability rescues you: for a differentiable function the rate in direction u is exactly ∇f·u, so two numbers in the plane, or three in space, encode all of them. The gradient is the compressed form of the entire local behaviour of f to first order.
Everything else follows from the dot product. Writing ∇f·u = |∇f|cos θ shows that the rate is greatest when θ = 0, so the fastest increase is along ∇f at rate |∇f|; most negative along −∇f; and zero when u is perpendicular to ∇f, which means the gradient is normal to the level set through the point. Uphill, downhill and level are all read off one vector.
That last property is what makes gradients ubiquitous outside pure mathematics. Gradient descent moves along −∇f because no direction reduces f faster to first order. Heat flows down a temperature gradient, by Fourier's law. Force is minus the gradient of potential energy. In every case the gradient is doing the same job: turning a scalar landscape into a field of directions.
Computing the gradient, and what its magnitude means
Each component is an ordinary partial derivative: differentiate with respect to one variable while holding the others fixed. For f = x²y³ the x-derivative treats y³ as a constant, giving 2xy³, and the y-derivative treats x² as a constant, giving 3x²y². There is no interaction between the components — the difficulty in multivariable calculus is never in taking the partials, it is in remembering what to do with them afterwards.
The magnitude |∇f| = √(fₓ² + f_y² + f_z²) is a genuine physical rate, not just an algebraic length. If f is elevation in metres and distances are metres, then |∇f| is the maximum slope in metres of rise per metre travelled — multiply by 100 for a percentage grade, or take the arctangent for an angle. A gradient magnitude of 0.1 is a 10% grade, which is a steep road; a magnitude of 1 is 45 degrees.
The bearing reported here is atan2(f_y, fₓ) in degrees, measured anticlockwise from the positive x-axis. Using atan2 rather than arctan(f_y/fₓ) matters: the two-argument form keeps the quadrant, so a gradient of (−3, −4) is reported at −126.87° rather than being confused with (3, 4) at 53.13°.
This calculator differentiates numerically, one coordinate at a time, using a central difference at a step of 10⁻⁴ scaled to the size of that coordinate and then Richardson-extrapolating a pair of them to fourth-order accuracy. For a smooth function that is accurate to roughly twelve significant figures, well inside the eight decimal places displayed. It costs 4n function evaluations for an n-variable gradient, which is negligible here and prohibitive in high dimensions — the reason large-scale optimisation uses automatic differentiation instead.
Worked example: f(x, y) = x² + y² at the point (3, 4)
This is the default, and every number is exact.
- Partial with respect to x. Hold y fixed: ∂f/∂x = 2x, which at x = 3 is 6.
- Partial with respect to y. Hold x fixed: ∂f/∂y = 2y, which at y = 4 is 8.
- Assemble. ∇f(3, 4) = (6, 8).
- Magnitude. |∇f| = √(36 + 64) = √100 = 10. So the steepest slope at this point is 10 units of f per unit of distance.
- Unit direction. (6, 8)/10 = (0.6, 0.8), at a bearing of atan2(8, 6) = 53.13° from the x-axis.
- Check it is normal to the level curve. The level curve through (3, 4) is x² + y² = 25, a circle of radius 5. Its tangent at that point is perpendicular to the radius, and the radius direction is (3, 4)/5 = (0.6, 0.8) — the same unit vector. The gradient of a circularly symmetric function points radially outward, as it must.
Two consequences worth extracting. First, |∇f| = 10 = 2 × 5, which is 2r at radius r — the gradient of x² + y² grows linearly with distance from the origin, so the bowl gets steeper as you go out. Second, the tangent line to the level curve at (3, 4) is 6(x − 3) + 8(y − 4) = 0, that is 6x + 8y = 50, or 3x + 4y = 25. You have obtained a tangent line without ever solving the circle for y, which is the practical payoff of the normal-vector property.
Now compare with f = √(x² + y²) at the same point, which is the distance from the origin rather than its square. Its gradient is (x/r, y/r) = (0.6, 0.8), of magnitude exactly 1 everywhere except the origin. That is the right answer for a distance function: moving one unit directly away from the origin increases your distance from it by exactly one unit, no matter where you start.
Reading the gradient
Read the magnitude as a rate and the direction as a bearing. A large magnitude means f changes quickly near the point, so a small step moves you a long way in value; a small one means the landscape is nearly flat there. Since the magnitude has the units of f per unit length, comparing gradients between problems only makes sense when the units match.
A gradient of exactly zero is a critical point, and the calculator flags it. Every directional derivative is zero, there is no steepest direction, and the bearing is undefined rather than zero. First derivatives cannot distinguish a maximum from a minimum from a saddle; that needs the Hessian matrix of second partial derivatives, whose eigenvalue signs give the answer. The one-variable version of the same argument, where the whole story is the sign of f'', is on the critical points calculator.
Use the sign pattern of the components as a quick sanity check. If ∂f/∂x is positive, moving in the +x direction must increase f; evaluate f at a nearby point and confirm it. This catches sign errors faster than re-deriving the partials, and it is the check most people skip.
The chart shows f along the steepest-ascent line through your point. It should be rising as it crosses the origin of that plot, and its slope there should equal the reported magnitude. If it looks flat, the gradient is small; if it looks kinked, the function is not differentiable near the point and the reported gradient is not trustworthy.
Reference: gradients of common scalar fields
| f | ∇f | |∇f| | Note |
|---|---|---|---|
| c (constant) | (0, 0, 0) | 0 | Every point is critical |
| ax + by + cz | (a, b, c) | √(a²+b²+c²) | Constant everywhere; level sets are planes |
| x² + y² | (2x, 2y) | 2√(x²+y²) | Radially outward, steeper further out |
| √(x² + y²) | (x/r, y/r) | 1 | Unit radial vector; undefined at the origin |
| xyz | (yz, xz, xy) | √(y²z²+x²z²+x²y²) | Vanishes on all three coordinate planes |
| 1/r | −r̂/r² | 1/r² | Inverse-square field; the electrostatic potential |
| ln r (in the plane) | r̂/r | 1/r | Potential of a line source |
| e^x sin y | (e^x sin y, e^x cos y) | e^x | A harmonic function: its Laplacian is zero |
The 1/r row is the one to remember: its gradient has magnitude 1/r², which is why an inverse-square force field is the gradient of an inverse-distance potential. Differentiating raises the power of the fall-off by exactly one.
Mistakes and assumptions
- Treating the gradient as a scalar. ∇f is a vector; the scalar rate in a chosen direction is ∇f·u, which is the directional derivative.
- Forgetting to evaluate at the point. ∇f = (2x, 2y) is a vector field; the answer to "the gradient at (3,4)" is the vector (6, 8).
- Using arctan instead of atan2 for the bearing. The single-argument form loses the quadrant, so it cannot tell (3, 4) from (−3, −4).
- Reporting a bearing at a critical point. The zero vector has no direction; the angle is undefined, not zero.
- Assuming the gradient exists wherever the partials do. The identity Dᵤf = ∇f·u requires differentiability, which is strictly stronger than the existence of the partial derivatives.
- Differentiating a non-smooth function numerically. At a corner such as the origin of √(x²+y²), a central difference returns a finite number for a gradient that does not exist.
- Expecting the gradient to point at the maximum. It points along the locally steepest ascent, which is generally not towards any particular distant point. Follow it and the direction changes at every step.
Tangent planes and Lagrange multipliers, both for free
Because ∇f is normal to the level set, the tangent plane to f(x, y, z) = c at P is simply ∇f(P)·(r − P) = 0 — no implicit differentiation required. The same fact underlies Lagrange multipliers: at a constrained optimum the level sets of objective and constraint touch, so their gradients are parallel and ∇f = λ∇g. Compute both gradients here and compare their directions to check a candidate solution.
The gradient among the other operators
The gradient is the first of the three operators built from ∇. Applied to a scalar it produces a vector field; the divergence takes a vector field back to a scalar, and the curl takes a vector field to another vector field. Composing gradient with divergence gives the Laplacian ∇²f = ∇·(∇f), which governs steady-state heat, electrostatics and every equilibrium problem; composing gradient with curl gives identically zero, which is the test for whether a vector field is itself a gradient.
That last identity is the practical link between these pages. A field with zero curl on a simply connected region is the gradient of something, and the curl calculator will reconstruct that potential for you. Conversely, entering the components of any gradient into the curl calculator returns zero to within rounding — a good way to check a gradient you have computed by hand.
In optimisation, the gradient is the entire first-order information available, and gradient descent is the algorithm that uses only that. Its weakness is visible in the reference table above: for x² + y² the gradient magnitude grows with distance from the minimum, so a fixed step size is too large far away and too small close in. Methods that use second derivatives — Newton's method, quasi-Newton methods — exist to fix exactly that.
The step-size and cancellation issues in every partial derivative here are the ones discussed on the numerical derivative calculator. And if what you want is not the steepest direction but the rate along a direction you have already chosen, that is the directional derivative calculator, which is this page's output dotted with your unit vector.
