""" Exercise A.7 from "A primer on..." Fix a bug related to the indices of the difference equation. The bug was in the line defining the for loop, which original read: for i in years[1:]: This gives an index error since it will run to the last element of years, and then we use the index [i+1] inside the loop. The code below has been corrected, so it starts at zero and runs to the second-to-last element of the years array. """ import numpy as np import matplotlib.pyplot as plt x0 = 100 # initial amount p = 5 # interest rate N = 40 # number of years years = range(N+1) x = np.zeros(N+1) # Compute solution x[0] = x0 for i in years[:-1]: x[i+1] = x[i] + (p/100.0) * x[i] plt.plot(years, x, 'ro') plt.xlabel('years') plt.ylabel('amount') plt.show()