How can I modify the iteration loop?

1 view (last 30 days)
I am trying to modify the iteration loop, so that I can show the final payment of the car loan and the month which the final payment occurs.
clc; clear; % clear the command window and memory
x = 2000; % starting balance of the current account
r = 0.015/12; % the interest rate on the current account
sal = 24000; % salary
liv = 1800; % monthly living expenses
carloan = 15700; % starting sum of the car loan
carrep = 357; % monthly car loan repayments
carrs = 0.05/12; % monthly car loan interest
N = 120; % the number of months to consider
t = 1:N+1; % our timeline
xhist=zeros(1,N+1); % create a vector to hold the account history
xhist(1) = x;
carhist=zeros(1,N+1); % vector to hold the car loan history
carhist(1) = carloan;
for m=1:N % commands inside the loop are executed for each month
x = (1+r)*x+sal/12-liv; % apply the interest and pay in the salary
carloan = (1+carrs)*carloan; % apply the interest to the car loan
if carloan-carrep>0
x = x-carrep; % take money off the current account...
carloan = carloan-carrep; % ...and make car loan payment
else
x = x-carloan; % take the remainder off the current account...
carloan = 0; % ...and close the car loan
end
xhist(m+1) = x; % save the sum at the beginning of month m
carhist(m+1) = carloan; % do the same for the car loan
end
fprintf('%.2f\n',x); % balance at the beginning of (N+1)th month
fprintf('%.2f\n',carloan); % print the outstanding loan balance
So far I've only managed to get the car loan balance for a given month.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Dec 2016
Before you do
x = x-carloan; % take the remainder off the current account...
print out x -- it will be the final payment. m will be the month number.
After you do the next command
carloan = 0; % ...and close the car loan
add a "break"

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!