How can I increase a count in the while iterator block?

I am trying to run a whilte iterator block for the convergence of a variable, but this variable is not decreasing at all.
The goal is to reach at Display3 a value higher than 7.94 (Psep) and Display1 to show a value lesser than 31.
Could you help with this matter?
Thanks in advance.

14 Comments

hello
1/ I assume that this part
2 / Psep seems to be a constant = 7.94, so I don't see how it could evolve ...
3/ there is nothing that increments (counter) in your code . The while iterator block output should eventually be used to drive a counter which output then must be fed at the same firts add block (neg input) . I would also recommend to convert the while block output from logicl to numerical if you use it afterwards to do numerical operations
all the best
The output of the comparison to zero is being fed into the Add block where it is being subtracted. At best that would result in subtracting 1 each iteration; at worst it would result in subtracting 0 each iteration.
Hi, thanks for your time to analyse this problem.
1- You are right, it is inside a While iterator subsystem
2- You are right, Psep is a constant but it is only a value for comparison, the real thing is that I need that Fcn P be greater than Psep.
It comes from a larger system in which I already know for certain that Fcn P is lower than Psep. (the display block after the while iterator block is not the answer I am looking form).
That is why, inside the while iterator system, I am looking for lowering In1 by 1 each time the while iterator is running in order to have a Fcn P greater than Psep.
3- Again you are right. I am not really certain in the use of the while iterator block in order to control what I am planning to do. Could you tell me how I am supposed to do that? or give me a reference or example that I can use as a guide to do the job.
Thanks again.
Hi, I appreciate your time to analyse this problem.
You are right. it comes from a larger system and I already know for certain that Fcn P is lower than Psep and I am looking for lowering In1 by 1 each time the iterator is running in order to have a Fcn P greater than Psep. When Fcn P becomes greater than Psep, I expect to finish the cycle.
hello
can you share a working / simplified simulink file ?
I'll try to fix the problem
hi.
Thanks very much for your interest in this matter.
I have been working based on an example and have adapted it to the problem itself.
I send the answer I have come across.
Thanks again.
I think the link is broken
can you double check ?
Carlos
Carlos on 4 Mar 2026 at 16:47
Moved: Mathieu NOE on 4 Mar 2026 at 17:18
Hello again.
Sorry for the delay.
I was trying the hyperlink and the attachment icons, but they are not running as I expected.
I insert this diagram to ilustrate the flow.
The Fxn WHP2 block have this in it:
33.85269581 - 1.47824232 * u + 0.02529957 * u * u - 0.00014113 * u * u * u
After that, it runs well when I tried only one value from % Apert block. With more values, it doesn't run well.
I am looking for to use an algebraic constraint block afterwards outside this block for convergence, but if this block does not work for various data points, I get stuck.
If you could advise about this, I really appreciate.
is the simulink file to big to be attached with the paper clip button ?
The antivirus is probably blocking the attachment function.
I will try later.
Hello again.
Here is the attachment.
can you save it in Simulink Version 25.1 (matlab R2025a) please ?
R2025a version of the file enclosed.
Note: you could have opened the file in MATLAB Online and saved it there.
Thanks very much for your suggestions.

Sign in to comment.

 Accepted Answer

hello again @Carlos
I am not sure we really need a while block here. I haven't used simulink for very long , so I may be a bit rusted , but in your case I think there is a simpler and more direct approach
as we want to creat a counter which output is to be used as a control variable (your matlab special functio) so let's do it so and no need for a while counter
Nb : this is what the doc says :
While Iterator Subsystem
The while iterator subsystem repeats execution during a simulation time step while a logical condition is true. ....
A while iterator subsystem runs multiple iterations on each model time step. The number of iterations is controlled by the while iterator condition.
The while iterator subsystem is similar to a function-call subsystem in that it can run for any number of iterations on a given time step.
here we want to control how many iterations we do to let the full code converge. this is not exactly (in my view) what the while block will do. what it will do is to repeat exection even before the main loop has even done a single time step forward (and no retroaction will be executed in your code).
my suggestion is very simple : the memory block is used to create the counter (red square) , which is directly fed by the output of the logical operator (with a conversion from logical to integer in between) , then the output of the conter is used as the input of the feedback path. The gain block (in blue) is used to control the accuracy of the final result. As the variable %Apert seems to be given with 2 digits after the decimal separator, so we want to convert the output of the counter to have a similar precision : the simple trick here is to divide the output of the counter by 100 . Of course you will need 100 times more iterations to converge but it's the price to pay if you want to keep the precision.
NB : as we have implemented a counter, I prefer the solver options to be coherent with that choice , so I opted for fixed step solver. The time increment may not be critical as we are running a simulation only. You could of course try to match what you may choose for a rela time target application (your choice). I simply opted for dt = 1e-3 s , but you can change this.
In my simulation the loop has converged in 4888 steps so the simulation time must be at least 5s. again you can shorten the simulation time if you can afford a reduction in precision on the final values.
the simulation is stopped with a "stop" block, right after your logical comparator.
Below some screen shots, attached the modified code. Hope it helps !

5 Comments

I believe there is even a more straightforward solution to your problem
the main topic is how to solve your 3rd degree equation efficiently and maybe without iteration
well I think it's possible if the function is smooth and monotonic, you can "invert" it without having to compute the roots of the polynomial. You can simply create the look up table of the inverse function (by inverting input and output)
the look up table is initiated by this simple code (input and output valide range is also defined here) - then the simulink file becomes very simple and you get your answer in one single step without thousands of iterations ! see attachment
%% matlab function "inversion" using a look up table
% out = 33.85269581 -1.47824232*u +0.02529957*u.*u -0.00014113*u.*u.*u;
% first local minimum is solution of a second order polynomial
% dout_du = - 1.47824232 + 0.02529957*2.*u - 0.00014113*3*u.*u;
a = - 0.00014113*3;
b = 0.02529957*2;
c = - 1.47824232;
disc = b^2-4*a*c;
sol1 = (-b+sqrt(disc))/(2*a);
sol2 = (-b-sqrt(disc))/(2*a);
sol = min(sol1,sol2);
% valid range for function
u = linspace(0,sol,200);
y = 33.85269581 -1.47824232*u +0.02529957*u.*u -0.00014113*u.*u.*u;
plot(u,y);
hold on
% reverse order so y (out) is mononically increasing (look up tables in simulink must have break points mononically increasing )
y = y(end:-1:1);
u = u(end:-1:1);
plot(u,y,'*'); % here the two plots perfectly overlay
xlabel('u')
ylabel('y')
% in simulink the look up input is now y and the output u (NB : do not use "out"
% as variable name here )
It is really good your solution. Thanks very much.
My pleasure ! do you mind accepting my answer ? thanks !
Sorry for the delay accepting your answers.
On my cell phone, the icon to accept a response does not appear. I had to do it on the computer.
Again, thank you very much for your help. It was relly helpful.
as always, my pleasure !

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2025b

Asked:

on 20 Feb 2026

Commented:

on 16 Mar 2026 at 8:10

Community Treasure Hunt

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

Start Hunting!