Compound interest and a mortgage Open ↗
Code
% Saving: 200 a month at 5 % a year, compounded monthly, for 30 years
r = 0.05/12; n = 0:360;
saved = 200 * ((1 + r).^n - 1) / r;
paidIn = 200 * n;
% Borrowing: 300 000 over 30 years at 4 % — the annuity formula gives a fixed monthly payment
P = 300000; i = 0.04/12; N = 360;
pay = P * i / (1 - (1 + i)^-N);
balance = P * (1 + i).^n - pay * ((1 + i).^n - 1) / i;
interest = i * balance(1:end-1); % interest part of each payment
subplot(2, 1, 1)
area(n/12, saved), hold on, plot(n/12, paidIn, 'k--', 'LineWidth', 1.5), hold off
title(sprintf('Saving 200/month at 5%%: %.0f after 30 years (%.0f paid in)', saved(end), paidIn(end)))
xlabel('years'), legend('balance', 'paid in')
subplot(2, 1, 2)
area(n(2:end)/12, [interest; pay - interest]')
title(sprintf('Mortgage: %.2f per month, %.0f interest in total', pay, sum(interest)))
xlabel('years'), legend('interest', 'repayment')