Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose that you take out a loan for $22,000. The bank charges 6% annual interes

ID: 2082256 • Letter: S

Question

Suppose that you take out a loan for $22,000. The bank charges 6% annual interest on the unpaid balance, compounded monthly (that's 0.5% interest each month). Write a MATLAB function M-file named RepayLoan that has a monthly payment as input. The program will compute and send as output how many months will it take to pay off the loan. Additionally, the function will print a table containing the balance remaining at the end of each month. For example, the table at left shows that with a monthly payment of $3000, the loan is repaid in the 8th month The payment is made at the start of the month. The interest is compounded at the end of the month, after the payment is made. The last payment will be less than the monthly payment & the final balance is zero. Once you get the sample case working, you MUST also try RepayLoan (80) BEFORE testing on Cody. Don't submit infinite loops! Be sure to prevent infinite loop cases & other bad input (such as nonscalar)

Explanation / Answer

Matlab Script:

function m = RepayLoan(x)
if length(x)==1
init = 22000;
if x>0.005*init
m=0;
disp('month balance reamaining');
while 1==1
init = init-x;
if init>0
init_next = init+(init*0.005);
init = init_next;
m = m+1;
fprintf('%d %d ',m,init);
else
m = m+1;
fprintf('%d %d ',m,0);
break;
end
end
else
m = 'input cannot be lessthan 0.5% of 22000 which is 110';
end
else
m='non scalar';
end
end

command window log:

>> clear all
>> RepayLoan(3000)
month balance reamaining
1 19095
2 1.617548e+004
3 1.324135e+004
4 1.029256e+004
5 7.329022e+003
6 4.350667e+003
7 1.357420e+003
8 0

ans =

8

>> RepayLoan(80)

ans =

input cannot be lessthan 0.5% of 22000 which is 110

>> RepayLoan([80 100])

ans =

non scalar

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote