MAKEYBUS Builds the bus admittance matrix and branch admittance matrices. [YBUS, YF, YT] = MAKEYBUS(MPC) [YBUS, YF, YT] = MAKEYBUS(BASEMVA, BUS, BRANCH) Returns the full bus admittance matrix (i.e. for all buses) and the matrices YF and YT which, when multiplied by a complex voltage vector, yield the vector currents injected into each line from the "from" and "to" buses respectively of each line. Does appropriate conversions to p.u. Inputs can be a MATPOWER case struct or individual BASEMVA, BUS and BRANCH values. Bus numbers must be consecutive beginning at 1 (internal ordering). See also MAKEJAC, MAKESBUS, EXT2INT.
0001 function [Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch) 0002 %MAKEYBUS Builds the bus admittance matrix and branch admittance matrices. 0003 % [YBUS, YF, YT] = MAKEYBUS(MPC) 0004 % [YBUS, YF, YT] = MAKEYBUS(BASEMVA, BUS, BRANCH) 0005 % 0006 % Returns the full bus admittance matrix (i.e. for all buses) and the 0007 % matrices YF and YT which, when multiplied by a complex voltage vector, 0008 % yield the vector currents injected into each line from the "from" and 0009 % "to" buses respectively of each line. Does appropriate conversions to p.u. 0010 % Inputs can be a MATPOWER case struct or individual BASEMVA, BUS and 0011 % BRANCH values. Bus numbers must be consecutive beginning at 1 (internal 0012 % ordering). 0013 % 0014 % See also MAKEJAC, MAKESBUS, EXT2INT. 0015 0016 % MATPOWER 0017 % Copyright (c) 1996-2015 by Power System Engineering Research Center (PSERC) 0018 % by Ray Zimmerman, PSERC Cornell 0019 % 0020 % $Id: makeYbus.m 2644 2015-03-11 19:34:22Z ray $ 0021 % 0022 % This file is part of MATPOWER. 0023 % Covered by the 3-clause BSD License (see LICENSE file for details). 0024 % See http://www.pserc.cornell.edu/matpower/ for more info. 0025 0026 if nargin < 3 0027 mpc = baseMVA; 0028 baseMVA = mpc.baseMVA; 0029 bus = mpc.bus; 0030 branch = mpc.branch; 0031 end 0032 0033 %% constants 0034 nb = size(bus, 1); %% number of buses 0035 nl = size(branch, 1); %% number of lines 0036 0037 %% define named indices into bus, branch matrices 0038 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ... 0039 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus; 0040 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ... 0041 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ... 0042 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch; 0043 0044 %% check that bus numbers are equal to indices to bus (one set of bus numbers) 0045 if any(bus(:, BUS_I) ~= (1:nb)') 0046 error('buses must appear in order by bus number') 0047 end 0048 0049 %% for each branch, compute the elements of the branch admittance matrix where 0050 %% 0051 %% | If | | Yff Yft | | Vf | 0052 %% | | = | | * | | 0053 %% | It | | Ytf Ytt | | Vt | 0054 %% 0055 stat = branch(:, BR_STATUS); %% ones at in-service branches 0056 Ys = stat ./ (branch(:, BR_R) + 1j * branch(:, BR_X)); %% series admittance 0057 Bc = stat .* branch(:, BR_B); %% line charging susceptance 0058 tap = ones(nl, 1); %% default tap ratio = 1 0059 i = find(branch(:, TAP)); %% indices of non-zero tap ratios 0060 tap(i) = branch(i, TAP); %% assign non-zero tap ratios 0061 tap = tap .* exp(1j*pi/180 * branch(:, SHIFT)); %% add phase shifters 0062 Ytt = Ys + 1j*Bc/2; 0063 Yff = Ytt ./ (tap .* conj(tap)); 0064 Yft = - Ys ./ conj(tap); 0065 Ytf = - Ys ./ tap; 0066 0067 %% compute shunt admittance 0068 %% if Psh is the real power consumed by the shunt at V = 1.0 p.u. 0069 %% and Qsh is the reactive power injected by the shunt at V = 1.0 p.u. 0070 %% then Psh - j Qsh = V * conj(Ysh * V) = conj(Ysh) = Gs - j Bs, 0071 %% i.e. Ysh = Psh + j Qsh, so ... 0072 Ysh = (bus(:, GS) + 1j * bus(:, BS)) / baseMVA; %% vector of shunt admittances 0073 0074 %% build connection matrices 0075 f = branch(:, F_BUS); %% list of "from" buses 0076 t = branch(:, T_BUS); %% list of "to" buses 0077 Cf = sparse(1:nl, f, ones(nl, 1), nl, nb); %% connection matrix for line & from buses 0078 Ct = sparse(1:nl, t, ones(nl, 1), nl, nb); %% connection matrix for line & to buses 0079 0080 %% build Yf and Yt such that Yf * V is the vector of complex branch currents injected 0081 %% at each branch's "from" bus, and Yt is the same for the "to" bus end 0082 i = [1:nl; 1:nl]'; %% double set of row indices 0083 Yf = sparse(i, [f; t], [Yff; Yft], nl, nb); 0084 Yt = sparse(i, [f; t], [Ytf; Ytt], nl, nb); 0085 % Yf = spdiags(Yff, 0, nl, nl) * Cf + spdiags(Yft, 0, nl, nl) * Ct; 0086 % Yt = spdiags(Ytf, 0, nl, nl) * Cf + spdiags(Ytt, 0, nl, nl) * Ct; 0087 0088 %% build Ybus 0089 Ybus = Cf' * Yf + Ct' * Yt + ... %% branch admittances 0090 sparse(1:nb, 1:nb, Ysh, nb, nb); %% shunt admittance