Display all while loop outputs into a chart?

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