Why does the code stop doing iterations?

6 views (last 30 days)
When the car loan has been paid off, the student loan value goes to 0 as well.
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 = 28650; % graduate's salary
RPI = 0.017; % RPI value
liv = 1506; % monthly living expenses
stloan = 27000; % starting sum of the student loan
if sal<21000
rep = 0; % monthly student loan repayment
rs = RPI/12; % monthly student loan interest
elseif sal<41000
rep = 0.09*(sal-21000)/12; % monthly student loan repayment
rs = (RPI+0.03*(sal-21000)/20000)/12; % student loan interest
else
rep = 0.09*(sal-21000)/12; % monthly student loan repayment
rs = (RPI+0.03)/12; % monthly student loan interest
end
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;
sthist=zeros(1,N+1); % vector to hold the student loan history
sthist(1) = stloan;
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; % apply the interest and pay in the salary
stloan = (1+rs)*stloan; % apply the interest to the student loan
carloan = (1+carrs)*carloan; % apply the interest to the car loan
x = x-liv;
if stloan-rep>0
x = x-rep; % take money off the current account...
stloan = stloan-rep; % ...and make student loan payment
else
x = x-stloan; % take the remainder off the current account...
stloan = 0; % ...and close the student loan
end
if carloan-carrep>0
x = x-carrep; % take money off the current account...
carloan = carloan-carrep; % ...and make car loan payment
else
fprintf('Month of Final Car Loan Payment = %.2f\n',m); % print the month of final payment
fprintf('Final Car Loan Payment = %.2f\n',carloan); % final loan payment
x = x-carloan; % take the remainder off the current account...
carloan = 0; % ...and close the car loan
break
end
xhist(m+1) = x; % save the sum at the beginning of month m
sthist(m+1) = stloan; % do the same for the student loan
carhist(m+1) = carloan; % do the same for the car loan
end
fprintf('Balance = %.2f\n',x); % balance at the beginning of (N+1)th month
fprintf('Student Loan Balance = %.2f\n',stloan); % print the outstanding student loan balance
fprintf('Car Loan Balance = %.2f\n',carloan); % print the outstanding car loan balance
  2 Comments
the cyclist
the cyclist on 6 Dec 2016
@Baja Laja, just want to let you know that deleting your questions after they have been answered (especially by two of the top contributors on the site) is an excellent way to ensure that your future questions will not be answered at all.
Rena Berman
Rena Berman on 20 Jan 2017
(Answers Dev) Restored Question.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 4 Dec 2016
Because when m = 49, you hit the break command, and leave the for loop. See the documentation for break for details.
  4 Comments
Baja Laja
Baja Laja on 5 Dec 2016
Edited: Baja Laja on 5 Dec 2016
Sorry I don't understand what you mean.
Walter Roberson
Walter Roberson on 5 Dec 2016
if carloan ~= 0
if carloan-carrep>0
x = x-carrep; % take money off the current account...
carloan = carloan-carrep; % ...and make car loan payment
else
fprintf('Month of Final Car Loan Payment = %.2f\n',m); % print the month of final payment
fprintf('Final Car Loan Payment = %.2f\n',carloan); % final loan payment
x = x-carloan; % take the remainder off the current account...
carloan = 0; % ...and close the car loan
break
end
else
%already finished paying off the car, do not do anything
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!