#! /usr/bin/env python """ deriv.py is a program which calculate a simple derivative for f(x) = x(x-1) using a limit approximation for a small delta rather than the lim_delta->0 Results are print out for f(x=1) and variuos values of delta . Reslults: part (a): df/dx = 2x -1 The calculated value does not perfectly agree with the actual value as we are using a mathematical approximation for the limit as x -> 0. Paul Eugenio PHZ4151C 13 Feb 2019 """ from __future__ import division, print_function import numpy as np def derivative(fun, x, delta): """ Numerical Derivative also know as the Forward Difference defined by (f(x + dx) - f(x)) / dx """ return (fun(x + delta) - f(x))/delta # main # def f(x): """ program test function f(x) = x**2 - x """ return x*x - x x, delta = 1, 1e-2 # for x =1, df(x=1)/dx = 1 print("f(x) = x*x - x") print("At x = 1, df/dx = {0} whereas the correct value is 1".format( derivative(f, x, delta) ) )