All problems
Numerical Gradient
Problem
Compute the numerical gradient of f(x) = sum(x_i^2) at a given point using central differences with h = 1e-6.
Returns a list grad where grad[i] = (f(point + h*e_i) - f(point - h*e_i)) / (2h).
Hint
For each dimension i, perturb only that coordinate by ±h.
Test cases
case 1
in: [[1,2,3]]
expected: [2,4,6]
case 2
in: [[0,0]]
expected: [0,0]
case 3
in: [[5]]
expected: [10]
case 4
in: [[1,1,1,1]]
expected: [2,2,2,2]
Starter codePython
def numerical_gradient(point, h=1e-6):
# f(x) = sum(x_i ** 2)
# Your code here
pass4 test cases · run your code against them in the editor