"""Calculate derivatives using centered differences and Richardson extrapolation.""" import math # ==================================================================== # Driver program: Calculates first- and second-order derivatives # Method: centered differences with Richardson extrapolation # # The derivatives are first calculated with step sizes h and h/2. # Richardson extrapolation then removes the leading O(h**2) error. # # Alexander Godunov, September 2026 # ==================================================================== def main(): # ---------------------------------------------------------------- # Step 1: specify the evaluation point and initial step size # # The example uses # # f(x) = ln(x), # # for which f'(x) = 1/x and f''(x) = -1/x**2. # ---------------------------------------------------------------- x = 2.0 h = 0.2 # ---------------------------------------------------------------- # Step 2: calculate the first- and second-order derivatives # ---------------------------------------------------------------- print("Centered differences with Richardson extrapolation") print(f"f(x) = ln(x), x = {x:7.3f}, h = {h:7.3f}\n") print(" order exact D(h) D(h/2)" " Richardson estimated error" " actual error") for derivative_order in range(1, 3): (derivative_h, derivative_h2, derivative_richardson, error_estimate, status) = richardson_derivative( test_function, x, h, derivative_order) if status != 0: print(f"Derivative of order {derivative_order} could not be " f"evaluated; status = {status}") continue if derivative_order == 1: exact = 1.0/x else: exact = -1.0/x**2 actual_error = abs(derivative_richardson-exact) print(f"{derivative_order:6d}{exact:17.8e}" f"{derivative_h:17.8e}{derivative_h2:17.8e}" f"{derivative_richardson:17.8e}{error_estimate:17.8e}" f"{actual_error:17.8e}") def richardson_derivative(func, x, h, derivative_order): """Calculate a derivative using one Richardson extrapolation.""" # ================================================================= # Input ... # func - function to be differentiated # x - point where the derivative is required # h - initial finite-difference step size # derivative_order - order of the derivative (1 or 2) # # Output ... # derivative_h - centered difference calculated with h # derivative_h2 - centered difference calculated with h/2 # derivative_richardson - Richardson-extrapolated derivative # error_estimate - magnitude of the Richardson correction # status - 0: success # 1: derivative_order is not 1 or 2 # 2: h is not positive # # A centered difference has an error proportional to h**2. For # step refinement by a factor of 2, Richardson extrapolation gives # # D_R = D(h/2) + [D(h/2)-D(h)]/(2**2-1). # ================================================================= derivative_h = 0.0 derivative_h2 = 0.0 derivative_richardson = 0.0 error_estimate = 0.0 status = 0 # Check the order of the requested derivative if derivative_order < 1 or derivative_order > 2: status = 1 return (derivative_h, derivative_h2, derivative_richardson, error_estimate, status) # A positive finite-difference step is required if h <= 0.0: status = 2 return (derivative_h, derivative_h2, derivative_richardson, error_estimate, status) # Calculate the centered differences using h and h/2 derivative_h = centered_difference(func, x, h, derivative_order) derivative_h2 = centered_difference(func, x, h/2.0, derivative_order) # Remove the leading O(h**2) error correction = (derivative_h2-derivative_h)/3.0 derivative_richardson = derivative_h2 + correction # The Richardson correction provides an error estimate error_estimate = abs(correction) return (derivative_h, derivative_h2, derivative_richardson, error_estimate, status) def centered_difference(func, x, h, derivative_order): """Calculate a derivative using a second-order centered difference.""" if derivative_order == 1: return (func(x+h)-func(x-h))/(2.0*h) return (func(x+h)-2.0*func(x)+func(x-h))/h**2 def test_function(x): """Function used in the driver program.""" # Replace this expression with the function required in a practical # calculation. return math.log(x) if __name__ == "__main__": main()