VOOZH about

URL: https://www.geeksforgeeks.org/data-science/gradient/

⇱ Gradient - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Gradient

Last Updated : 25 Oct, 2025

The gradient is a fundamental concept in calculus that extends the idea of a derivative to multiple dimensions. It plays an important role in vector calculus, optimization, machine learning, and physics. The gradient of a function provides the direction of the steepest ascent, making it essential in areas such as gradient descent in machine learning and optimization problems.

Mathematical Definition

Given a scalar function of multiple variables, the gradient is defined as a vector of its partial derivatives:

where each component represents the rate of change of with respect to .

Gradient in Two and Three Dimensions

For a function f(x,y), the gradient is:

For a function f(x,y,z), the gradient is:

Geometric Interpretation

1. Direction of Steepest Ascent: The gradient vector points in the direction where the function increases most rapidly.

2. Magnitude Represents Rate of Change: The length ∥∇f∥ indicates how steep the function is in that direction.

3. Gradient Perpendicular to Level Curves: If f(x,y) defines a surface, its gradient at a point is perpendicular to the level curves f(x,y)=c, where c is a constant.

Numerical Example

Consider the function:

Step 1: Compute the Gradient

The partial derivatives are:

Thus, the gradient is:

Step 2: Evaluate at a Point

At (x,y)=(1,2):

This means the function increases most rapidly in the direction (2,12).

Python Implementation

We can compute the gradient using SymPy (for symbolic differentiation) and NumPy (for numerical computation).

1. Computing the Gradient Symbolically

Output:

Gradient: [2*x, 6*y]

2. Evaluating the Gradient at (1,2)

Output:

Gradient at (1,2): [2, 12]

3. Visualizing the Gradient Field

Output:

👁 Screenshot-from-2025-03-08-06-02-43-min
Visualizing Gradient Field

Applications of the Gradient

1. Optimization (Gradient Descent)

In machine learning, the gradient guides gradient descent, an optimization algorithm used to minimize loss functions. The update rule is:

where:

  • 𝜃 are the parameters
  • 𝛼 is the learning rate

2. Physics (Electric and Gravitational Fields)

In electromagnetism and gravity, the gradient of a potential function gives the field direction:


where 𝑉 is electric potential and 𝑈 is gravitational potential.

3. Computer Vision and Image Processing

The gradient is used for edge detection in images. Operators like Sobel filters compute image gradients to highlight edges.

4. Robotics and Navigation

Robots use gradient-based path planning to navigate toward a goal while avoiding obstacles.

Comment
Article Tags:
Article Tags:

Explore