用matlab写一个潮流计算获得节点电压幅值真值为0的代码
时间: 2024-05-13 07:16:23 浏览: 92
以下是用MATLAB编写的潮流计算程序,其中包括计算节点电压幅值和判断节点电压幅值是否为0的代码:
```matlab
% 输入数据
busdata = [1 1 0 1.06 0 0;
2 2 0 1.045 0 0;
3 2 0 1.01 0 0;
4 3 0 1.0 0 0;
5 3 0 1.0 0 0];
linedata = [1 2 0.02 0.06 0.06 0;
1 3 0.03 0.08 0.05 0;
2 3 0.01 0.03 0.02 0;
2 4 0.04 0.12 0.1 0;
3 5 0.02 0.06 0.04 0;
4 5 0.01 0.04 0.03 0];
% 构建节点导纳矩阵
nbus = max(busdata(:,1));
Ybus = zeros(nbus,nbus);
for k = 1:size(linedata,1)
f = linedata(k,1);
t = linedata(k,2);
y = 1/(linedata(k,3) + 1i*linedata(k,4));
Ybus(f,f) = Ybus(f,f) + y + 1i*linedata(k,6)/2;
Ybus(t,t) = Ybus(t,t) + y + 1i*linedata(k,6)/2;
Ybus(f,t) = Ybus(f,t) - y;
Ybus(t,f) = Ybus(t,f) - y;
end
for k = 1:size(busdata,1)
if busdata(k,2) == 1
Ybus(k,k) = Ybus(k,k) + 1i*busdata(k,4)^2/busdata(k,3);
end
end
% 构建负荷功率向量
Pd = busdata(:,5);
Qd = busdata(:,6);
% 构建发电机功率向量
Pg = zeros(nbus,1);
Qg = zeros(nbus,1);
for k = 1:size(busdata,1)
if busdata(k,2) == 2
Pg(k) = busdata(k,5);
Qg(k) = busdata(k,6);
end
end
% 构建节点电压相角和幅值向量
V = ones(nbus,1);
theta = zeros(nbus,1);
% 迭代求解节点电压相角和幅值
iter = 0;
maxiter = 100;
tol = 1e-6;
err = inf;
while err > tol && iter < maxiter
iter = iter + 1;
P = Pg - Pd;
Q = Qg - Qd;
for k = 1:nbus
S = 0;
for m = 1:nbus
S = S + V(m)*(Ybus(k,m)*cos(theta(k)-theta(m)) + Ybus(k,m)*sin(theta(k)-theta(m))*1i);
end
P(k) = P(k) - V(k)^2*real(S);
Q(k) = Q(k) - V(k)^2*imag(S);
end
for k = 2:nbus
P(k) = P(k) - V(k)*V(1)*(Ybus(k,1)*cos(theta(k)-theta(1)) + Ybus(k,1)*sin(theta(k)-theta(1))*1i);
Q(k) = Q(k) - V(k)*V(1)*(Ybus(k,1)*sin(theta(k)-theta(1)) - Ybus(k,1)*cos(theta(k)-theta(1))*1i);
end
for k = 2:nbus
P(k) = P(k) - Q(1)*V(k)*sin(theta(k)-theta(1));
Q(k) = Q(k) + P(1)*V(k)*sin(theta(k)-theta(1));
end
for k = 2:nbus
V(k) = (P(k)^2 + Q(k)^2)^0.5/V(1);
theta(k) = atan2(Q(k),P(k)) - atan2(V(k),V(1));
end
err = max(abs([P;Q]));
end
% 判断节点电压幅值是否为0
if any(abs(V) < 1e-6)
fprintf('存在节点电压幅值为0。\n');
else
fprintf('所有节点电压幅值均不为0。\n');
end
```
其中,输入数据包括节点数据和线路数据,节点数据中包括节点编号、节点类型、节点电压幅值和相角、节点有功和无功负荷,线路数据中包括起始节点编号、终止节点编号、电阻、电抗、电纳以及短路电流等参数。程序首先根据输入数据构建节点导纳矩阵,然后迭代求解节点电压幅值和相角,最后判断节点电压幅值是否为0并输出结果。
阅读全文