Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

how possible to terminate a loop in case my condition is not met?

1 vue (au cours des 30 derniers jours)
Rengin
Rengin le 20 Avr 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
Below I pasted a part of my code. it is a sort of simulation program. Loading_B matrix created in the end is (nparticle x ngen) sized matrix. Each particle is assigned for one row in Loading_B matrix. In case an element for an assigned particle in a row is zero, I want to go back the beginning of the main loop and start generating random v_last values till I meet all requirements to fulfill all rows in Loading_B matrix. How can I do that?
tap = 2.5; %tap step in per cent
tapmax = 5;
nparticle = 100;
nbus = 25;
ngen = 5;
vmin = 0.95;
vmax = 1.05;
%Initialize the positions of the particles
Xs=zeros(nparticle,2);
TapPositions=zeros(nparticle,2); % Initial position of particles (tap position/control parameter)
Voltages=zeros(nparticle,ngen); % Initial position of particles (voltage/control parameter)
Loading_A=zeros(nbus,nparticle);
% loading initial line and bus data
data25
% Busbars at where WFs are connected
n_WF1=4; % 30 MVA
n_WF2=7; % 60 MVA
n_WF3=8; % 45 MVA
n_WF4=9; % 21 MVA
n_WF5=11; % 60 MVA
% Nominal powers of WFs [MW]
NominalWF = zeros(n,1);
NominalWF(n_WF1,1)=30;
NominalWF(n_WF2,1)=60;
NominalWF(n_WF3,1)=45;
NominalWF(n_WF4,1)=21;
NominalWF(n_WF5,1)=60;
for particle=1:nparticle
v_last1 = vmin + (vmax-vmin).* rand (particle,1);
v_last2 = vmin + (vmax-vmin).* rand (particle,1);
v_last3 = vmin + (vmax-vmin).* rand (particle,1);
v_last4 = vmin + (vmax-vmin).* rand (particle,1);
v_last5 = vmin + (vmax-vmin).* rand (particle,1);
% Initial reference voltages of WFs
voltage = [v_last1 v_last2 v_last3 v_last4 v_last5];
% Put reference voltages of WFs into bus-data
busdata(n_WF1,3)=voltage(particle,1);
busdata(n_WF2,3)=voltage(particle,2);
busdata(n_WF3,3)=voltage(particle,3);
busdata(n_WF4,3)=voltage(particle,4);
busdata(n_WF5,3)=voltage(particle,5);
% Initial tap positions of transformers
tap_p1 = round(-tapmax+2*tapmax*rand(1));
tap_p2 = round(-tapmax+2*tapmax*rand(1));
tap_1 = tap_p1*tap/100;
tap_2 = tap_p2*tap/100;
TapPositions(particle,1)=tap_p1;
TapPositions(particle,2)=tap_p2;
% Change the tap position of the transformer (TR1 and TR2)
linedata(1,6) = 1 + tap_1;
linedata(2,6) = 1 + tap_2;
%After initializing reference voltages of WFs and transformer tap
%positions, execute Load (Power) Flow
LFYBUS % Bus Admittance Matrix
LFNEWTON_edited % Newton-Raphson Method
BUSOUT % Power Flow Solution
% Calculation of apparent power at CPs and of WFs
Sg= sqrt(Qg.^2+Pg.^2);
% Reactive power at CPs (QCP1 and QCP2)
objective1=Qg(1,1);
objective2=Qg(2,1);
Xs(particle,1)=abs(objective1);
Xs(particle,2)=abs(objective2);
for i=1:n % n, the number of busbars
if Sg(i,1)~=0 && NominalWF(i,1)~=0
Loading_A(i,particle)=(Sg(i,1)./NominalWF(i,1))*100;
end
Loading_B = (Loading_A(sum(Loading_A') ~= 0, :))';
end
% Reactive power at CPs within WF loading limits (LoadingWF<=150)
for g=1:ngen
if Loading_B(particle,g)>110;
...
...
...
end
end
end
end

Réponses (2)

the cyclist
the cyclist le 20 Avr 2015
Modifié(e) : the cyclist le 20 Avr 2015
Sounds like you want the continue command.
Alternatively, you could rewrite your main loop as a while loop rather than a for loop.

Rengin
Rengin le 20 Avr 2015
I think I should use "while true" and "break" comments in somewhere. The problem is that I can only check the elements in a row one by one. If an element in the row doesn't meet the condition which I put, I don't want to check the following element. I want to go back the beginning of main loop and generate random values again till I find suitable condition for the same row.

Community Treasure Hunt

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

Start Hunting!

Translated by