Compute derivative of $$ f(x) = \sin(x) $$ at $x=2\pi$ using finite difference $$ \frac{f(x+h) - f(x)}{h} $$ for $h=10^{-1},10^{-2},\ldots,10^{-14}$.
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
from numpy import sin,arange,zeros,pi,abs
from matplotlib.pyplot import loglog,xlabel,ylabel
def f(x):
return sin(x)
h = 10.0**arange(-1,-15,-1)
df= zeros(len(h))
x = 2.0*pi
f0= f(x)
for i in range(len(h)):
f1 = f(x+h[i])
df[i] = (f1 - f0)/h[i]
loglog(h,abs(df-1.0),'o-')
xlabel('h')
ylabel('Error in derivative');