Restarting a for-loop when a condition is met

I am trying to make a movie 60 frames long which shows a change in pressure profile over 60 months. I have a for-loop which calculates vector V for each month m=1:60. Then a nested for-loop which calculates the pressure at 101 equidistant points. This gives me 60 pressure profiles which I've been able to play frame-by-frame.
Problem: Now I want the first for-loop to restart whenever a pressure value drops below a value x. I've tried using an if-statement and a while-loop but I can't seem to get it to work/put it in the right place. Maybe this is because I've used the variable m in a lot of the subsequent calculations. I realize that the loop will never finish but I only need 60 iterations.
for m=1:N
V(m) = (4*q)/(pi*((d-wm*(m))^2));
for i = 1:n
P1(i,m) = p - r*g*H(i) - r/(2*(d-wm*(m)))*f*V(m)^2*D(i);
end
plot(P1(:,m),'r-')
title('Pressure along pipeline', 'FontSize', 14);
xlabel('Distance', 'FontSize', 14);
ylabel('Pressure [Pa]', 'FontSize', 14);
xlim([1 100]), ylim([0 p])
ss = strcat('Month',{' '},num2str(m));
legend(ss)
grid
M(:,m) = getframe;
pause(0.1)
end

5 commentaires

When you say that you want the loop to restart, do you mean that you want to have the loop go back to m = 1 ? Or do you mean that you want to go on to the next iteration of m ?
dpb
dpb le 14 Sep 2015
Why not vectorize the two loops and then just set the values of P<threshold == NaN? You could then create the movie from the computed data...
NotSoWiseman
NotSoWiseman le 14 Sep 2015
Walter, I'd like it to go back to m=1. Essentially the diameter is being tightened with every iteration of m and once the criteria is met the diameter should go back to the original width. dpb I'll need to read up on what NaN does.
dpb
dpb le 14 Sep 2015
'Not a Number' but the plotting routines ignore NaN when drawing. If you terminate the loop early, the sizes will be noncommensurate in array Pi and you'll have to have cell array to store the results or will get mismatch in lengths that have to deal with (although if you preallocate to NaN on size(m*n) you'll get to the same place in the end, just that using the vectorized solution is the more "Matlab-y" way, avoiding loops).
NotSoWiseman
NotSoWiseman le 14 Sep 2015
Okay that actually makes a lot of sense. I understand vectorization is preferable for a number of reasons, however the assignment explicitly states we have to use for-loops and an if-statement to implement the criteria... I'll still try and use the approach you suggested approach

Connectez-vous pour commenter.

 Réponse acceptée

You would not do this with a for loop, you would do it with a while loop.
m = 1;
while m <= N
.....
if some condition
m = 1;
else
m = m + 1;
end
end

3 commentaires

NotSoWiseman
NotSoWiseman le 14 Sep 2015
Wow that was embarrassingly easy. It looped infinitely since the criteria is met at m = ±16, however I just replaced it with a different variable that increases each iteration. Thank you
I know this is a very old post. But I wanted to do the same thing and I also noticed that it looped infinitely after the condition is met as well. Can you explain what you meant by replacing it with a different variable to make it work? Thanks!
Ajeet Bahadur Singh
Ajeet Bahadur Singh le 21 Juil 2022
Modifié(e) : Ajeet Bahadur Singh le 21 Juil 2022
I followed the same and its working fine, thanks a lot. But the only issue is that while loop is running forever (Although I can stop it manually after certain accuracy is achieved) probably because it is minimizing the error (as in my case) all the way upto infinite decimal points. Is there a way to reduce the accuracy upto certain decimal points only of finding the error of my solution?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by