%% Mortgage Payments
% We want to understand the relationships between the mortgage payment 
% of a fixed rate mortgage, the principal (the amount borrowed), the
% annual interest rate, and the period of the loan.  
% We are going to assume (as is usually the case in the United States)
% that payments are made monthly, even though 
% the interest rate is given as an annual rate.  Let's define
peryear = 1/12; percent = 1/100; 
%%
% So the number of payments on a $30$-year loan is
30*12 
%%
% and an annual percentage rate of, say, 8% comes out to a monthly rate of:
8*percent*peryear 
%%
% Now consider what happens with each monthly payment.  Some of the 
% payment is applied to interest on the outstanding principal amount, 
% $P$, and some of the payment is applied 
% to reduce the principal owed.  The total amount, $R$, of the monthly
% payment remains constant over the life of the loan.  
% So if $J$ denotes the monthly interest rate,
% then the amount applied to reduce the principal is
% $R - JP$, and the new principal after the payment is applied is
%
% $$P-R+JP=P(1+J)-R=Pm-R,$$
%
% where $m = 1 + J$. So a table of the amount of the principal still
% outstanding after $n$ payments is tabulated as follows for a loan of
% initial amount $A$, for $n$ from $0$ to $6$:
syms m J P R A n x y
disp('No. of Payments    Remaining Principal');
P = A; 
for n = 0:6 
    disp([num2str(n), '                  ', char(P)]) 
    P = simplify(-R + P*m); 
end 
%%
% We can write this in a simpler way by noticing that 
% 
% $$ P = Am^n + (\hbox{terms divisible by }R) $$
%
% For example, with $n = 7$ we have:
factor(P - A*m^7)
%%
% But, on the other hand, the quantity inside the parentheses is the sum 
% of a geometric series 
%
% $$ \sum_{k=1}^{n-1}m^k = \frac{m^n-1}{m-1}. $$
%
% So we see that the principal after $n$ payments can be written as  
%
% $$ P = Am^n - R(m^n-1)/(m-1) . $$
%
%%
% Now we can solve for the monthly payment amount $R$ under the assumption 
% that the loan is paid off in $N$ installments, i.e., $P$ is reduced to 
% $0$ after $N$ payments:
syms N;
solve(A*m^N - R*(m^N - 1)/(m - 1), R) 
R = subs(ans, m, J + 1) 
%%
% For example, with an initial loan amount $A =$ \$150,000 and a 
% loan lifetime of $30$ years ($360$ payments), we get the following table
% of payment amounts as a function of annual interest rate:
format bank; 
disp('      Rate (%)    Monthly Payment ($)');
for rate = 1:10, 
  disp([rate,double(subs(R, {A, N, J}, ...
       {150000, 360, rate*percent*peryear}))])
end   
%%
% Note the use of |*format bank*| to write the floating-point numbers with
% two digits after the decimal point.
%%
% There's another way to understand these calculations that's a little 
% slicker, and that uses MATLAB's linear-algebra capability.  Namely, we 
% can write the fundamental equation 
%
% $$ P_{\mathrm{new}} = P_{\mathrm{old}} m - R $$
%
% in matrix form as
%
% $$ v_{\mathrm{new}}  = B v_{\mathrm{old}}, $$
%
% where
%
% $$ v = \begin{pmatrix} P \\ 1 \end{pmatrix} $$
%
% and
%
% $$ B = \begin{pmatrix} m &-R\\ 0& 1\end{pmatrix}. $$
%
% We can check this using matrix multiplication:
syms R P; B = [m -R; 0 1]; v = [P; 1]; B*v  
%%
% which agrees with the formula we had above.  Thus, the column vector 
% *|[P; 1]|* resulting after $n$ payments can be computed by
% left-multiplying the starting vector *|[A; 1]|* by the matrix $B^n$. 
% Assuming that $m > 1$, that is, that the rate of interest is positive,
% the calculation
[eigenvectors, diagonalform] = eig(B)  
%%
% shows us that the matrix $B$ has eigenvalues $m$, $1$, and corresponding 
% eigenvectors   
% 
% $$\begin{pmatrix} 1 \\ 0 \end{pmatrix} \hbox{ and }\begin{pmatrix} 1
% \\ (m-1)/R \end{pmatrix} = \begin{pmatrix} 1 \\ J/R \end{pmatrix}.$$
% 
% Now we can write the
% vector *|[A; 1]|* as a linear combination of the eigenvectors: 
%
% $$\begin{pmatrix} A \\ 1 \end{pmatrix} = x\begin{pmatrix} 1 \\ 0
% \end{pmatrix} + y\begin{pmatrix} 1 \\ J/R \end{pmatrix}.$$
% 
% We can solve for the coefficients:
[x, y] = solve(A == x*1 + y*1, 1 == x*0 + y*J/R, x, y)  
%%
% and so 
%
% $$\begin{pmatrix} A \\ 1 \end{pmatrix} = (A - (R/J))\begin{pmatrix}
% 1 \\ 0 \end{pmatrix} + (R/J)\begin{pmatrix} 1 \\ J/R \end{pmatrix}$$
%
%%
% and  
%
% $$B^n\begin{pmatrix} A \\ 1 \end{pmatrix} = (A -
% (R/J))m^n\begin{pmatrix} 1 \\ 0 \end{pmatrix} + (R/J)\begin{pmatrix} 1
% \\ J/R \end{pmatrix}.$$
%
%%
% So the principal remaining after $n$ payments is:
%
% $$ P = \left((A J - R )m^n + R \right) / J = A m^n - R (m^n - 1) / J.  $$
%
%%
% This is the same result that we obtained earlier.
% To conclude, let's determine the amount of money $A$ you can afford to 
% borrow as a function of what you can afford to pay as the monthly 
% payment $R$.  We simply solve for $A$ in the equation that
% $P = 0$ after $N$ payments:
solve(A*m^N - R*(m^N - 1)/(m - 1), A)  
%%
% For example, if we were shopping for a house and could afford to pay 
% \$1500 per month for a $30$-year fixed-rate mortgage, 
% the maximal loan amount as a function of the interest rate is given by:
disp('      Rate (%)    Maximal Loan ($)');
for rate = 1:10, 
  disp([rate,double(subs(ans, {R, N, m}, ...
       {1500, 360, 1 + rate*percent*peryear}))])
end 

