用matlab写一个潮流计算获得节点电压幅值真值,且电压幅值为0的代码
时间: 2024-04-30 19:19:08 浏览: 283
以下是一个简单的MATLAB代码来计算潮流,并获得节点电压幅值的真值。在该代码中,我们使用了潮流计算中的牛顿-拉夫森法来解决潮流方程组。为了找到电压幅值为0的节点,我们将检查计算得到的电压幅值是否小于一个非常小的阈值(例如0.0001),如果是,则将其视为0。
```
% 输入数据
busdata = [1 1.06 0; 2 1.045 0; 3 1.01 0; 4 1.0 0]; % 节点数据
linedata = [1 2 0.02 0.06 0; 1 3 0.05 0.19 0; 2 3 0.03 0.08 0; 2 4 0.04 0.11 0; 3 4 0.06 0.13 0]; % 线路数据
S = [0.9; 0.4; 0.3; 0.2]; % 负荷数据
% 初始化
nbus = size(busdata,1); % 节点数
nline = size(linedata,1); % 线路数
P = zeros(nbus,1); % 节点有功注入
Q = zeros(nbus,1); % 节点无功注入
V = busdata(:,2); % 节点电压幅值
theta = zeros(nbus,1); % 节点相角
Y = calcY(busdata,linedata); % 计算导纳矩阵
% 牛顿-拉夫森法迭代
iter = 0;
maxiter = 20;
tol = 0.0001;
while iter < maxiter
% 计算节点注入功率
for i = 1:nbus
for j = 1:nbus
P(i) = P(i) + V(i)*V(j)*(real(Y(i,j))*cos(theta(i)-theta(j)) + imag(Y(i,j))*sin(theta(i)-theta(j)));
Q(i) = Q(i) + V(i)*V(j)*(real(Y(i,j))*sin(theta(i)-theta(j)) - imag(Y(i,j))*cos(theta(i)-theta(j)));
end
P(i) = P(i) + busdata(i,3);
Q(i) = Q(i) + busdata(i,4);
end
% 计算雅可比矩阵
J = calcJ(busdata,Y,V,theta);
% 计算方程残差
F = [P-S; Q];
% 计算方程的增量
dx = J\(-F);
% 更新节点电压和相角
theta = theta + dx(1:nbus);
V = V + dx(nbus+1:end);
% 检查是否收敛
if max(abs(F)) < tol
break;
end
iter = iter + 1;
end
% 输出结果
disp('节点电压幅值:');
disp(V);
% 找到电压幅值为0的节点
disp('电压幅值为0的节点:');
for i = 1:nbus
if abs(V(i)) < 0.0001
disp(busdata(i,1));
end
end
% 计算导纳矩阵
function Y = calcY(busdata,linedata)
nbus = size(busdata,1);
nline = size(linedata,1);
Y = zeros(nbus);
for i = 1:nline
Y(linedata(i,1),linedata(i,2)) = -1/(linedata(i,3) + 1i*linedata(i,4));
Y(linedata(i,2),linedata(i,1)) = Y(linedata(i,1),linedata(i,2));
end
for i = 1:nbus
Y(i,i) = -sum(Y(i,:));
end
end
% 计算雅可比矩阵
function J = calcJ(busdata,Y,V,theta)
nbus = size(busdata,1);
J = zeros(2*nbus);
for i = 1:nbus
for j = 1:nbus
if i == j
J(i,j) = -Q(i)/V(i) - imag(Y(i,i));
J(i,j+nbus) = P(i)/V(i) + real(Y(i,i));
J(i+nbus,j) = P(i)/V(i) - real(Y(i,i));
J(i+nbus,j+nbus) = Q(i)/V(i) - imag(Y(i,i));
else
J(i,j) = V(i)*V(j)*(real(Y(i,j))*sin(theta(i)-theta(j)) - imag(Y(i,j))*cos(theta(i)-theta(j)));
J(i,j+nbus) = V(j)*(real(Y(i,j))*cos(theta(i)-theta(j)) + imag(Y(i,j))*sin(theta(i)-theta(j)));
J(i+nbus,j) = V(i)*(real(Y(i,j))*cos(theta(i)-theta(j)) + imag(Y(i,j))*sin(theta(i)-theta(j)));
J(i+nbus,j+nbus) = V(i)*(-real(Y(i,j))*sin(theta(i)-theta(j)) + imag(Y(i,j))*cos(theta(i)-theta(j)));
end
end
end
end
```
阅读全文