Gradient Calculator

  • Free
  • No sign-up
  • Works on phones
  • Shows the working
Multivariable#22

Gradient & Directional Derivative

\(\nabla f\) and \(D_{\mathbf{u}} f = \nabla f \cdot \hat{\mathbf{u}}\) at a point.

The gradient calculator finds \( \nabla f \) for a function of two variables at a point, along with its length. It also works as a directional derivative calculator: give it a direction vector and it returns \( D_{\mathbf u} f \), the rate at which \( f \) changes as you move that way.

It’s aimed at multivariable calculus students working on gradients, steepest ascent and level curves. Also, it is free and needs no sign-up.

Gradient Calculator: Level curves of f(x, y) = x² + y² are circles; the gradient ∇f = ⟨2x, 2y⟩ at (1, 1) points straight…
Level curves of f(x, y) = x² + y² are circles; the gradient ∇f = ⟨2x, 2y⟩ at (1, 1) points straight outward, the direction of steepest increase.

How to use the gradient calculator

  1. Type \( f(x, y) \). Use ^ for powers and sqrt(), sin(), ln(), e^(x) and pi for functions. Implicit multiplication like 3x y works.
  2. Enter the point \( (x_0, y_0) \).
  3. Enter a direction \( \langle u_1, u_2 \rangle \). It doesn’t have to be a unit vector, because the calculator normalizes it for you. It just can’t be the zero vector.
  4. Press Compute gradient. As a result, the output lists \( \nabla f \), \( |\nabla f| \), the unit vector \( \hat{\mathbf u} \) and the directional derivative \( D_{\hat{\mathbf u}} f \).

Formula

The gradient collects the two partial derivatives into a vector:

An open book with pencils and papers, suggesting study or research.

$$\nabla f(x, y) = \left\langle f_x(x, y),\ f_y(x, y) \right\rangle$$

The directional derivative in the direction of a vector \( \mathbf u \) is the dot product of the gradient with the unit vector \( \hat{\mathbf u} = \frac{\mathbf u}{|\mathbf u|} \):

$$D_{\hat{\mathbf u}} f = \nabla f \cdot \hat{\mathbf u} = f_x\,\hat u_1 + f_y\,\hat u_2$$

Since \( \nabla f \cdot \hat{\mathbf u} = |\nabla f|\cos\theta \), the largest possible rate is \( |\nabla f| \). This is in the direction of \( \nabla f \) itself. The rate is \( -|\nabla f| \) in the opposite direction. It is 0 at right angles to it. The gradient calculator computes \( f_x \) and \( f_y \) symbolically. It evaluates them at the point. Then it applies these formulas.

Worked example

Example 1: gradient and directional derivative. Let \( f(x, y) = x^2y - y^3 \). Find \( \nabla f(2, 1) \) and the rate of change of \( f \) at \( (2, 1) \) in the direction \( \langle 3, -4 \rangle \).

A young girl writing with a pen, focused on her work.

First the partials: \( f_x = 2xy \) and \( f_y = x^2 - 3y^2 \). At \( (2, 1) \):

$$\nabla f(2, 1) = \langle 4,\ 4 - 3 \rangle = \langle 4, 1 \rangle, \qquad |\nabla f| = \sqrt{17} \approx 4.1231$$

The direction vector has length \( \sqrt{9 + 16} = 5 \), so \( \hat{\mathbf u} = \langle 0.6, -0.8 \rangle \). Then

$$D_{\hat{\mathbf u}} f = 4(0.6) + 1(-0.8) = 1.6 = \tfrac85.$$

Moving from \( (2, 1) \) toward \( \langle 3, -4 \rangle \), \( f \) increases at 1.6 units per unit of distance. For example, this is the example loaded in the calculator above.

Example 2: fastest and flat directions. Same function and point. Where does \( f \) increase fastest, and in which direction does it not change at all?

  • Steepest ascent is along \( \nabla f = \langle 4, 1 \rangle \), and the rate there is \( \sqrt{17} \). Enter \( u_1 = 4 \), \( u_2 = 1 \) and the calculator shows \( D_{\hat{\mathbf u}} f = \sqrt{17} \). The unit vector is about \( \langle 0.9701, 0.2425 \rangle \).
  • Zero rate happens perpendicular to the gradient, for example along \( \langle 1, -4 \rangle \): \( \frac{4(1) + 1(-4)}{\sqrt{17}} = 0 \). That direction is tangent to the level curve \( x^2y - y^3 = 3 \) through \( (2, 1) \).

How to read the output

  • \( \nabla f \) is a vector at the point, not a number. It points uphill, in the direction of steepest increase.
  • \( |\nabla f| \) is the steepest slope available at that point. When it is 0, you are at a critical point, which is where optimization problems look for maxima and minima.
  • \( \hat{\mathbf u} \) is your direction scaled to length 1. If you forget to normalize by hand, your directional derivative will be off by a factor of \( |\mathbf u| \), which is the most common mistake on exams.
  • \( D_{\hat{\mathbf u}} f \) is a signed number. Positive means \( f \) increases in that direction, negative means it decreases.
  • The gradient is perpendicular to level curves. Contour lines of \( f \) cross \( \nabla f \) at right angles, which is why it’s used for normal vectors and tangent lines to curves \( f(x, y) = k \).

