While loop and comparisons

7 views (last 30 days)
Lewis Waswa
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";
R=xlsread(filename,sheet,Range);
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";
R=xlsread(filename,sheet,Range);
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.
  5 Comments
Lewis Waswa
Lewis Waswa on 17 Jun 2021
Thank you @dpb. However my problem is basic I think as the problem is where to place the while loop in that code. There are no sets of equations. V_out is checked if it is not less than the value, the function runs again. It is so much of not a computation problem as it is a selection problem. Please see my revised code.
function [V_out,FeederR]=get_con3(cond_table)
filename = 'C:\Users\22451102\Desktop\inputs_JP.xlsx';
sheet = 1;
Range = "G6:G12";
R=xlsread(filename,sheet,Range); % Initial feeder resistance
for i=1:7 % no. of branches to check
for j=1:size(cond_table,1) % no. of conductors in the conductor table whose resistances we compare with R
A=startgetV(1,1,0,0)
V_out=A(:,1,1) % initialize V_out for comparison
FeederR=zeros(7,1);
if( V_out(i,:) <= 0.95) % First condition
FeederR=R(:,1) ; % output the Feeder as it is
V_out=V_out(i,:); % output the V_out and get out of the loop
break
else
while V_out(i,:) > 0.95 % check the second condition
if cond_table(j)<=R(i) % check that the replacement resistance is less than the initial one
R(i)=cond_table(j) % Replace the initial resistance
FeederR=R(:,1); % Update the Feeder
xlswrite(filename,FeederR, sheet,Range) % Overwrite the initial feeder details
i=i+1; % move to the next conductor and check
break
else
j=j+1
end
end
end
end
end
The question would be, how do I terminate the loop once I have checked all the R(i). My program returns an error
dpb
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.

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!