# While loop and comparisons

7 views (last 30 days)
Lewis Waswa on 16 Jun 2021
Commented: dpb on 17 Jun 2021
Hi guys,
I have 2 files - file 1 (length n) is a list of conductor resistances from which I am supposed to pick an optimal resistance based on the performance of a network. File 2 is an excel file with network properties from which power flow simulation is conducted.
The output voltage of the network ,V_out is supposed to be kept at less than 1 from my powerflow simulation
If the V_out is more than 1pu, I have to retrieve another conductor (based on its resistance from file 1) with lesser resistance, write this into my excel file 2 which is used in the powerflow simulation and carry out the simulation again.
This is supposed to do that until the V_out is less than 1.
I have written the following code based on an if loop and the second based on a while loop.
The first code returns a single iteration of the power flow.
The second code has an endless loop and it does not do much. How can I tweak this to get the desired results.
function FeederR=get_conFV(cond_table) % Only requires the table of resistances to start
filename = 'C:\Users\inputs.xlsx';
sheet = 1;
Range = "G6:G12";
A=startMaxFV(1,1,1,0); % This function conducts the powerflow and outputs V_out
V_out=A(:,:,1);
% VI_out1=max(VI_out.').'
V_out=max(max(V_out.').');
for i=1:7
for j=1:size(cond_table,1)
FeederR=zeros(7,1);
if V_out > 1 % Greater than 1 pu if A is greater than 1, replace the value in B with a value from table that is less than B
if cond_table(j)< R(i)
R(i)=cond_table(j); % assign the resitance value of the table to the container B
break
i=i+1; % Go to the next branch
elseif R(i)<cond_table(j) % move up the table
j=j+1;
end
end
end
end
FeederR=R(:,1);
xlswrite(filename,FeederR, sheet,Range);
The above code carries out the first comparison alone. I would like to continue conducting the powerflow using the updated values till when V_out is less than or equal to 1.
Below is the code I have tried using
function FeederR=get_conFV(cond_table) % Only requires the table of resistances to start
filename = 'C:\Users\inputs.xlsx';
sheet = 1;
Range = "G6:G12";
A=startMaxFV(1,1,1,0); % This function conducts the powerflow and outputs V_out
V_out=A(:,:,1);
% VI_out1=max(VI_out.').'
V_out=max(max(V_out.').');
for i=1:7
for j=1:size(cond_table,1)
FeederR=zeros(7,1);
while V_out > 1 % Greater than 1 pu if A is greater than 1, replace the value in B with a value from table that is less than B
A=startMaxFV(1,1,1,0); % This function conducts the powerflow and outputs V_out
V_out=A(:,:,1);
% VI_out1=max(VI_out.').'
V_out=max(max(V_out.').');
if cond_table(j)< R(i)
R(i)=cond_table(j); % assign the resitance value of the table to the container B
break
i=i+1; % Go to the next branch
elseif R(i)<cond_table(j) % move up the table
j=j+1;
end
end
end
end
FeederR=R(:,1);
xlswrite(filename,FeederR, sheet,Range);
Any guidance is appreciated.
dpb on 17 Jun 2021
There's too much that's undefined in your code snippet I can't make heads nor tails out of it, sorry.
BUT as to "are no sets of equations. V_out is checked if it is not less than the value, the function runs again." what's the function that runs again if it doesn't contain something to compute the new value with the revised R? It doesn't matter if it's an analytic solution, empirical correlation or just a lookup from an external file if it returns a new target value given a new R, that's all that is needed.
If it is indeed simply a case of stepping through a set of allowable R values, and comparing to some value, why not just precompute the output for the population (or a range across the population) and then you could bracket the value and hone in pretty quickly it would seem.
Sorry if that's not the answer you're looking for precisely to try to fix the given code, but I'd have to be able to see how this is supposed to be working and understand more about what the other pieces that aren't given are to be able to see just what to try to do.
If you're running off the end of the table, that seems to me to be indicative of my first concern that you can't meet the criterion you've set with discrete values.
Clearly at that point you need to have a way to check the index to ensure you don't use one that's out of range; perhaps it's a place for a try...catch...end block where your catch breaks the loop? I dunno if that'll solve your problem; it may just create an endless loop, but it would fix the out-of-range error.

### Categories

Find more on Programming in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!