clc
clear all
Length=input('length in km:');
if Length <=80
    f=input('frequency in Hz:');
    r=input('resistance in ohms/phase/km:');
    L=input('inductance in mH/phase/km:');
    VRLL=input('Vr(L-L) in kV:');
    MVA=input('MVA at Receiving end:');
    pf=input('Receiving end Power factor:');
    R=r*Length;
    X=2*pi*f*L*10^-3*Length;
    z=(r+j*2*pi*L*10^-3*f);
    Z=z*Length;                            
    AR = (acos(pf));
    VR = VRLL/sqrt(3) + j*0;   % kV (receiving end phase voltage)
    A=cos(AR)
    B=sin(AR)
    SR = MVA*(A + j*B)     %   MVA (receiving end power)
    IR = conj(SR)/(3*conj(VR))         %  kA (receiving end current)
    IS = IR
    VS = VR + Z*IR;
    VSLL = sqrt(3)*abs(VS)
    SS = 3*VS*conj(IS)
    REG = (VSLL - VRLL)/VRLL*100
    Eff = real(SR)/real(SS)*100   
elseif
    r=input('resistance in ohms/phase/km:');
    x=input('reactance in ohms/phase/km:');
    y=input('admittance in mhos/phase/km:');
    R=r*Length;
    X=x*Length;
    Y=j*y*Length;
    Z=R+j*X;                 
    VRLL=input('Vr(L-L) in kV:');
    P=input('MW at Receiving end:');
    pf=input('Receiving end Power factor:');
    VR = (VRLL*1000)/sqrt(3) + j*0   % kV (receiving end phase voltage)
    IR = (P*10^6)/(3*VR*pf)         %  kA (receiving end current)
    Ic1=(Y/2)*VR
    AR = acos(pf);
    Il = IR*(cos(AR) - j*sin(AR))+Ic1
    VS = VR+(Z*Il)
    Ic2=(Y/2)*VS
    IS = Il+Ic2
    VRnl = abs(VS/(1+(Z*Y/2)))
    REG = (VRnl - VR)/VR*100
    Eff = (P*10^6)/((P*10^6)+(3*abs(Il^2)*R))*100
else
    f=input('frequency in Hz:');
    r=input('resistance in ohms/phase/km:');
    L=input('inductance in mH/phase/km:');
    C=input('shunt capacitance in microF/phase/km:');
    VRLL=input('Vr(L-L) in kV:');
    P=input('MW at Receiving end:');
    pf=input('Receiving end Power factor:');
    z=r+(j*2*pi*f*L*10^-3);
    y=(j*2*pi*f*C*10^-6);
    gamma=sqrt(z*y);
    Zc=sqrt(z/y);
    VR = (VRLL*1000)/sqrt(3) + j*0   % kV (receiving end phase voltage)
    IR = (P*10^6)/(3*VR*pf)         %  kA (receiving end current)
    AR = acos(pf);
    IR_lag =  IR*(cos(AR) - j*sin(AR))
    VS = (VR*cosh(gamma*Length))+(Zc*IR_lag*sinh(gamma*Length))
    IS = ((VR/Zc)*sinh(gamma*Length))+(IR_lag*cosh(gamma*Length))
    pfs= cos(angle(VS)- angle(IS))     %  (sending end power factor)
    A = cosh(gamma*Length)
    VSLL = sqrt(3)*abs(VS)
    REG = abs((((VSLL/(1000*A)) - VRLL)/VRLL))*100
    Input_power = 3*abs(VS)*abs(IS)*pfs
    Eff = ((P*10^6) /Input_power)*100
end