#! /usr/bin/env python3 # def humps_derivative ( ): from humps import humps_fun, humps_deriv import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'humps_derivative():' ) print ( ' Estimate the derivative of the humps function' ) print ( ' Use user-written "derivative()" estimator.' ) x = np.linspace ( 0.0, 2.0, 11 ) dx = 1.0E-05 y = derivative ( humps_fun, x, dx ) x2 = np.linspace ( 0.0, 2.0, 201 ) y2 = humps_deriv ( x2 ) plt.plot ( x, y, 'ro' ) plt.plot ( x2, y2, 'b-' ) plt.legend ( [ 'Estimate', 'Exact' ] ) filename = 'humps_derivative.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) return def derivative ( f, x, dx ): dfdx = ( f(x+dx) - f(x-dx) ) / 2.0 / dx return dfdx if ( __name__ == "__main__" ): humps_derivative ( )