In [3]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
f = lambda a: (a-5)**2+3
a = np.arange(0,10,0.1)
y = f(a)
In [5]:
plt.plot(a, y, "k-");
In [16]:
def derivative(f, a, eps=1e-3):
    return (f(a+eps) - f(a-eps)) / (2*eps)
In [17]:
ai = 8
for _ in range(10):
    ai = ai - 0.2 * derivative(f, ai)
    print("%.2f" % ai)
6.80
6.08
5.65
5.39
5.23
5.14
5.08
5.05
5.03
5.02