loop index 'i' is changed inside of a FOR loop
5 次查看(过去 30 天)
显示 更早的评论
Dear MATLAB users,
I need an explanation on the reason why the i in line 23 is red-underlined in the program and the error discription is "loop index 'i' is changed inside of a FOR loop". I tried to remove line 23 so that line 4 could stand for line 23 but the iteration values of P and Q were in complex instead of scalar. Please what could be the problem and what is the way out? Thank you
% while (V_tolerance > 1e-7 || V_angle_tol > 1e-7 || w_tolerance > 1e-7)
.
.
.
.
% Computing V
4 for i = 1:nbus
sumyv = 0;
for k = 1:nbus
if i ~= k
sumyv = sumyv + YBUS(i,k)* V(k); % Vk * Yik
end
end
% calculating the active and reactive power at the generator buses
23 for i = 1:nbus
for j = 1 : nbus
if type(i) == 2 % Computing Pi & Qi for Generator buses
delta(i) = ((Pm(i) - P(i))/Dpu);
P(i) = P(i) + V(i)*V(j)*(G(i,j)*cos(delta(i)-delta(j)) + ...
B(i,j)*sin(delta(i)-delta(j)))+ PO(i)-PL(i)
Q(i) = Q(i) + V(i)*V(j)*(G(i,j)*sin(delta(i)-delta(j)) - ...
B(i,j)*cos(delta(i)-delta(j)))+ QO(i)-QL(i)
end
end
end
V(i) = (1/YBUS(i,i))*((P(i)-1i*Q(i))/conj(V(i)) - sumyv); % Compute Bus Voltages
V_mag(i) = abs(Vprev(i)) + acc1*(abs(V(i))-abs(Vprev(i)));
V_ang(i) = angle(Vprev(i)) + acc2*(angle(V(i))-angle(Vprev(i)));
delta(i) = delta_prev(i) + acc3*(delta(i) - delta_prev(i));
% V(i) = Vprev(i) + acc1*(V(i)-Vprev(i));
V(i) = V_mag(i)*exp(1i*V_ang(i));
if (i == 1 || i == 2)
V_mag(i) = 1;
[x, y] = pol2cart(angle(V(i)),V_mag(i));
V(i)= x+1i*y;
end
if i == 3
[a, b] = pol2cart(0,abs(V(i)));
V(i)= a+1i*b;
end
end
2 个评论
Torsten
2022-12-26
The loop indices in line 4 and line 23 cannot both be named "i" because the loop starting at line 4 ha snot yet been closed when the loop at line 23 starts.
回答(2 个)
Sulaymon Eshkabilov
2022-12-25
Because this part of your code is outside of the set loop:
end % Wrongly placed
...
V(i) = (1/YBUS(i,i))*((P(i)-1i*Q(i))/conj(V(i)) - sumyv); % Compute Bus Voltages
V_mag(i) = abs(Vprev(i)) + acc1*(abs(V(i))-abs(Vprev(i)));
V_ang(i) = angle(Vprev(i)) + acc2*(angle(V(i))-angle(Vprev(i)));
delta(i) = delta_prev(i) + acc3*(delta(i) - delta_prev(i));
3 个评论
Sulaymon Eshkabilov
2023-1-2
See the previous loops where one "end" is missing:
4 for i = 1:nbus
sumyv = 0;
for k = 1:nbus
if i ~= k
sumyv = sumyv + YBUS(i,k)* V(k); % Vk * Yik
end
end
% calculating the active and reactive power at the generator buses
"end" is missing
Sulaymon Eshkabilov
2023-1-2
% See the below given answer:
Or you had better combine two loops to make your simulation more efficient, e.g.:
...
for i = 1:nbus
sumyv = 0;
for k = 1:nbus
if i ~= k
sumyv = sumyv + YBUS(i,k)* V(k); % Vk * Yik
end
% end
% calculating the active and reactive power at the generator buses
% 23 for i = 1:nbus
% for j = 1 : nbus
if type(i) == 2 % Computing Pi & Qi for Generator buses
delta(i) = ((Pm(i) - P(i))/Dpu);
P(i) = P(i) + V(i)*V(j)*(G(i,j)*cos(delta(i)-delta(j)) + ...
B(i,j)*sin(delta(i)-delta(j)))+ PO(i)-PL(i)
Q(i) = Q(i) + V(i)*V(j)*(G(i,j)*sin(delta(i)-delta(j)) - ...
B(i,j)*cos(delta(i)-delta(j)))+ QO(i)-QL(i)
end
end
% end
V(i) = (1/YBUS(i,i))*((P(i)-1i*Q(i))/conj(V(i)) - sumyv); % Compute Bus Voltages
V_mag(i) = abs(Vprev(i)) + acc1*(abs(V(i))-abs(Vprev(i)));
V_ang(i) = angle(Vprev(i)) + acc2*(angle(V(i))-angle(Vprev(i)));
delta(i) = delta_prev(i) + acc3*(delta(i) - delta_prev(i));
% V(i) = Vprev(i) + acc1*(V(i)-Vprev(i));
V(i) = V_mag(i)*exp(1i*V_ang(i));
if (i == 1 || i == 2)
V_mag(i) = 1;
[x, y] = pol2cart(angle(V(i)),V_mag(i));
V(i)= x+1i*y;
end
if i == 3
[a, b] = pol2cart(0,abs(V(i)));
V(i)= a+1i*b;
end
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!