For loop: max iterations exceeded

I'm trying to run a simple for loop that iterates through each record of a CSV (which has 185 records) and does things with it. But I get the error "maximum number of iterations reached".
rrs_chl = readtable('filename.csv');
for i = 1:height(rrs_chl)
...
<run functions>
...
end
Exiting: Maximum number of iterations has been exceeded
- increase MaxIter option.
Current function value: 0.015808
I'm not sure how to resolve this. I am assuming it stems from the for loop.
I tried
options = struct('MaxFunEvals', 2000)
and also
options = optimset('MaxIter',2000)
but I'm still getting the same error. I am using Matlab R2015a 64-bit.

Answers (2)

That message typically comes from optimization routines, and it indicates that it wasn't able to meet the optimization criteria in the specified number of internal iterations. It has nothing to do with your for loop, so we're going to need to know what's going on the the body of the for loop.

3 Comments

Oh, sorry! I was worried that including all the extra functions might add clutter to my question and make it hard to read.
Unfortunately I'm using quite a large program that was written by someone else (NASA) and involves a lot of extra functions. I'm not quite sure what all of them do, so i don't know which ones i should include here. I also don't think I would be allowed to write out all the code here. So here is some more info that might be helpful.
Right underneath the "Maximum iterations exceeded" error, I get this error:
Index exceeds matrix dimensions.
Error in giop (line 250)
rtest = abs(mrrs(v) - rin(v)) ./ rin(v);
giop is a function that is called in the program I am currently running. Here are the relevant lines where it breaks:
if isfield(gopt,'qc') == 1
v = find(wl >= 400 & wl <= 600.);
rtest = abs(mrrs(v) - rin(v)) ./ rin(v);
"rin" is defined earlier as:
rin = rrs ./ (0.52 + 1.7 .* rrs); %where rrs is a vector of 6 doubles
if isfield(gopt,'trans') == 1
if strcmp(gopt.trans,'flat')
% spectrally flat transmission a la Gordon 2007
rin = rrs ./ 0.529;
end
end
I don't know if this helps at all...
I noticed that the main function where it is breaking (giop.m) uses fminsearch:
% inversion (default to fminsearch)
%
if isfield(gopt,'inv') == 0; gopt.inv = 'fmin'; end
I read in some other forums that there is a way to "optimize" fminsearch to control the number of iterations or something. Do you know what the syntax would be for that? I've read the documentation, but can't figure out how that would apply to my code.
It's fminsearch (or one of the other similar iterative solvers) that is throwing the error, indeed.
gopt looks like the local structure holding the options. MaxIter is a field therein; you can try setting it up to something larger but as noted earlier it's likely more fundamental that if you do some other issue will arise.
Without the more extensive code and data don't know there's much more anybody here can tell you other than you have found the culprit. As the old "Mission Impossible" line went, "Your mission should you choose to accept it.." is to now look at the details of the problem and see what would need to do to get convergence.

Sign in to comment.

Has nothing whatsoever to do with the for loop; it is in the inner iteration of whatever function it is that you're calling.
We can't tell you the solution altho the error message is a hint; perhaps raising that iteration limit might let it converge; often that's hiding a problem in the problem statement that just can't find a solution as posed.
Anyway, the problem is in what you did NOT show; you'll find you'll have the same error if you just
<run functions>
from the command line with the same inputs set as you have in the script.

Categories

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

Products

Asked:

on 8 Jun 2017

Commented:

dpb
on 8 Jun 2017

Community Treasure Hunt

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

Start Hunting!