clc;
clear all;
%                                           
%         Bus bus   R       X               
%         nl  nr   p.u.    p.u.    
    data=[1   6   0.123    0.518                      
          1   4   0.08     0.37                         
          4   6   0.087    0.407   
          5   2   0.282    0.64     
          2   3   0.723    1.05];
nl=data(:,1); nr=data(:,2); R=data(:,3); X=data(:,4);
nbr=length(data(:,1)); nbus = max(max(nl), max(nr));
Z = R + j*X;                            %branch impedance
y= ones(nbr,1)./Z;                     %branch admittance
Ybus=zeros(nbus,nbus);          % initialize Ybus to zero
for k = 1:nbr;   % formation of the off diagonal elements
    if nl(k) > 0 & nr(k) > 0
    Ybus(nl(k),nr(k)) = Ybus(nl(k),nr(k)) - y(k);
    Ybus(nr(k),nl(k)) = Ybus(nl(k),nr(k));
    end
end
for n = 1:nbus       % formation of the diagonal elements
    for k = 1:nbr
    if nl(k) == n | nr(k) == n
    Ybus(n,n) = Ybus(n,n) + y(k);
    else, end
    end
end
Ybus
