loop index 'i' is changed inside of a FOR loop

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 Comments

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.
@Torsten, It was already closed with end before the % calculating the active and reactive power at the generator buses i.e
end
% calculating the active and reactive power at the generator buses.
May be like this:
for i = 1:nbus
sumyv = 0;
for k = 1:nbus
if i ~= k
sumyv = sumyv + YBUS(i,k)* V(k); % Vk * Yik
end
V(i) = (1/YBUS(i,i))*((P(i)-1i*Q(i))/conj(V(i)) - sumyv); % Compute Bus Voltages
end
% calculating the active and reactive power at the generator buses

Sign in to comment.

Answers (2)

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 Comments

@Sulaymon Eshkabilov, Pls what do you mean? It was used to closed that loop
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, thank you for reply. I tried it but it was rather leading to results far from the expected, though it does not converge as well. Expected result is:
delta = [ 0.268; 0.1774; 0; 0;x]; x = cant say but I am seeing the results staying at
delta = 1e-2* [ 0.3; 0.17644; 0; 0;x];
Please, I would appreciate if you can help me look at it, I think I am close to the result, may be one syntax error is not making it converge. Pls kindly help me with your email so that i send it. For protection of my research I cant upload the whole program here. My email is sanusielect@yahoo.com
Thank you

Sign in to comment.

% 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

Categories

Find more on General Physics in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 25 Dec 2022

Commented:

on 3 Jan 2023

Community Treasure Hunt

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

Start Hunting!