All problems
Hessian in 1D Finite Difference
Problem
The five-point central stencil for the second derivative is
f''(x) ≈ (-f(x+2h) + 16*f(x+h) - 30*f(x) + 16*f(x-h) - f(x-2h)) / (12*h^2)
which is exact for polynomials up to degree 5. Apply it to f(x) = sum(coeffs[i] * x^i) at x with h = 1e-3.
Hint
The weights -1, 16, -30, 16, -1 over 12 h^2 give fourth-order accuracy.
Test cases
case 1
in: [[0,0,1],1]
expected: 2
case 2
in: [[0,0,0,0,1],1]
expected: 12
case 3
in: [[0,0,0,1],2]
expected: 12
case 4
in: [[5],3]
expected: 0
Starter codePython
def hessian_1d_5point(coeffs, x, h=1e-3):
# Your code here
pass4 test cases · run your code against them in the editor