Error: "Cannot determine number of iterations for a loop..."

13 views (last 30 days)
Hi Everyone!
So, I having a problem with a MATLAB Function that I am using in SIMULINK. When I try to run my code I get the following error, "Cannot determine the exact number of iterations for a loop with range 0.111:0.001:0.131" , and the model goes into Debug mode.
The general structure of my code is:
function [y] = fcn(x,y_prev)
%#codegen
ctr=1;
for A = (y_prev-0.01):0.001:(y_prev+0.01)
% Some Calculations
B(ctr) = %some algebraic expression based on the variable "x"
ctr=ctr+1;
end
temp = (y_prev-0.01):0.001:(y_prev+0.01);
y = max(temp(B == max(B)));
Where "y_prev" is the previous iteration's value for y (determined by a memory block outside of the MATLAB Function.
I feel like I have tried just about every possible solution. I have added "assert" commands as I first thought that MATLAB couldn't determine bounds for the variables. And I also bounded the output variable range from the Ports & Data Manager. However, if I change "temp" to a constant range (such as 0.2:0.001:0.22) instead of calculating my range from y_prev, it works. I have also noticed that if I restrict the range of the input variable "x" that the code works. I have checked to make sure that B is a real number, and there are no issues there.
I should also mention that I had written this code on a previous computer where it worked beautifully. But, alas that computer died and I am now using a new computer. I have tried to run my code on several other computers just to make sure that it is not my new computer, but I haven't had any luck.
Does anybody have any suggestions or ideas for this?

Accepted Answer

Walter Roberson
Walter Roberson on 30 Aug 2012
Stepsizes that are not integers or powers of 2 (e.g., 1/2) lead to round-off errors that can make it difficult to predict the exact number of iterations. See http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
You could, for example, use
for At = -10:10
A = y_prev - At/1000;
  1 Comment
John
John on 30 Aug 2012
Thanks Walter!
I had tried something similar using "round" but I must have made a mistake. Sometimes it is just helpful to get a fresh eye on the problem.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!