%% Loan Computations
%% Kurt Bryan and SIMIODE
% This script illustrates loan payment computations for the "Money Matters" project in Chapter 2.
% First, choose interest rate "r", initial borrowed amount "p0", monthly payment/investment "b"

r = 0.03; %Interest rate
payments = 180; %Number of payments
p0 = 250000; %Initial loan balance
b = 1726.45; %Monthly payment amount
p = zeros(payments,1); %Array to hold monthly balance, Matlab indexes from 1

% Loop over months, store balance in array "p".

fprintf("Month %d Balance %.2f\n", 0, p0);
p(1) = (1 + r/12)*p0 - b;
fprintf("Month %d Interest %.2f Balance %.2f\n", 1, r*p0/12, p(1));
for k=2:payments
    p(k) = (1 + r/12)*p(k-1) - b;
    fprintf("Month %d Interest %.2f Balance %.2f\n", k, r*p(k-1)/12, p(k));
end