%% 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
poczem=11;                   %pocet fiktivnich zemnich cest + zem lano
%vydalenosti mezi fazovymi vodici[m]
d1112=3.517;
d1113=6.801;
d1213=3.493;
%
d1121=5;
d1122=7.6;
d1123=8.5;
%
d1221=6.5;
d1222=6.8;
d1223=6.9;
%
d1321=8.5;
d1322=7.7;
d1323=5.2;
%vzdalenosti od zemniho lana [m]
d110=4.974;
d120=8.417;
d130=11.4;
%vzdalenosti od fiktivni zemni cesty
d11z=23.3;
d12z=19.9;
d13z=16.5;
%polomer alfe [m]
rfaze=9.54*10^(-3);
rzemni=8.65*10^(-3);
rkabel=17.331*10^(-3);
%rezistance alfe [ohm/km]
rofaze=0.156;
rozemni=0.235;
rokabel=0.727;
ropudy=100; %[ohm.m]
%delka vedeni [km]
l=5.204;
%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=zeros(3,poczem);
matd13pom=[d110 d120 d130]';
pom=[d11z d12z d13z]';
for i=1:poczem
    for k=1:3
        if i==1
        matd13(k,i)=matd13pom(k,i);
        end
        if i>1
            matd13(k,i)=pom(k);
        end
    end
end
matd21=matd12;
matd22=[Dg d2122 d2123;d2122 Dg d2223;d2123 d2223 Dg];
matd23=matd13;
matd31=matd13';
matd32=matd23';
matd33=zeros(poczem,poczem);
for i=1:poczem
    for k=1:poczem
        matd33(i,k)=0.1;
    end
end
for i=1:poczem
    for k=1:poczem
        if i==1 | k==1
            matd33(i,k)=56.7;
        end
        if i==k
        matd33(i,k)=Dg;
        end
        if i~=k & i==1 & k==1
            matd33=pomzem(i,k);
        end
    end
end
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)
            x=i*k;
            if i<8 & k<8;
            switch x
                case 49
                Zmat(i,k)=rozemni+Rg+j*0.1445*log10(Dg/(psi*rzemni));
                case i^2
                    Zmat(i,k)=rofaze+Rg+j*0.1445*log10(Dg/(psi*rfaze));
                otherwise
                    Zmat(i,k)=Rg+j*0.1445*log10(Dg/(matd(i,k)));
            end
            end
            if i>7 | k>7
                 switch x
                case i^2
                    Zmat(i,k)=rofaze+Rg+j*0.1445*log10(Dg/(psi*rfaze));
                otherwise
                    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(3,poczem);
Z20=zeros(3,poczem);
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
    for k=1:poczem
    Z10(i,k)=Zmat(i,k+6);
    end
end
Z01=Z10.';
for i=1:3
    for k=1:poczem
    Z20(i,k)=Zmat(i+3,k+6);
    end
end
Z02=Z20.';
for i=1:poczem
    for k=1:poczem
        Z00(i,k)=Zmat(i+6,k+6);
    end
end
%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)
%
%% transformace do slozkovych souradnic
a=exp(j*2*pi/3);
T=[1 1 1;1 a^2 a;1 a a^2];
T1=inv(T);
Zpot1=Zkor11;
Z0=T1*Zpot1*T;
Z0(1,1)
%