Trying to use a for loop to calculate years with an IF statement but it seems to ignore it.

1 view (last 30 days)
So the program is meant to stop if balance >= 500000 but it seems to soldier on until it runs out of months. Use of a for loop is required.
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M = 1; % month ticker
years = 12/M;
interest = 0.0025;
for M = 1:300
if balance(M) >= 50000 %balance(M)
end
M = M + 1; % you need to increment months + 1 every cycle
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
end
%Balance %This will display the final Balancefprintf('%g Months until $500000 reached\n', M);
fprintf('%g Years until $500000 reached\n', years);
12 Years until $500000 reached
format bank
plot(balance);
grid on
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')

Answers (1)

Jan
Jan on 13 Sep 2021
Edited: Jan on 13 Sep 2021
Either
for M = 2:300
if balance(M) >= 50000 %balance(M)
break; % Leave the for M loop
end
% NOPE ! M = M + 1; % you need to increment months + 1 every cycle
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
end
or
for M = 2:300
if balance(M) < 50000 %balance(M)
% NOPE ! M = M + 1; % you need to increment months + 1 every cycle
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
end
end
If you do not know in advanve how log a loop runs, a while loop is nicer:
M = 2;
while M <= 300 && balance(M) < 50000
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
M = M + 1;
end
While the counter M must be increased in the while loop, this is done in the for loop automatically.
[EDITED] M starts at 2 now.
  4 Comments
Jan
Jan on 13 Sep 2021
This looks strange:
M = 1; % month ticker
years = 12/M; % Are you sure that 12/1 is the number of years?!
% The usual definition is Months / 12
for M = 1:300
if balance(M) >= 500000 %balance(M)
break; % Leave the for M loop
end
% No! Do not increase the loop counter inside the
% body of a FOR loop:
% M = M + 1;
My code failed, because accessing balance(M - 1) requires the loop to start at M=2. I've edited my answer.
Label = max(balance) is not useful before the loop, because balance contains 1 value only.
Chett Manly
Chett Manly on 14 Sep 2021
You were right about the year ticker, it needs to be lower in the code to work correctly.
If you disable the M line in the loop, and change the for if argument to not have the M ticker the program.
This version runs but if I change the M value to 2 and the for arguement to 2:300 then it won't run.
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M = 1; % month ticker
interest = 0.0025;
for M = 1:300
if balance(M) >= 500000 %balance(M)
break; % Leave the for M loop
end
M = M + 1; % you need to increment months + 1 every cycle
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
Label = max(balance) %this labels the plot with max balance
end
%Balance %This will display the final Balancefprintf('%g Months until $500000 reached\n', M);
years = M/12;
fprintf('%g Years until $500000 reached\n', years);
format bank
plot(balance);
grid on
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')
%This creates the Label on the plot
txt = {'Max Balance:',Label,'\leftarrow'};
text(M,Label,txt,'HorizontalAlignment','right')

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!