For loop doesn't run properly

5 visualizzazioni (ultimi 30 giorni)
Stanley Ka
Stanley Ka il 16 Mag 2021
Commentato: Stanley Ka il 16 Mag 2021
I coded this myself to plot a graph of digging time vs amount of gold in the bucket for each specific depth. Everything seemed to work fine and there are no errors but the for loop doesn't run properly. Even though n updates each iteration from 2 to 8 the final answer I get is always for n = 2. However, when I don't use for loop and specify n = 2, 3, 4, etc. the code works fine and returns the actual solution I want. I know there could be 1 tiny silly mistake somewhere that is causing this problem, but I've been staring at this for almost 3 hours and still have no clue. Can someone help please.
load = 0;
sluice_time = 2*((20/1)/60);
dig_time = 0;
tot_time = 0;
bucket = 0;
shovel = 6;
tot_gold = 0;
% New:
gold_avg3 = []; % A new empty vector for the average gold in the bucket for each depth
gold_vector3 = []; % A new empty vector for totol gold in the bucket for b(iii)
for n = 2:8
i = 1;
d = 1;
while tot_time < 239.999
while load < 18
if d == n % When the depth is at the particular depth we want
load = load + 3;
dig_time = dig_time + 1;
tot_gold = tot_gold + gold(n); % Only takes the shovel load from a particular depth therefore the amount of gold is the same
gold_vector3(i) = tot_gold;
i = i+1;
d = 1; % After we took the gold at that specific level of depth, we have to go back to the surface and repeat the same process again
else
dig_time = dig_time + 1; % Dig time increases regardless of whether we put the shovel load into the bucket or not
gold_vector3(i) = tot_gold; % while we're digging at a depth that we're not interested, the amount of gold in the bucket remains the same
i = i+1;
d = d+1; % If the depth is not the depth we're interested in, move on to the next depth
end
end
tot_time = tot_time + dig_time + sluice_time;
dig_time = 0;
load = 0;
tot_gold = 0;
bucket = bucket + 1;
tot_dig_time = (1:bucket*6*n); % The total dig time now would be 6n per bucket, where n is the particular depth (Ie. level 2 for 12 min, level 3 for 18 min etc.)
end
gold_avg3(n) = sum(gold_vector3)/bucket; % Assign the nth element to the tot_gold of that nth iteration
plot(tot_dig_time,gold_vector3) % Plot the new scenario
hold on
end
  1 Commento
Mathieu NOE
Mathieu NOE il 16 Mag 2021
hello
tried to run your code but got this error message (gold(n) is undefined) :
Unrecognized function or variable 'gold'.
Error in Stanley (line 33)
tot_gold = tot_gold + gold(n); % Only takes the shovel load from a particular depth
therefore the amount of gold is the same

Accedi per commentare.

Risposta accettata

Jan
Jan il 16 Mag 2021
tot_time = 0;
for n = 2:8
while tot_time < 239.999
while load < 18
if d == n % When the depth is at the particular depth we want
else
end
end
tot_time = tot_time + dig_time + sluice_time;
end
end
After the first iteration of the for n loop, tot_time is >= 239.999 and it is not changed again. Therefore the inner loop is not entered anymore.
You can check this problems using the debugger. Set a breakpoint in your code and step through it line by line.
  1 Commento
Stanley Ka
Stanley Ka il 16 Mag 2021
I found the problem, I just needed to reset gold_vector3 and changed the vector length for tot_time in plot and it worked perfectly
Thanks so much for pointing out the error

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by