How can I repeat a complete "for loop" with new values and keep it running until a condition gets satisfied?

I know this is not a very big problem for Matlab experts, but I am kind of new so please help me with my code. Any kind of help is appreciated. I consider a=18 to begin with. Values of Cl and Cd depend on it. Then the "for loop" (beginning with j) runs in order to calculate "tor" and finally "power". Now if "power" exceeds 10, I wish to decrease the value of "a" by 1 (i.e.a=17) and then repeat the whole loop again with new Cl and Cd, in order to calculate power. If power then becomes less than 10, it should stop and move to next "V" value, otherwise it should keep decreasing value of "a" until power becomes less than 10. How can I do that?
I am quoting my code here:
format long; clc; clear all; close all;
a=18; t=[0 1 2 3 4 ]; M=[7 8 9 10 11];
for X=1:1:length(M) V=M(:,X)
Cl=(0.1024*a)+0.293; Cd=(0.0002*a)+0.006;
j=1; for r=0.1:0.85:3.5
sum_T=0;
lambda=(3.5*12.56)/V;
Vr=sqrt(((2*V/3)^2)+((r*12.56)^2));
phi_1=atan(3.5/(lambda*r));
phi_2=(2*phi_1/3);
chord=(1/3)*(16*3.14*r/Cl)*(sin(phi_1/3))^2;
Cx=Cl*sin(phi_2)-Cd*cos(phi_2);
Cy=Cl*cos(phi_2)+Cd*sin(phi_2);
product=0.5*3*1.225*(Vr^2)*0.85;
dU=product*chord*Cx;
dT=dU*r;
sum_T=sum_T+dT;
tor(j)=dT;
j=j+1;
end
power=(12.56*sum(tor))/1000;
if (power<10)
output1=power
else
a=a-1 % i.e. a=17 (value of a)
% REPEAT THE WHOLE LOOP OF j AND CALCULATE power
%IF EVEN THEN power>10, WE DECREMENT "a" i.e. a= 16 and so on
end
end

1 Comment

For the same code described above, how can I write the power output (array) against every value of V ? The loop continues to iterate until every element of the array is lower than 10. In the process certain elements which are already lower than 10 (let's say for V=6, Power= 9.5) keeps going down. I don't want that. I want the loop to break whenever power comes below 10 and move to next value of V, without changing the previous value of power (for e.g. iterating for V=7 should not change power corresponding to V=6). If I add another "if loop" inside and put a break then it comes out and does not continue for the rest of the V values. Please help!

Sign in to comment.

 Accepted Answer

Something like this maybe:
% Initial values to enter loop
a = 19;
power = 20;
while power > 10
a = a-1;
% FOR LOOP HERE
end
Now you can just make the output "power" instead of "output1", and there's no need for the if-else after the for loop, since the while conditional checks it for you.
-Cam

6 Comments

Thank you. Just tried that. But it goes into a never ending loop.
That happens if the while condition is always being fulfilled. If you are updating the "power", but it never gets below 10, then it'll loop forever. If you want to stop after a certain number of iterations, you can add that to the conditional and update the count in there too:
iter = 0;
while power > 10 && iter < 100
% FOR LOOP
iter = iter+1;
end
You're awesome Cam! Thank you!! My Power was always greater than 10, for some cases. So the infinite loop. Its working now.
For the same code described above, how can I write the power output (array) against every value of V ? The loop continues to iterate until every element of the array is lower than 10. In the process certain elements which are already lower than 10 (let's say for V=6, Power= 9.5) keeps going down. I don't want that. I want the loop to break whenever power comes below 10 and move to next value of V, without changing the previous value of power (for e.g. iterating for V=7 should not change power corresponding to V=6). If I add another "if loop" inside and put a break then it comes out and does not continue for the rest of the V values. Please help!
I'm not sure I fully understand what your application of this is, but I can give it a shot. You could reverse the order of the loops:
power = 20*ones(10,1); % Or whatever
V = 1:numel(power) % Or whatever
for k = 1:numel(V)
while power(k) > 10
% Do stuff to reduce power(k) only
end
end
Unless there are operations that you have to do on the whole power array at once, you could use this approach. Otherwise, you could do something like this before you do you operations on the power array:
idxToChg = power > 10;
% Do all operations on arrays, indexing into them with idxToChg
Really depends on exactly how the math works out, how large your arrays are, and what you're really trying to do.
Hope this helps give you some ideas.
-Cam
Thank you so much for your time and effort! I tried indexing but did not get what I wanted so changed the while loop to if else statement in order to make it work on a particular element at one time, and it somehow worked. Thank you again for helping !! :)

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 7 Sep 2017

Commented:

on 16 Sep 2017

Community Treasure Hunt

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

Start Hunting!