#! /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): the calculated value does not perfectly agree with the actual value as we are using a mathematical approximation for the limit as x -> 0. part (b): The accuracy get better because the mathematical approximation improves for smaller delta, but it eventually gets worst due to computational round off and subtractive cancellation errors. 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 = 1 # for x =1, df(x=1)/dx = 1 print("delta","df/dx",sep='\t') for i in range(2,20,2): delta = 10**(-i) print( delta, derivative(f, x, delta),sep='\t')