フィルターのクリア

Not sure why while loop won't run.

1 回表示 (過去 30 日間)
Chett Manly
Chett Manly 2021 年 9 月 13 日
コメント済み: Chett Manly 2021 年 9 月 13 日
%trying to run a while loop, (then a for and a forever) but to me the code seems logical but it has a bug that is stopping it.
%The idea is it loops until the balance hits $500000 then plots a graph. kinda out of ideas...
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M(1) = 1; % month ticker
interest = 0.0025;
while balance < 500000
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
M(M) = M + 1; % you need to increment months + 1 every cycle
end
%M(M) = M + 1;
plot(balance,M);

採用された回答

Matt J
Matt J 2021 年 9 月 13 日
編集済み: Matt J 2021 年 9 月 13 日
balance(1) = 1000;
contribution = 1200;
interest = 0.0025;
M = 2; % month ticker
while balance(end) < 500000
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
M = M + 1; % you need to increment months + 1 every cycle
end
plot(balance);
  1 件のコメント
Chett Manly
Chett Manly 2021 年 9 月 13 日
%Damn that was a simple mistake I had...
%Added some plot formatting and we are functioning.
balance(1) = 1000;
contribution = 1200;
M(1) = 2; % month ticker
interest = 0.0025;
while balance(end) < 500000
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
M = M + 1; % you need to increment months + 1 every cycle
end
format bank
plot(balance);
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by