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)

Respuestas (1)

Star Strider
Star Strider el 4 de Mzo. de 2018
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

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Mzo. de 2018

Respondida:

el 4 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by