""" Exercise A.7 from "A primer on..." The exercise illustrates a common problem when solving difference equations, and also similar tasks using for-loops. The equation can be formulated in different ways, as described in the exercise, and we need to take care that the lower and upper bounds of the for-loop match with the formulation inside it. This code is the working code, after modifying the for-loop bounds. """ import matplotlib.pyplot as plt import numpy as np x0 = 100 # initial amount p = 5 # interest rate N = 4 # number of years years = list(range(N+1)) x = np.zeros(len(years)) # Compute solution x[0] = x0 for i in years[:-1]: #modified from '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()