PIXELBANKv8.2.1
Menu

Numerical Gradient

Implement a function to compute the numerical gradient of a given function at a specific point using finite differences. This task is essential in calculus and optimization when analytical gradients are not available.

The concept of gradients is crucial in understanding the rate of change of a function. In mathematics, the derivative of a function ff at a point xx represents the rate of change of the function with respect to xx. The derivative can be approximated using the central difference formula, which is a fundamental concept in numerical analysis.

To approximate the derivative, we can follow these steps:

  1. Evaluate the function at x+hx + h and xhx - h.
  2. Calculate the difference between these two function values.
  3. Divide the result by 2h2h to obtain the approximate derivative.
f(x)f(x+h)f(xh)2hf'(x) \approx \frac{f(x + h) - f(x - h)}{2h}

This technique is widely used in machine learning for training models when analytical gradients are unavailable.

Example:

Input:
numerical_gradient(lambda x: x**2, 3.0)
Output:
6.0
Reasoning:

d/dx(x²) = 2x, at x=3: gradient = 6

Constraints:

  • Use h = 1e-5 for the finite difference
  • The function f takes a single float and returns a float
  • Return the gradient rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.