Why does the code stop doing iterations?

조회 수: 7 (최근 30일)
Baja Laja
Baja Laja 2016년 12월 4일
댓글: Rena Berman 2017년 1월 20일
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
the cyclist
the cyclist 2016년 12월 6일
@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 2017년 1월 20일
(Answers Dev) Restored Question.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2016년 12월 4일
Because when m = 49, you hit the break command, and leave the for loop. See the documentation for break for details.
  댓글 수: 4
Baja Laja
Baja Laja 2016년 12월 5일
편집: Baja Laja 2016년 12월 5일
Sorry I don't understand what you mean.
Walter Roberson
Walter Roberson 2016년 12월 5일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by