Gauss Seidel Method Looping error ( Index exceeds the number of array elements (1). in LINE 54) V2n

4 views (last 30 days)
MVA_Base = 100; %base MVA
V1 = 1.0; %fixed voltage
V2o = 1.0; %fixed voltage
V3o = 1.0; %gauss voltage
P2 = 0.6; % given active power on bus 2
Q1 = 0; % reactive power on bus 1
Q2 = 0; % reactive power on bus 2
P3 = -0.8; % given active power on bus 3
Q3 = -0.6; % reactive power on bus 3
X12 = 0.4; % reactance between bus 1 & 2
X13 = 0.4; % reactance between bus 1 & 3
X23 = 0.3; % reactance between bus 2 & 3
% Ybus
Y11 = (1/(1i*X12)) + (1/(1i*X13));
Y12 = -1/(1i*X12);
Y13 = -1/(1i*X13);
Y21 = -1/(1i*X12);
Y22 = (1/(1i*X12)) + (1/(1i*X23));
Y23 = -1/(1i*X23);
Y31 = -1/(1i*X13);
Y32 = -1/(1i*X23);
Y33 = (1/(1i*X13)) + (1/(1i*X23));
YB = [ Y11 Y12 Y13;
Y21 Y22 Y23;
Y31 Y32 Y33];
n=0; % Number of Iteration
type = [1 2 3]; % Bus Type: 1. Slack Bus 2. PV Bus 3.PQ Bus
V = [ V1 V2o V3o];
Q = [ Q1 Q2 Q3];
Vprev = V ;
Qprev = Q2;
P=P2-P3;
Q=Q2-Q3;
Vmagfixed = 1.0;
tolerance=1;
nbus = 3; % total number of buses.
while (tolerance > 0.00001)
for i = 2:nbus
sigYV = 0;
for k = 1:nbus
if i~=k
sigYV = sigYV + YB(i,k)* V(k);
end
end
if type(i) == 2
Q2n = -imag(conj(V2o)*(sigYV + YB(i,i)*V2o)); % Computing Qi for PV bus
V2n = (1/YB(i,i))*((P(i)-1i*Q2n)/conj(V(i))-sigYV); % Compute Bus Voltage.
end
if type(i) == 3
V3n = (1/YB(i,i))*((P(i)-1i*Q(i))/conj(V(i)) - sigYV); % Compute Bus Voltage.
end
if type(i) == 2
V2n = pol2cart (abs(Vmagfixed), angle(V2n)*180/pi);
end
end
n = n + 1; % Increment iteration count.
DeltaV2 = abs(V2n-V2o);
DeltaV3 = abs(V3n-V3o);
DeltaQ2 = abs(Q2n-Q2);
tolerance = max(DeltaV2,DeltaV3,DeltaQ2);
if tolerance <=0.00001
break;
else
V2o = V2n;
V3o = V3n;
end
end
Iteration;
V;
%apparent power of line
S12=(conj(V1)*V1*Y12)-(conj(V1)*V2*Y12);
S21=(conj(V2)*V2*Y21)-(conj(V2)*V1*Y21);
S23=(conj(V2)*V2*Y23)-(conj(V2)*V3*Y23);
S32=(conj(V3)*V3*Y32)-(conj(V3)*V2*Y32);
S31=(conj(V3)*V3*Y31)-(conj(V3)*V1*Y31);
S13=(conj(V1)*V1*Y13)-(conj(V1)*V3*Y13);
%apparent power on buses
S1=S12+S13;
S2=S23+S21;
S3=S31+S32;
  2 Comments
Walter Roberson
Walter Roberson on 10 Oct 2020
V2n = (1/YB(i,i))*((P(i)-1i*Q2n)/conj(V(i))-sigYV); % Compute Bus Voltage.
You initilized P as the difference between two scalars, so it is a scalar.
You index P at i, so when i is more than 1 you have a problem.
You never assign into further P locations, so your P would have to have nbus or more elements.

Sign in to comment.

Answers (1)

Shishir Reddy
Shishir Reddy on 28 May 2025
Edited: Shishir Reddy on 28 May 2025
Hi Parth
The error you are encountering occurs at the following line in your Gauss-Seidel loop.
V2n = (1/YB(i,i))*((P(i)-1i*Q2n)/conj(V(i))-sigYV);
The root cause of this issue is that you have defined ‘P’ and ‘Q’ as scalars earlier in your code.
P = P2 - P3;
Q = Q2 - Q3;
This overwrites P and Q as single numbers (1-element arrays), which causes an indexing error when you later try to access P(i) or Q(i) inside the loop, especially when i = 2 or 3.
To fix this, ’P’ and ‘Q’ have to be vectors with size of nbus, which is 3 in this case.
P = [0, P2, P3];
Q = [0, Q2, Q3];
It is important to make sure that you initialize these values before the Gauss-Seidel loop.
Also, you have another issue with the 'max' function. The following line in the code will throw an error because 'max' only accepts one or two inputs.
max(DeltaV2, DeltaV3, DeltaQ2)
The values can be wraped in an array like this:
tolerance = max([DeltaV2, DeltaV3, DeltaQ2]);
These two changes should resolve the runtime errors in your Gauss-Seidel implementation.
I hope this helps.

Categories

Find more on Mathematics and Optimization 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!