%% Program sestaveni impedancni matice vedeni - 2 potahy, 1 zemnici lano
%znaceni: d1223 - potah 1-L2 + potah 2-L3, zemni lano - 0 (navigace ve vektoru - num.3)
%
%% zadani_rozmeru_vedeni_a_konstant_START
%vydalenosti mezi fazovymi vodici[m]
d1112=3.52;
d1113=6.80;
d1213=3.49;
%
d1121=5.00;
d1122=6.81;
d1123=8.50;
%
d1221=d1122;
d1222=6.80;
d1223=6.90;
%
d1321=d1123;
d1322=d1223;
d1323=5.20;
%vzdalenosti od zemniho lana [m]
d110=4.97;
d120=8.42;
d130=11.40;
%polomer alfe [m]
rfaze=10.87*10^(-3);
rzemni=5.3*10^(-3);
%rezistance alfe [ohm/km]
rofaze=0.1181;
rozemni=2.931;
ropudy=0.5; %[ohm.m]
%delka vedeni [km]
l=5.198;
%korekcni soucinitel [ ]
psi=0.81;
%% zadani_rozmeru_vedeni_a_konsatnt_END
%
%% prirazeni stejnych hodnot a pomocne vypocty_START
d2122=d1112;
d2123=d1113;
d2223=d1213;
d210=d110;
d220=d120;
d230=d130;
%hloubka fiktivniho vodice v zemi
Dg=0.178*((ropudy*10^7)^(1/2))/(50^(1/2));
%vypocet odporu
Rg=(pi^2)*50*10^(-4); %ohm/km
R=l*rofaze;
%vytvoreni matic vzdalenosti lan
matd11=[Dg d1112 d1113;d1112 Dg d1213;d1113 d1213 Dg];
matd12=[d1121 d1122 d1123;d1221 d1222 d1223;d1321 d1322 d1323];
matd13=[d110 d120 d130]';
matd21=matd12;
matd22=[Dg d2122 d2123;d2122 Dg d2223;d2123 d2223 Dg];
matd23=matd13;
matd31=matd13';
matd32=matd23';
matd33=[Dg];
matd=[matd11 matd12 matd13;matd21 matd22 matd23;matd31 matd32 matd33];
%% prirazeni stejnych hodnot a pomocne vypocty_END
%
%% sestaveni impedancni matice_START
Zmat=zeros((numel(matd))^0.5,(numel(matd))^0.5);
for i=1:((numel(Zmat))^0.5)
        for k=1:((numel(Zmat))^0.5)
            if i==((numel(Zmat))^0.5) && k==((numel(Zmat))^0.5)
                Zmat(i,k)=rozemni+Rg+j*0.1445*log10(Dg/(psi*rzemni));
            else
                if i==k
                    Zmat(i,k)=rofaze+Rg+j*0.1445*log10(Dg/(psi*rfaze));
                else
                    Zmat(i,k)=Rg+j*0.1445*log10(Dg/(matd(i,k)));
                end
            end
        end
end
%% sestaveni impedancni matice_END
Zmat;
%disp('Impedancni matice vedeni (ohm/km)')
%disp(Zmat)
%% redukce zemni cesty_START
%vytvoreni submatic ze Zmat_START
Z11=zeros((numel(matd11))^0.5,(numel(matd11))^0.5);
Z12=zeros((numel(matd12))^0.5,(numel(matd12))^0.5);
Z21=zeros((numel(matd21))^0.5,(numel(matd21))^0.5);
Z22=zeros((numel(matd22))^0.5,(numel(matd22))^0.5);
Z10=zeros((numel(matd31)),1);
Z20=zeros((numel(matd32)),1);
Z00=zeros((numel(matd33))^0.5,(numel(matd33))^0.5);
for i=1:3
        for k=1:3
            Z11(i,k)=Zmat(i,k);
        end
end
for i=1:3
        for k=4:6
            Z12(i,k-3)=Zmat(i,k);
        end
end
for i=4:6
        for k=1:3
            Z21(i-3,k)=Zmat(i,k);
        end
end
for i=4:6
        for k=4:6
            Z22(i-3,k-3)=Zmat(i,k);
        end
end
for i=1:3
    Z10(i)=Zmat(i,7);
end
Z01=Z10.';
for i=1:3
    Z20(i)=Zmat(i+3,7);
end
Z02=Z20.';
Z00=Zmat(7,7);
%vytvoreni submatic ze Zmat_END
%
%vypocet_START
Zkor11=Z11-Z10*inv(Z00)*Z01;
Zkor12=Z12-Z10*inv(Z00)*Z02;
Zkor21=Z21-Z20*inv(Z00)*Z01;
Zkor22=Z22-Z20*inv(Z00)*Z02;
%vypocet_END
%% redukce zemni cesty_END
Z=[Zkor11 Zkor12;Zkor21 Zkor22];
%disp('Modifikovana matice vedeni (ohm/km)')
%disp(Z)
%% ziskani vlastni impedance fazi a jednotlivych smycek
% impedance fazi_START
imp=0;
disp('fazove impedance - potah1 po rade faze 1 2 3')
for i=1:3
   disp(Z(i,i))
    faze(i)=Z(i,i);
end
disp('fazove impedance - potah2 po rade faze 1 2 3')
for i=4:6
   disp(Z(i,i))
end
% impedance fazi_END
%
% mezifazove impedance_START (potlaceno vypsani)
for i=1:6
    pomi=0;
    for k=1:6
        pomk=0;
        if k~=i
            imp=Z(k,k)-Z(i,k);
            if i>3
                pomi=1;
                i=i-3;
            end
            if k>3
                pomk=1;
                k=k-3;
            end
            baf=[i;k]';
           %disp('potah1 potah 2');
            %disp(baf);
            %disp(imp);
           if pomk==1
        k=k+3;
        end
        if pomi==1
        i=i+3;
        end   
        end
         end
end
% mezifazove impedance_END