How to Compute Derivative Using Numpy?

Calculus, the study of continuous change, is a fundamental subject in mathematics that has numerous applications in fields ranging from physics to economics. One of the key concepts in calculus is derivative, which measures the instantaneous rate of change of a function at a given point.

The derivative of function f(x) at point x is mathematically defined as ?

f'(a) = lim(h -> 0) [(f(a+h) - f(a))/h]

However, computing derivatives by hand can be timeconsuming and errorprone. NumPy provides the gradient() function to calculate derivatives quickly and accurately for both onedimensional and multidimensional functions.

Basic Steps for Computing Derivatives

Step 1: Define the Function

First, define the function you want to differentiate. Let's use f(x) = x² as an example ?

import numpy as np

def f(x):
    return x**2

# Test the function
print(f(3))
9

Step 2: Define the Domain

Specify the x values where you want to compute the derivative. For a single point ?

import numpy as np

def f(x):
    return x**2

# Single point
x = np.array([2.0])
y = f(x)
derivative = np.gradient(y, x)
print(f"Derivative at x=2: {derivative[0]}")
Derivative at x=2: 4.0

Step 3: Compute Over a Range

To compute derivatives over multiple points, use an array of x values ?

import numpy as np

def f(x):
    return x**2

# Multiple points
x = np.array([0, 1, 2, 3, 4])
y = f(x)
derivative = np.gradient(y, x)

print("x values:", x)
print("f(x) values:", y)
print("Derivatives:", derivative)
x values: [0 1 2 3 4]
f(x) values: [ 0  1  4  9 16]
Derivatives: [1. 2. 4. 6. 7.]

Multidimensional Derivatives

NumPy's gradient() function can compute partial derivatives of multidimensional functions. For function f(x, y) = x² + y², we calculate partial derivatives with respect to both variables ?

import numpy as np

# Create a 2D grid
x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
X, Y = np.meshgrid(x, y)

# Define function f(x,y) = x^2 + y^2
Z = X**2 + Y**2

# Compute partial derivatives
dy, dx = np.gradient(Z, x, y)

print("Function values Z:")
print(Z)
print("\nPartial derivative with respect to x:")
print(dx)
print("\nPartial derivative with respect to y:")  
print(dy)
Function values Z:
[[ 2  5 10]
 [ 5  8 13]
 [10 13 18]]

Partial derivative with respect to x:
[[2. 2. 2.]
 [4. 4. 4.]
 [6. 6. 6.]]

Partial derivative with respect to y:
[[2. 4. 6.]
 [2. 4. 6.]
 [2. 4. 6.]]

Comparison with Analytical Solutions

Function Analytical Derivative NumPy Result
f(x) = x² f'(x) = 2x Very close approximation
f(x) = sin(x) f'(x) = cos(x) Good for smooth functions
f(x,y) = x² + y² ?f/?x = 2x, ?f/?y = 2y Accurate for polynomial functions

Alternative Methods

While NumPy's gradient() is convenient, other approaches include ?

  • SymPy: Symbolic computation for exact analytical derivatives
  • Finite differences: Manual implementation using difference quotients
  • Automatic differentiation: Libraries like JAX or PyTorch for machine learning

Conclusion

NumPy's gradient() function provides an efficient way to compute numerical derivatives for both single and multivariable functions. It's particularly useful for data analysis and scientific computing where you need quick approximations of derivatives without analytical calculations.

Updated on: 2026-03-27T09:08:37+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements