So I have a bank account of 6000 dollars and it has a 12% annual interest rate. I have to stop the while loop when the 6000 doubles. Just having some trouble displaying this as a chart with one column counting years and the other displaying the balance after each year. This is what I have right now but it only displays the final year and final balance. Please help!
Balance = 6000;
Year = 0;
while (Balance < 12000)
Year = Year + 1;
Balance = Balance*1.12;
end
T = table(Year, Balance)

回答 (1 件)

Star Strider
Star Strider 2018 年 3 月 4 日

0 投票

You need to subscript them and save them as vectors:
Balance = 6000;
Year = 0;
k = 1;
while (Balance < 12000)
k = k+1;
Year(k) = Year(k-1) + 1;
Balance(k) = Balance(k-1)*1.12;
end
T = table(Year(:), Balance(:))
figure(1)
plot(Year, Balance)
grid on

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

質問済み:

2018 年 3 月 4 日

回答済み:

2018 年 3 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by