Formula and Method for the Gradient of a Two-Variable Function
For a function of two variables, f(x, y), the gradient is the vector of its first partial derivatives: ∇f = (∂f/∂x, ∂f/∂y). At any point (x₀, y₀), this vector points in the direction the function increases fastest, and its length is the maximum rate of increase in any direction at that point. This calculator works with a general two-variable quadratic function, f(x, y) = ax² + by² + cxy + dx + ey, which is the standard testbed for gradient problems because every term differentiates cleanly.
How the calculation works
To find ∂f/∂x, differentiate with respect to x while treating y as a constant: ∂f/∂x = 2ax + cy + d. To find ∂f/∂y, differentiate with respect to y while treating x as a constant: ∂f/∂y = 2by + cx + e. Plugging in the point (x₀, y₀) gives the gradient vector ∇f(x₀, y₀) = (2ax₀ + cy₀ + d, 2by₀ + cx₀ + e). The calculator then reports the vector's magnitude, |∇f| = √((∂f/∂x)² + (∂f/∂y)²), and its direction, θ = atan2(∂f/∂y, ∂f/∂x), measured counterclockwise from the positive x-axis.
Common mistakes
- Forgetting the cross term: when a function has an xy term, both partial derivatives pick up a piece of it — ∂(cxy)/∂x = cy and ∂(cxy)/∂y = cx — not just one of them.
- Mixing up gradient with slope: the gradient of a two-variable function is a vector with two components, not a single number like the slope of a line; "steepness" in one particular direction is a different quantity (the directional derivative).
- Assuming a nonzero function value means a nonzero gradient: the gradient depends only on the partial derivatives, so a large f(x, y) can still sit at a flat point where ∇f = (0, 0).
Real-world applications
- Gradient descent in machine learning repeatedly steps opposite the gradient of a loss function to find the input that minimizes it.
- Physics uses the gradient of a potential or temperature field to find the direction of steepest change, such as heat flow or force direction.
- Optimization problems locate candidate maxima, minima, and saddle points by finding where the gradient equals the zero vector.
- Contour and topographic maps use the gradient of an elevation function to show the steepest uphill or downhill direction at any location.