用matlab写一个潮流计算获得节点电压幅值真值并生成图的代码
时间: 2023-06-14 08:07:12 浏览: 191
潮流计算matlab代码
以下是一个简单的MATLAB代码,用于执行节点电压幅值的潮流计算并生成电压幅值的图形。该代码使用牛顿-拉夫森方法解决了潮流方程,并将节点电压幅值和相位角以及功率因数输出到命令窗口。
```
% 输入数据
busdata = [1 1.05 0; 2 1.0 0; 3 1.0 0; 4 1.0 0]; % 节点数据
linedata = [1 2 0.1 0.2 0.04; 1 3 0.05 0.1 0.02; 2 4 0.05 0.1 0.02; 3 4 0.1 0.3 0.08]; % 线路数据
S = [1 -0.5]; % 负荷数据
% 初始化变量
nbus = size(busdata,1);
nline = size(linedata,1);
P = zeros(nbus,1);
Q = zeros(nbus,1);
V = busdata(:,2);
theta = busdata(:,3)*pi/180;
G = zeros(nbus, nbus);
B = zeros(nbus, nbus);
% 计算导纳矩阵
for i=1:nline
from = linedata(i,1);
to = linedata(i,2);
r = linedata(i,3);
x = linedata(i,4);
b = linedata(i,5);
y = 1/(r + 1j*x);
G(from, to) = -real(y);
G(to, from) = -real(y);
B(from, to) = -imag(y) + b/2;
B(to, from) = -imag(y) + b/2;
G(from, from) = G(from, from) + real(y);
G(to, to) = G(to, to) + real(y);
B(from, from) = B(from, from) + b/2;
B(to, to) = B(to, to) + b/2;
end
% 计算节点功率
for i=1:nbus
for j=1:nbus
P(i) = P(i) + V(i)*V(j)*(G(i,j)*cos(theta(i)-theta(j)) + B(i,j)*sin(theta(i)-theta(j)));
Q(i) = Q(i) + V(i)*V(j)*(G(i,j)*sin(theta(i)-theta(j)) - B(i,j)*cos(theta(i)-theta(j)));
end
end
% 计算雅可比矩阵
J = zeros(2*nbus, 2*nbus);
for i=1:nbus
for j=1:nbus
if i == j
J(i,j+nbus) = 2*V(i)*G(i,i);
J(i+nbus,j) = -2*V(i)*B(i,i);
else
J(i,j+nbus) = V(i)*V(j)*(G(i,j)*sin(theta(i)-theta(j)) - B(i,j)*cos(theta(i)-theta(j)));
J(i+nbus,j) = V(i)*V(j)*(G(i,j)*cos(theta(i)-theta(j)) + B(i,j)*sin(theta(i)-theta(j)));
end
end
end
% 牛顿-拉夫森迭代求解潮流方程
iter = 0;
tol = 1e-8;
maxiter = 20;
delta = [1; 1];
while (max(abs(delta)) > tol) && (iter < maxiter)
iter = iter + 1;
F = zeros(2*nbus,1);
for i=1:nbus
F(i) = P(i) - V(i)^2*G(i,i) - V(i)*sum(V.*G(i,:).*cos(theta - theta(i)) - V.*B(i,:).*sin(theta - theta(i)));
F(i+nbus) = Q(i) - V(i)^2*B(i,i) - V(i)*sum(V.*G(i,:).*sin(theta - theta(i)) + V.*B(i,:).*cos(theta - theta(i)));
end
delta = -J\F;
theta = theta + delta(1:nbus);
V = V + delta(nbus+1:end);
end
% 计算节点电压幅值和相位角以及功率因数
Vmag = abs(V);
Vang = angle(V)*180/pi;
PF = P(2)/sqrt(P(2)^2+Q(2)^2);
% 画出节点电压幅值图
figure;
plot(1:nbus,Vmag,'o-','MarkerSize',10,'LineWidth',2);
xlabel('节点编号');
ylabel('电压幅值');
title('节点电压幅值');```
这段代码的输入数据包括节点数据、线路数据和负载数据。其中,节点数据包括节点编号、初始电压幅值和相位角,线路数据包括起始节点、终止节点、电阻、电抗和电纳,负载数据包括负荷节点和有功功率和无功功率。
该代码使用牛顿-拉夫森方法来解决潮流方程,并通过迭代计算节点电压幅值和相位角以及功率因数。最后,它生成一个节点电压幅值图,用于显示各个节点的电压幅值。
阅读全文