Gradient Vector Calculator (grad f)

Enter a scalar function of two or three variables and a point, and this calculator returns ∇f there — every partial derivative, the magnitude that equals the maximum possible rate of change, the unit vector pointing straight uphill, and the compass bearing of that direction in the plane. The same vector is the normal to the level curve or level surface through the point, so this is also the fastest route to a tangent line or tangent plane.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Scalar function fUse x, y and z. Available: + - * / ^, sin, cos, exp, ln, sqrt, abs, pi, e.x^2 + y^2
Number of variablesWith two variables, z is held at 0 and ∂f/∂z is reported as zero.Two — f(x, y)
x at the pointFirst coordinate of the evaluation point.3
y at the pointSecond coordinate of the evaluation point.4
z at the pointThird coordinate; used only with three variables.0

It returns

  • |∇f| — the maximum rate of change — The steepest slope available at this point, per unit distance travelled.
  • ∂f/∂x
  • ∂f/∂y
  • ∂f/∂z
  • Bearing of steepest ascent in the xy-plane — Measured anticlockwise from the positive x-axis, in the range −180° to 180°.
  • f at the point

The formula

f=(fx,fy,fz)
f(P)(rP)=0

In plain text: ∇f = (∂f/∂x, ∂f/∂y, ∂f/∂z)

  • fA scalar field — one number attached to every point
  • ∇fThe gradient, a vector field pointing in the direction of fastest increase
  • |∇f|The maximum rate of change of f per unit distance at the point
  • ∇f/|∇f|Unit vector of steepest ascent; its negative is steepest descent

The gradient is defined so that the directional derivative in any unit direction u is the dot product ∇f·u. That single property fixes it uniquely.

Updated Category Multivariable & Vector Calculus Verified against published test cases Reading time 11 min

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 as a constant, giving 2xy³, and the y-derivative treats 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.

  1. Partial with respect to x. Hold y fixed: ∂f/∂x = 2x, which at x = 3 is 6.
  2. Partial with respect to y. Hold x fixed: ∂f/∂y = 2y, which at y = 4 is 8.
  3. Assemble. ∇f(3, 4) = (6, 8).
  4. Magnitude. |∇f| = √(36 + 64) = √100 = 10. So the steepest slope at this point is 10 units of f per unit of distance.
  5. Unit direction. (6, 8)/10 = (0.6, 0.8), at a bearing of atan2(8, 6) = 53.13° from the x-axis.
  6. 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

Each is obtained by partial differentiation; r denotes √(x²+y²+z²).
f∇f|∇f|Note
c (constant)(0, 0, 0)0Every 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)1Unit 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̂/r1/rPotential of a line source
e^x sin y(e^x sin y, e^x cos y)e^xA 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.

Frequently asked questions

What does the gradient vector point towards?

The direction of steepest increase of f at that point, and nothing further away than that. It is a local statement: follow the gradient and the direction changes continuously as you move. It does not point at the maximum of the function, and on a curved landscape the path of steepest ascent is generally not a straight line to any particular summit.

What does the magnitude of the gradient mean?

It is the maximum rate of change of f per unit distance at that point — the value of the directional derivative along the gradient itself. If f is elevation in metres and horizontal distance is in metres, a magnitude of 0.1 is a 10% grade. No direction at that point can give a rate larger than this value or smaller than its negative.

Why is the gradient perpendicular to level curves?

Because moving along a level curve leaves f unchanged, so the directional derivative in that direction is zero, so ∇f·u = 0 — which is exactly the statement that ∇f is orthogonal to u. Since that holds for every tangent direction of the level set, the gradient is normal to it. This is what makes it the natural normal vector for a tangent plane.

What happens when the gradient is zero?

You are at a critical point: a local maximum, a local minimum, or a saddle. Every directional derivative vanishes, the surface is momentarily flat in every direction, and there is no direction of steepest ascent, so the bearing is undefined. Classifying the point requires the Hessian matrix of second partial derivatives — positive definite for a minimum, negative definite for a maximum, indefinite for a saddle.

What is the difference between the gradient and the derivative?

In one variable they coincide: ∇f = f'(x), a single number. In several variables the derivative is a linear map that takes a displacement to a change in f, and the gradient is the vector that represents that map through the dot product. The distinction matters when the coordinate system is not orthonormal, where the two objects transform differently.

How do I find the tangent plane to a surface using the gradient?

Write the surface as a level set g(x, y, z) = c, compute ∇g at your point, and the tangent plane is ∇g(P)·(r − P) = 0. For a surface given as z = h(x, y), set g = h(x, y) − z, whose gradient is (hₓ, h_y, −1). No implicit differentiation is needed in either case.

Is the gradient always defined?

No. It requires f to be differentiable at the point, which fails at corners, cusps and discontinuities. The function √(x² + y²) has a perfectly good gradient everywhere except the origin, where the cone has a point. A numerical calculator cannot detect this on its own — it will return a finite number — so check that your function is smooth where you are evaluating it.

Why is my gradient reported as 6.0000000012 instead of 6?

Because the partial derivatives are computed by finite differences rather than symbolically. The method used here is a central difference with Richardson extrapolation, accurate to roughly twelve significant figures on a smooth function, so a residue in the eleventh or twelfth digit is expected. It is not an error in your function or in the arithmetic.

How does gradient descent use this?

It repeatedly steps in the direction −∇f, which is the direction of steepest decrease, by an amount controlled by a learning rate. The whole difficulty is that the gradient is only a first-order local description: too large a step overshoots into a region where the gradient is different, too small a step converges slowly. Second-order methods use curvature information to choose the step, which the gradient alone cannot supply.

References