Index in position 1 exceeds array bounds. Index must not exceed 1.
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 5 Dec 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 Comments
Karl
on 5 Dec 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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!