MATLAB: I got error in the code “Powerflow Newton Raphson 2×2 matrix”. How to fixed it

simpowersystems

syms mV1 aV1 mV2 aV2 mV3 aV3
syms mY21 aY21 mY22 aY22 mY23 aY23
syms mY31 aY31 mY32 aY32 mY33 aY33
P2j = mV2*mV1*mY21*cos(aY21-aV2+aV1) + mV2*mV2*mY22*cos(aY22);
Q2j = -mV2*mV1*mY21*sin(aY21-aV2+aV1) - mV2*mV2*mY22*sin(aY22);
J = jacobian([P2j,Q2j],[aV2,mV2]);
Y =[20-40i -20+40i;-20+40i 20-40i;];
V1 = 1.0;
V2 = 1.0;
S2 = -(2+4i);
aV2 = 0;
mV2 = 1;
X = [aV2;mV2];
mV1 = 1.0; aV1 = 0;
mY12 = abs(Y(1,1)); aY12 = angle(Y(1,1));
mY11 = abs(Y(1,1)); aY11 = angle(Y(1,1));
dP2 = real(S2) - eval(P2j);
dQ2 = imag(S2) - eval(Q2j);
dC = [dP2;dQ2];
error = sum(abs(dC));
maxMis = 0.0005;
iter = 0;
while error>=maxMis && iter<10 <---------%%%%%error is here!!
iter = iter+1;
Jx = eval(J)
dX = Jx\dC;
X = X+dX;
aV2 = X(1,1);
mV2 = X(2,1);
dP2 = real(S2) - eval(P2j);
dQ2 = imag(S2) - eval(Q2j);
dC = [dP2;dQ2];
G = [eval(P2j);eval(Q2j)];
error = sum(abs(dC));
Q1 = -mV1*mV1*mY11*sin(aY11) - mV1*mV2*mY12*sin(aY12);
P1 = mV1*mV1*mY11*cos(aY11) + mV1*mV2*mY12*cos(aY12);
H = [Q1;P1];
S12 = V1*(conj(V1)-conj(V2))*(-20+40i);
S21 = V2*(conj(V2)-conj(V1))*(-20+40i);
Sl =[S12;S21];
fprintf('%g',iter), disp([dC,G,X,H,Sl])
end
After running program, it said
Conversion to logical from sym is not possible.
Error in Project1 (line 32)
while error>=maxMis && iter<10

Best Answer

You should never eval() a symbolic variable or expression: the language used for symbolic expressions is not exactly the same as MATLAB. You should use subs() if you want to bring in values for variables used in expressions.
You do not assign values to aY21 or aY22 but error is defined in terms of them. What do you expect it to mean to use >= to compare an expression with free variables to a specific numeric value to control the bounds of a while loop ?
In some contexts you can replace comparisons of symbolic expressions with piecewise(), but you would not do that as a bounds test for while -- you would not know whether the condition was satisfied until after you had left the loop for other reasons.