Filter löschen
Filter löschen

Index in position 1 exceeds array bounds. Index must not exceed 1.

1 Ansicht (letzte 30 Tage)
João
João am 4 Dez. 2023
Kommentiert: Karl am 5 Dez. 2023
I'm doing this project about the Gauss-Seidel method to solve nodal voltages in power system analysis.
However, in the line of
V(i) = (1/Y(i, i)) * (conj( S(i) / V(i) ) - Y(i, :) * V(i, :));
I get the aforementioned error. I have seen other answers here, but in my specific case I can't seem to get this right for the life of me.
Y = [-6.8 + 1i*42.033 , -6.748+1i*36.725 , 0 , -0.0532+1i*5.29 , 0 , 0 ;
-6.748+1i*36.725, -6.75+1i*42.09 , -0.08+1i*5.35, 0 , 0 , 0 ;
0 , -0.08+1i*5.35 , -0.0795+1i*5.3497 , 0 , 0 , 0 ;
-0.0532+1i*5.29 , 0 , 0 , -12.3428+1i*134.8 , -12.29+1i*129.2 , 0 ;
0 , 0 , 0 , -12.29+1i*129.2, -0.265+1i*12.19763 , -0.26+1i*13.08 ;
0 , 0 , 0 , 0 , -0.2654+1i*13.08 , -0.265+1i*13.079 ] ;
S2 = 0;
S3 = 150e6*(0.95 + 1i*sin(acos(0.95)));
S4 = 0;
S5 = 0;
S6 = 300e6*(0.95 + 1i*sin(acos(0.95)));
S = [0, S2, S3, S4, S5, S6];
% B) Gauss-Seidel method to solve for nodal voltages
V = [1.05, 1.05, 1.05, 1.05, 1.05, 1.05]; % Initial guess
max_iter = 1000;
tolerance = 1e-6;
for iter = 1:max_iter
for i = 2:6
dVmax = 0;
V_old = 1.05;
V(i) = (1/Y(i, i)) * (conj( S(i) / V(i) ) - Y(i, :) * V(i, :));
dV = abs(V(i) - V_old );
if (dV > dVmax)
dVmax = dV;
V_old = V(i);
else
V_old = V(i);
end
end
if max(abs(V - V_old)) < tolerance
break;
end
display (V);
end
Index in position 1 exceeds array bounds. Index must not exceed 1.

Antworten (1)

Walter Roberson
Walter Roberson am 5 Dez. 2023
V = [1.05, 1.05, 1.05, 1.05, 1.05, 1.05]; % Initial guess
Row vector.
V(i) = (1/Y(i, i)) * (conj( S(i) / V(i) ) - Y(i, :) * V(i, :));
The V(i) are doing linear indexing, which is consistent with the hypothesis that V is a vector. But the V(i,:) is row indexing, implying that V is a 2D array.
If you are planning to use V to store a "record" of the different V values, then you need to be consistent and index it by row everywhere you use it, and you would need to store into new rows.
But as long as you are indexing V by scalar then the implication is that V is a vector, and you should not use row indexing on it.
Your code is more consistent with V being a row vector only.
  2 Kommentare
João
João am 5 Dez. 2023
But when I consider V to be a row-only vector, such as V(i) or V(1, i), I get the following error:
"Unable to perform assignment because the left and right sides have a different number of elements."
Karl
Karl am 5 Dez. 2023
The error occurs because V is initialised as a vector of scalars, but you're trying to assign a vector to one of its elements. Compare with:
a = [1 2 3];
a(1) = 4; % this works
a(1) = [4 5]; % this gives your error
The right-hand side of your expression for V(i) evaluates to a vector because Y(i,:) is a vector. After your code fails, try checking the values of the individual pieces of the right-hand side of the expression, to see if there are any that aren't what you expect.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by