why is my plot not showing lines

%This script is to solve for the investment of $50
%with an interest of 1% or 0.01 the equation uses the
% A * (1 + r) .^ n equation of interest with investment
%Script wrote by Doyouknow
format bank
monthlyInvestment = 50; %monthlyInvestment = 50;
interestRate = 0.01; %interestRate = 0.01;
monthEndBalance = 0;
disp('Month Month End Balance')
figure;
for month = 1:12
hold on;
monthEndBalance = monthEndBalance + (monthlyInvestment * (1 + interestRate) .^ month);
fprintf([num2str(month)]), disp(monthEndBalance(:))
plot (month,monthEndBalance,'r.')
title('Plot of Investment') %Testing out to see if i can plot
xlabel('Month (month)') %using the given data so far not
ylabel('Year (monthEndBalance)')
xticks (1:12) %sets the x values to be 1 - 12 to represent the graph
end

 Respuesta aceptada

Ankit
Ankit el 9 de Sept. de 2022
Editada: Ankit el 12 de Sept. de 2022
Edited on 12.09.2022
Problem in your script is that you are not storing your results, values are updated in your "for loop"
%This script is to solve for the investment of $50
%with an interest of 1% or 0.01 the equation uses the
% A * (1 + r) .^ n equation of interest with investment
%Script wrote by Doyouknow
format bank
monthlyInvestment = 50; %monthlyInvestment = 50;
interestRate = 0.01; %interestRate = 0.01;
j = 1;
monthEndBalance =zeros(1,12);
prevmonthEndBalance = 0;
disp('Month Month End Balance')
Month Month End Balance
figure;
month = 1:12;
for i = 1:length(month)
monthEndBalance(i) = prevmonthEndBalance + (monthlyInvestment * (1 + interestRate) .^ i);
prevmonthEndBalance = monthEndBalance(i);
fprintf([num2str(month(i)) '-' num2str(monthEndBalance(month(i))) '\n'])
end
1-50.5 2-101.505 3-153.02 4-205.0503 5-257.6008 6-310.6768 7-364.2835 8-418.4264 9-473.1106 10-528.3417 11-584.1252 12-640.4664
plot (month,monthEndBalance);hold on
title('Plot of Investment') %Testing out to see if i can plot
xlabel('Month (month)') %using the given data so far not
ylabel('Year (monthEndBalance)')
xticks (1:12) %sets the x values to be 1 - 12 to represent the graph

3 comentarios

Doyouknow
Doyouknow el 9 de Sept. de 2022
Oooooo so it’s best to not use a for loop
Got it but is it possible to graph with a for loop? But I see with the hold on that will hold the values and save it for the graph. Alright I think I am seeing it thanks!
Doyouknow
Doyouknow el 9 de Sept. de 2022
Only issue I noticed is that the values are wrong. They are calculated incorrectly from what I am seeing more in the code you showed doesn’t work with the values I am attempting to get
Ankit
Ankit el 12 de Sept. de 2022
@Doyouknow Please check the updated solution.

Iniciar sesión para comentar.

Más respuestas (1)

chrisw23
chrisw23 el 9 de Sept. de 2022

0 votos

Each plot call (loop) creates a line with one point. ( debug by look at the Figure.Children.Children property or save the return value of plot() )
This results in a 12×1 Line array and not a single line as you expected. Precalculate your data before plotting or try to use an animated line and add points in your loop.

Categorías

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

Etiquetas

Preguntada:

el 9 de Sept. de 2022

Comentada:

el 12 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by