Part 2 (matlab)
Functions Many of you will have some form of a loan during your lifetime, either a student loan, mortgage, credit card debt, or other loan. If you never do have a loan, great! But you may have to calculate a payment for someone else, such as the company you work for. Such a loan will have a principal amount (called the present value), an interest rate, and a number of periods for the loan. The formula to calculate the payment P required to pay off a loan of value PV over n periods at r interest rate is
?? = ??(????) 1 ? (1 + ??)???
For example, if you have a $20,000 loan (PV = 20000) at a 5%
annual percentage rate, to be paid monthly over 10 years, the rate
r would be 0.05/12 (monthly rate) and the number of periods n would
be 12*10 = 120. The payment would then be $212.13 per month (with a
slightly larger payment at the end to take care of the tenths of a
penny I truncated off).
Please note that if n is in months, then r has to be the monthly
rate. The monthly rate can be found by taking the annual percentage
rate (APR) and dividing by 12.
a) Write a function payment that takes in PV, r, and n as inputs, and outputs the payment P.
b) Use the function you wrote above to create a loan amortization table. A loan amortization table will show how much principal is left after each payment and how much interest is paid with each payment. The columns should be principal paid, interest paid, cumulative principal, cumulative interest, and principal balance. So for the loan above, the amortization table would look like:
Principal Paid Interest Paid Cumulative Prin Cumulative Int Principle Balance
128.80 83.33 128.80 83.33 19871.20
129.33 82.80 258.13 166.13 19741.87
129.87 82.26 388.01 248.39 19611.99
130.41 81.72 518.42 330.10 19481.58
a)
%matlab code
clc;
close all;
clear all;
Principle = 20000;
rate = 5; %annual
no_installments = 10 %years
Payment = Emi(Principle,rate/(12*100),no_installments*12)
%end
Emi function Emi.m code
%start
function EMI = Emi(p,r,n)
EMI = (p * r * (1+r)^n)/(((1+r)^n)-1);
%end
Result:
Payment = 212.1310
b)
clc;
close all;
clear all;
P = 20000;
rate = .05/12;
no_installments = 10*12;
[Principal, Interest, Balance, Payment] = amortize(rate, ...
no_installments, P)
fp = fopen('loan.txt','w');
fprintf(fp,'Principle paid\n');
fprintf(fp,'%f\n',Principal);
fprintf(fp,'Interest paid\n');
fprintf(fp,'%f\n',Interest);
fprintf(fp,'cumu prin\n');
fprintf(fp,'%f\n',cumsum(Principal));
fprintf(fp,'cumu in\n');
fprintf(fp,'%f\n',cumsum(Interest));
fprintf(fp,'balance\n');
fprintf(fp,'%f\n',Balance);
fclose(fp)
%end of code
Get Answers For Free
Most questions answered within 1 hours.