""" Exercise A.4 from "A primer on..." Solve a system of difference equations describing the remaining value of a loan (x) and the monthly payments (y). As specified in the exercise, the solution is implemented as a function, which takes the relevant model parameters as arguments and returns the solution. """ import numpy as np import matplotlib.pyplot as plt def loan(L, p, N): x = np.zeros(N + 1) y = np.zeros(N + 1) months = range(N + 1) x[0] = L y[0] = 0 for n in months[1:]: y[n] = p / (100 * 12) * x[n-1] + L/N x[n] = x[n-1] + p / (100 * 12) * x[n-1] - y[n] return months, x, y months, x, y = loan(5e6, 5, 25 * 12) #plt.plot(months, x) plt.plot(months, y) plt.show()