What is wrong with this while loop

2 views (last 30 days)
Saaz Sakrikar
Saaz Sakrikar on 20 Oct 2015
Commented: Star Strider on 21 Oct 2015
I have written a long-ish function, it says "busy", heats up the computer, and does not finish even if I reduce the size of the arrays involved drastically. When I ctrl+C it, it always terminates on a while statement, or the if statement nested inside it, or the "end" for those 2.
This is the code, with the while loop and if statement written in full:
for t=2:(totaltime/timestep)
<stuff, including arraysize(t)=arraysize(t-1)>
for i=1:arraysize(t-1)
<lots of stuff, including increasing arraysize(t) - I thought the issues would crop up here>
end
n=(0.1/60)*timestep*currentcells(t-1);
while remcells(t)<n
r=randi(arraysize(t)); %line 108
if yeast.biomass(t,r)>0 %line 109; yeast.biomass is an array of size arraysize(t), I'm picking n random elements from it
remcells(t)=remcells(t)+1;
currentcells(t)=currentcells(t)-1; % I thought it was a potential problem but removing it made no difference
<some undemanding stuff>
else
continue
end
end
<very undemanding stuff>
end
Given that whenever I terminate it, it ends at that same line
"Operation terminated by user during timevolution (line 108)"
(More usually) "Operation terminated by user during timevolution (line 109)"
I assume there's something wrong with that while loop or the if statement. Is there? Or does it ending at that same line not mean anything?
Any help will be greatly appreciated! I am a novice at Matlab (and coding in general) so if there's something "obvious" that might be a solution please point it out...
  2 Comments
Walter Roberson
Walter Roberson on 20 Oct 2015
What evidence is there that yeast.biomass(t,r)>0 for any r ? Or that n is not Very Big ?
Saaz Sakrikar
Saaz Sakrikar on 21 Oct 2015
yeast.biomass is initialised at 1 for all yeast and is an exponentially growing function I only set it =-10^-6 to mark a yeast I want to remove.
n should be a small fraction of the size of the array yeast.biomass, so I don't think that should be a problem either.
I will check both of those out though, thanks for the advice. If that's going wrong there are deeper problems in my model!

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 20 Oct 2015
I can’t run your code, so these are wild guesses. There are two possibilities that I see:
First, see what the end condition for the for loop is, specifically the value of (totaltime/timestep). The default increment is 1 for colon-operator vectors. The end condition value could be larger than you thought.
Second, check the value of ‘yeast.biomass(t,r)’ is to be certain you enter the if block. Write the values of ‘n’ and ‘remcells(t)’ to the Command Window within your while loop. The while condition might never be satisfied.
These are where I would look.
  2 Comments
Saaz Sakrikar
Saaz Sakrikar on 21 Oct 2015
I don't think the totaltime/timestep is a problem, I used a siplified version of this code with the same loop and had no problems.
I will check the remcells by making n = 1 and seeing if the code responds.
Star Strider
Star Strider on 21 Oct 2015
It’s best not to take anything for granted. Check everything — the values of all variables — that might be a part of this problem.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!