Skip to content
DeepForgeby svx
All problems
MediumCalculusca-291

Five-Point Stencil Derivative

Problem

The five-point central stencil approximates the first derivative with error O(h^4):

f'(x) ≈ (-f(x + 2h) + 8 f(x + h) - 8 f(x - h) + f(x - 2h)) / (12 h)

Given polynomial coefficients, x, and h (default 1e-3), return the estimate.

Hint

The weights -1, 8, -8, 1 over a span of 2h each side give fourth-order accuracy.

Test cases

case 1
in: [[1,2,3],2]
expected: 13.99999999999757
case 2
in: [[0,1,0,0],4]
expected: 1.00000000000026
case 3
in: [[0,0,5],-1]
expected: -9.999999999998824
Starter codePython
def five_point_stencil_derivative(coeffs, x, h=1e-3):
    # Your code here
    pass

3 test cases · run your code against them in the editor