In: Computer Science
USING MATLAB:
A simple mortgage calculator that will output the monthly payment, the remaining balance, and so on.
years=30;
annual_rate=0.04; % 2.75% annual rate
house_value=500,000;
downpayment=3.5; % 25% down
and plot results
Code for mortgage function:
% L: size of the loan
% rate: annual rate
% years: length of loan in years
%
% Outputs
% P: monthly payment
% In: list of interests paid over the entire loan period
% Ba: Remaining balane over the entire loan period
function [P,In,Ba]=mortgage(L,rate,years)
N=years*12;
r=rate/12; % monthly rate
%L*(1+r) = P + P/(1+r) + P/(1+r)^2 + P/(1+r)^3
% = P (1-(1/1+r)^N)/(1-(1/(1+r))
P= L*(1+r)/((1-(1/(1+r))^N)/(1-1/(1+r)));
Ls(1)=L;
In(1)=0;
Ps(1)=0;
for i=2:N+1
In(i)=Ls(i-1)*r;
Ps(i)=P-In(i);
Ls(i)=Ls(i-1)-Ps(i);
end
Ba=Ls(2:end);
In=In(2:end);
Code for creating a script which calls the function to calculate:
years=20;
annual_rate=0.04; % 4% annual rate
house_value=500000;
downpayment=0.25; % 25% down
loan_size=house_value*(1-downpayment);
[P,In,Ba]=mortgage(loan_size,annual_rate,years);
% close all
fprintf('Monthly Payment: %1.2f \nTotal Interest Paid: %1.2f\nBalance remaining: %1.2f',P,sum(In),house_value-sum(In)-downpayment*house_value);
(Feel free to ask if you have any doubts in the comments below)