For the derivation of \( D_{\hat{\mathbf u}} f = \nabla f \cdot \hat{\mathbf u} \) and more practice, see the guide to the gradient and directional derivative. If you only need \( f_x \) and \( f_y \), start with partial derivatives or the partial derivative calculator.

Magnifying glass over a graph and text in a book

Worked example: steepest ascent and directional derivatives

For \( f(x, y) = x^2 + y^2 \), \( \nabla f = (2x, 2y) \). At \( (1, 2) \), \( \nabla f = (2, 4) \):

  • The function increases fastest in the direction \( (2, 4) \), at a rate equal to its length, \( \sqrt{20} \approx 4.47 \).
  • In the unit direction \( \mathbf{u} = (0.6, 0.8) \), the rate of change is \( \nabla f \cdot \mathbf{u} = 1.2 + 3.2 = 4.4 \).
  • In the direction \( -\nabla f \), it decreases fastest.

Gradients and contour lines

The gradient is always perpendicular to the level curves (contour lines) of a function. For example, on a hiking map, the gradient points straight uphill, across the contour lines, and its length shows how steep the slope is.

Gradient descent

Many machine-learning models are trained by repeatedly stepping in the direction of \( -\nabla f \) to reduce an error function. Each step is \( \mathbf{x}_{\text{new}} = \mathbf{x} - \eta\nabla f(\mathbf{x}) \), where \( \eta \) is the step size (learning rate).

Practice problems

  1. Find \( \nabla f \) for \( f(x, y) = 3x^2y \) at \( (1, 2) \).
  2. In which direction does \( f(x, y) = x + 2y \) increase fastest?

Answers: 1. \( (6xy, 3x^2) = (12, 3) \). 2. In the direction \( (1, 2) \), everywhere.

Detailed Calculator Usage Steps

To effectively use this gradient calculator, follow these specific steps. First, accurately input your function f(x, y) into the designated field. Use 'x' and 'y' for variables. Remember to use standard mathematical notation; for instance, 'x^2' for x squared and 'sqrt(x)' for the square root of x. Implicit multiplication, like '3xy', is generally understood.

Next, enter the coordinates of your specific point (x0, y0). Type the numerical value for x0 in the first point field and y0 in the second. For example, enter '2' and '1' for the point (2, 1).

Then, specify the direction vector. Input the x-component (u1) and y-component (u2) into their respective fields. This vector indicates the path you are interested in. Finally, click the 'Compute gradient' button. The calculator will then process your inputs and display the results clearly.

Verifying Your Gradient Calculator Results

After computation, always check the output. The gradient vector should point in the direction of steepest increase for your function. Visually, imagine the level curves of your function. The gradient calculator output must be perpendicular to these curves at the given point. Its length indicates the steepness.

Consider the sign of the directional derivative. A positive value means the function increases as you move in the specified direction. Conversely, a negative value indicates a decrease. A zero value suggests you are moving along a level curve, where the function's value remains constant. This quick assessment helps confirm the calculator's output makes sense for your problem.

Common Input Mistakes

Users sometimes make small errors when entering functions. A frequent mistake is forgetting to use an asterisk for multiplication, like typing '2x' instead of '2*x' if implicit multiplication is not supported (though this gradient calculator handles '2x'). Always enclose arguments for functions like 'sin' or 'ln' in parentheses, such as 'sin(x)' not 'sinx'.

Another common issue involves exponents. Ensure you use the caret symbol '^' correctly, for instance, 'x^3' for x cubed. Mistyping the point coordinates or the direction vector components can also lead to incorrect results. Double-check all numerical entries before computing. Correct input ensures accurate calculations and reliable output.

Related guides

Further reading

FAQ

What is the difference between the gradient and the directional derivative?

The gradient is a vector made of the partial derivatives. However, the directional derivative is a single number: the gradient dotted with a unit direction vector.

Do I need to enter a unit vector?

No. Enter any nonzero direction, such as \( \langle 3, -4 \rangle \). The calculator divides by its length before taking the dot product and shows the unit vector it used.

What is the maximum value of the directional derivative?

It is \( |\nabla f| \), reached when you move in the direction of the gradient. The minimum is \( -|\nabla f| \), in the opposite direction.

Does the gradient calculator work in three dimensions?

No. It handles functions \( f(x, y) \) of two variables with a two-component direction vector.

Embed this calculator on your website

Teachers, tutors and bloggers are welcome to use this calculator for free. Copy the code below and paste it into any page (in WordPress, use a "Custom HTML" block). The small script makes the calculator grow to fit its answer; if your site removes scripts, it still works at a fixed height. Please keep the credit link.

More calculators

See all calculators