How can I modify the iteration loop?

1 回表示 (過去 30 日間)
Baja Laja
Baja Laja 2016 年 12 月 3 日
コメント済み: Rena Berman 2017 年 1 月 20 日
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.
  1 件のコメント
Rena Berman
Rena Berman 2017 年 1 月 20 日
(Answers Dev) Restored Question.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 12 月 4 日
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"

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by