For loop: max iterations exceeded

11 visualizzazioni (ultimi 30 giorni)
alice
alice il 8 Giu 2017
Commentato: dpb il 8 Giu 2017
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.

Risposte (2)

Kelly Kearney
Kelly Kearney il 8 Giu 2017
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 Commenti
alice
alice il 8 Giu 2017
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.
dpb
dpb il 8 Giu 2017
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.

Accedi per commentare.


dpb
dpb il 8 Giu 2017
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.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by