Gradient Calculator: ∇f, Directional Derivatives and Tangent Planes
By FindMySum · Last reviewed
A gradient calculator finds ∇f, the vector of partial derivatives of a function of several variables, and tells you what it means at a point: how steep the function is, which way it climbs fastest, how fast it changes in any direction you choose, and the tangent plane that best approximates it there. Type a function of x and y (or x, y and z), a point and a direction. The derivatives are found symbolically, so you see the formulas as well as the numbers.
What the gradient is
For f(x, y), the gradient is
∇f = (∂f/∂x, ∂f/∂y).
Each partial derivative is an ordinary derivative with the other variable held constant. In three variables, add ∂f/∂z. At any point, ∇f:
- points in the direction of steepest increase of f;
- has length |∇f| equal to that steepest rate of increase;
- is perpendicular to the level curve (or level surface) through the point.
Worked example with the default function
f(x, y) = x²y + 3xy², at the point (1, 2).
- Partial with respect to x (y constant): ∂f/∂x = 2xy + 3y².
- Partial with respect to y (x constant): ∂f/∂y = x² + 6xy.
- At (1, 2): ∂f/∂x = 2(1)(2) + 3(4) = 16 and ∂f/∂y = 1 + 6(1)(2) = 13, so ∇f(1, 2) = (16, 13).
- Magnitude: |∇f| = √(16² + 13²) = √425 ≈ 20.616.
- Direction of steepest ascent: (16, 13) ÷ 20.616 ≈ (0.776, 0.631).
So at (1, 2), f increases fastest towards (0.776, 0.631), at about 20.6 units of f per unit of distance.
Directional derivatives
The rate of change of f in a direction u is the dot product of the gradient with the unit vector in that direction:
D_u f = ∇f · (u ÷ |u|).
For the default direction (3, 4), the unit vector is (0.6, 0.8), and
D_u f = 16 × 0.6 + 13 × 0.8 = 9.6 + 10.4 = 20.
That is slightly less than the maximum 20.616 because (0.6, 0.8) points a little away from the gradient. In general D_u f = |∇f| cos θ, where θ is the angle between u and ∇f: largest along ∇f, zero along a level curve, most negative straight against ∇f.
The tangent plane
Near the point, z = f(x, y) looks like a plane:
z = f(x₀, y₀) + f_x(x₀, y₀)(x − x₀) + f_y(x₀, y₀)(y − y₀).
For the example, f(1, 2) = 2 + 12 = 14, so the tangent plane is
z = 14 + 16(x − 1) + 13(y − 2).
This plane is also the linear approximation of f: at (1.1, 2.1) it predicts 14 + 1.6 + 1.3 = 16.9, close to the true value 17.09.
For a function of three variables, the gradient is normal to the level surface f(x, y, z) = f(P), and that surface's tangent plane is f_x(P)(x − x₀) + f_y(P)(y − y₀) + f_z(P)(z − z₀) = 0. For f = xyz at (1, 2, 3), ∇f = (6, 3, 2) and the plane is 6(x − 1) + 3(y − 2) + 2(z − 3) = 0.
Critical points
Where ∇f = 0, there is no direction of steepest ascent: the point is a local maximum, a local minimum or a saddle. For f = x² + y², the gradient (2x, 2y) is zero only at the origin, the bottom of the bowl. The calculator reports this case; deciding which kind of critical point it is needs second derivatives.
Typing functions
- Use
*or juxtaposition for multiplication (2xmeans 2·x),^for powers, and brackets freely. - Functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, exp, ln (natural log), log (base 10), sqrt, abs.
- Constants: pi and e. Any other letter is treated as a constant and the gradient is shown as a formula, but it can't be evaluated until you replace the letter with a number.
Where gradients are used
Gradients are everywhere in science and engineering. Heat flows down the temperature gradient (Fourier's law), electric fields are the negative gradient of electric potential, and optimisation algorithms from linear regression to deep learning follow the gradient downhill to a minimum.
More questions
What is the difference between a gradient and a derivative?
A derivative is the rate of change of a function of one variable, a single number at each point. The gradient is its extension to several variables: a vector made of all the partial derivatives. Each partial derivative is the ordinary derivative along one axis with the other variables held fixed.
How is the gradient used in machine learning?
Training a model means minimising a loss function of many parameters. Gradient descent repeatedly moves the parameters a small step in the direction of −∇(loss), the direction of steepest decrease. Backpropagation is an efficient way of computing that gradient for a neural network.
What is the gradient on a topographic map?
If f(x, y) is the elevation, the gradient at a point points straight uphill, perpendicular to the contour lines, and its magnitude is the steepness (rise over run). Closely spaced contours mean a large gradient. Water flows in the direction of −∇f.
Can the directional derivative be larger than the magnitude of the gradient?
No. Since D_u f = |∇f| cos θ, where θ is the angle between u and ∇f, the directional derivative is at most |∇f| (when u points along the gradient) and at least −|∇f| (when it points the opposite way). It is zero along level curves, where θ = 90°.
Do I need to use radians?
Yes, for trigonometric functions. The derivative of sin x is cos x only when x is in radians, so the calculator treats every angle as radians. Convert degrees by multiplying by π/180 inside the function, for example sin(x*pi/180).
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.