Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

The Derivative as a Limit

~10 mincode completion

A derivative is a rate: how fast the output moves when you nudge the input.

You cannot take a limit on a computer, so you pick a small and evaluate the fraction. But there are two ways to do it, and the choice matters more than it looks:

The central difference looks the same distance either side and is dramatically more accurate — with the forward difference is right to about 5 digits, the central one to about 10. It is what every gradient checker uses.

f(x) = x^2,  f'(x) = 2x,  at x = 3 the answer is 6

forward, h=1e-5: ((3.00001)^2 - 3^2)/1e-5     = 6.00001
central, h=1e-5: ((3.00001)^2 - (2.99999)^2)/2e-5 = 6.00000

Do not make tiny to compensate. Below about the two function values agree to within floating-point noise, you subtract two nearly equal numbers, and the accuracy collapses. Around is the sweet spot.

Your task:

Implement central_difference(coeffs, x, h), where coeffs describes a polynomial and the derivative is estimated at x.

coeffs[i] is the coefficient of , so [1, 0, 2] means . Evaluate with np.polyval(coeffs[::-1], x), which expects the highest power first.

Example Tests

x squared has slope 2x, which is 6 at x = 3

Input: {"h":0.00001,"x":3,"coeffs":[0,0,1]}

Expected: 6

A straight line has the same slope everywhere

Input: {"h":0.00001,"x":100,"coeffs":[4,2]}

Expected: 2

A constant does not change, so its derivative is zero

Input: {"h":0.00001,"x":2,"coeffs":[7]}

Expected: 0

Python
import numpy as np


def central_difference(coeffs, x, h):
    """
    Numerical derivative of a polynomial by central difference.

    Args:
        coeffs: coefficients low power first, so [1, 0, 2] is 1 + 2x^2
        x:      the point to differentiate at
        h:      the step size

    Returns:
        float: (f(x+h) - f(x-h)) / (2h)
    """
    # YOUR CODE HERE
    pass
Loading docs…