% Eyad Arabi (c)
% In this code the coverage of the Ladder matching network is plotted.
% The network has two tunable capacitors C1 and C2. 
% C2 represent the capacitor after the load 
% C1 is the capacitor close to the source
% The two inductors are named in the same way
% Both capacitors can tune from Cmin to Cmax

Cmin = 1e-12;  % minimum capacitance
Cmax = 7e-12;  % maximum capacitance
L1 = 13e-9;
L2 = 13e-9;
f = 0.9e9;  % frequency in GHz
omega = 2*pi*f;
C_up = Cmin:0.01e-12:Cmax;
C_down = Cmax:-0.01e-12:Cmin;
Y0 = 1/50;
Z0 = 50;
arg_angle = 0:0.01:2*pi;
f_num = 5;
gamma = (89-50)/(89+50);
theta = 10*pi/180;
f_vec = 300:50:3000;
%--------------------------------------------------------------------------
C2 = Cmin;
C1 = C_up;

Y1 = Y0*1/(1i*omega*L2)./(Y0+1/(1i*omega*L2));
Y2 = Y1+1i*omega*C2;
Y3 = (Y2*1/(1i*omega*L1))./(Y2+1/(1i*omega*L1));
Y4 = Y3+1i*omega*C1;


G1 = (Y0-Y4)./(Y0+Y4);

figure;

% [Line_handle, smith_handle] = smithchart(G3);
% set(Line_handle,'Color','k','LineStyle',':');

%--------------------------------------------------------------------------

C1 = Cmax;
C2 = C_up;

Y1 = Y0*1/(1i*omega*L2)./(Y0+1/(1i*omega*L2));
Y2 = Y1+1i*omega*C2;
Y3 = (Y2*1/(1i*omega*L1))./(Y2+1/(1i*omega*L1));
Y4 = Y3+1i*omega*C1;




G2 = (Y0-Y4)./(Y0+Y4);

%--------------------------------------------------------------------------

C2 = Cmax;
C1 = C_down;

Y1 = Y0*1/(1i*omega*L2)./(Y0+1/(1i*omega*L2));
Y2 = Y1+1i*omega*C2;
Y3 = (Y2*1/(1i*omega*L1))./(Y2+1/(1i*omega*L1));
Y4 = Y3+1i*omega*C1;



G3 = (Y0-Y4)./(Y0+Y4);

%--------------------------------------------------------------------------

C1 = Cmin;
C2 = C_down;

Y1 = Y0*1/(1i*omega*L2)./(Y0+1/(1i*omega*L2));
Y2 = Y1+1i*omega*C2;
Y3 = (Y2*1/(1i*omega*L1))./(Y2+1/(1i*omega*L1));
Y4 = Y3+1i*omega*C1;



G4 = (Y0-Y4)./(Y0+Y4);

%--------------------------------------------------------------------------
%------------Theoretical Analysis-Part 1-----------------------------------
% C1 = 0.01e-12:0.01e-12:50e-12;
 C2 = Cmin;
% 
 x1 = -1;

y1 = 0;


Re_Y2 = (Y0)/(1+(omega*Y0*L2)^2);
Im_Y2 = (omega*(C2*(1+(omega*Y0*L2)^2)-Y0^2*L2))/(1+(omega*Y0*L2)^2);

Re_Yin = (Re_Y2*(1-omega*Im_Y2*L1)+omega*Re_Y2*Im_Y2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

Im_Yin = (Im_Y2*(1-omega*Im_Y2*L1)-omega*Re_Y2^2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

x2 = (-Re_Yin^2+Y0^2-Im_Yin^2)/((Re_Yin+Y0)^2 + Im_Yin^2);

y2 = (-2*Y0*Im_Yin)/((Re_Yin+Y0)^2 + Im_Yin^2);
xc = x1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
yc = y1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));

radius = sqrt((xc-x1)^2+(yc-y1)^2);
G_Circle_2 = radius*cos(arg_angle)+xc+1i*(radius*sin(arg_angle)+yc);
[Line_handle, smith_handle] = smithchart(G_Circle_2);
set(Line_handle,'Color','k','LineStyle','--');
set(smith_handle,'LabelVisible','off');
 hold on
 
 C2 = Cmax;
% 
  x1 = -1;

y1 = 0;


Re_Y2 = (Y0)/(1+(omega*Y0*L2)^2);
Im_Y2 = (omega*(C2*(1+(omega*Y0*L2)^2)-Y0^2*L2))/(1+(omega*Y0*L2)^2);

Re_Yin = (Re_Y2*(1-omega*Im_Y2*L1)+omega*Re_Y2*Im_Y2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

Im_Yin = (Im_Y2*(1-omega*Im_Y2*L1)-omega*Re_Y2^2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

x2 = (-Re_Yin^2+Y0^2-Im_Yin^2)/((Re_Yin+Y0)^2 + Im_Yin^2);

y2 = (-2*Y0*Im_Yin)/((Re_Yin+Y0)^2 + Im_Yin^2);
xc = x1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
yc = y1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));

radius = sqrt((xc-x1)^2+(yc-y1)^2);
G_Circle_2 = radius*cos(arg_angle)+xc+1i*(radius*sin(arg_angle)+yc);
[Line_handle, smith_handle] = smithchart(G_Circle_2);
set(Line_handle,'Color','k','LineStyle','--');
set(smith_handle,'LabelVisible','off');
% %----------------------------Theoretical Analysis Part II------------------
 C1 = Cmin;
 %----This is the tangent point
 x1 = ((Y0*omega*L1)^2-(1-omega^2*L1*C1)^2)./((1-omega^2*L1*C1)^2 + (Y0*omega*L1)^2);
 y1 = (2*Y0*omega*L1*(1-omega^2*L1*C1))./((1-omega^2*L1*C1)^2 + (Y0*omega*L1)^2);
 

 Re_Y4 = Y0/(1+(Y0*omega*(L1+L2))^2);
 
 Im_Y4 = (omega*C1-Y0^2*omega*(L1+L2)*(1-omega^2*C1*(L1+L2)))/(1+(Y0*omega*(L1+L2))^2);
 
 x2 = (-Re_Y4^2+Y0^2-Im_Y4^2)./((Re_Y4+Y0)^2+Im_Y4^2);
 y2 = (-2*Im_Y4*Y0)./((Re_Y4+Y0)^2+Im_Y4^2);
 

 xc = x1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
 yc = y1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
% 
 radius = sqrt((xc-x1)^2+(yc-y1)^2);
 G_Circle_2 = radius*cos(arg_angle)+xc+1i*(radius*sin(arg_angle)+yc);
 [Line_handle, smith_handle] = smithchart(G_Circle_2);
 set(Line_handle,'Color','k','LineStyle','--');
 set(smith_handle,'LabelVisible','off');
% 

C1 = Cmax;
 %----This is the tangent point
 x1 = ((Y0*omega*L1)^2-(1-omega^2*L1*C1)^2)./((1-omega^2*L1*C1)^2 + (Y0*omega*L1)^2);
 y1 = (2*Y0*omega*L1*(1-omega^2*L1*C1))./((1-omega^2*L1*C1)^2 + (Y0*omega*L1)^2);
 
%  ga = x2+1i*y2;
% 
 Re_Y4 = Y0/(1+(Y0*omega*(L1+L2))^2);
 
 Im_Y4 = (omega*C1-Y0^2*omega*(L1+L2)*(1-omega^2*C1*(L1+L2)))/(1+(Y0*omega*(L1+L2))^2);
 
 x2 = (-Re_Y4^2+Y0^2-Im_Y4^2)./((Re_Y4+Y0)^2+Im_Y4^2);
 y2 = (-2*Im_Y4*Y0)./((Re_Y4+Y0)^2+Im_Y4^2);
 

 xc = x1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
 yc = y1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
% 
 radius = sqrt((xc-x1)^2+(yc-y1)^2);
 G_Circle_2 = radius*cos(arg_angle)+xc+1i*(radius*sin(arg_angle)+yc);
 [Line_handle, smith_handle] = smithchart(G_Circle_2);
 set(Line_handle,'Color','k','LineStyle','--');
 set(smith_handle,'LabelVisible','off');
% 
% %------------------Auxilliary Circle-----------------
 radius_aux = (1+2*xc+xc^2+yc^2-radius^2)/(2*(1+xc+radius));
 x_aux = radius_aux-1;
 Aux_Circle = radius_aux*cos(arg_angle)+x_aux+1i*(radius_aux*sin(arg_angle));
 [Line_handle, smith_handle] = smithchart(Aux_Circle);
 set(Line_handle,'Color','k','LineStyle','--');
set(smith_handle,'LabelVisible','off');

%--------------------------Auxiliary theory II ----------------------------
%--------------------------------------------------------------------------

 C2 = 1/(omega^2*L1)+(Y0^2*L2)/(1+(Y0*omega*L2)^2);
% 
  x1 = -1;

y1 = 0;


Re_Y2 = (Y0)/(1+(omega*Y0*L2)^2);
Im_Y2 = (omega*(C2*(1+(omega*Y0*L2)^2)-Y0^2*L2))/(1+(omega*Y0*L2)^2);

Re_Yin = (Re_Y2*(1-omega*Im_Y2*L1)+omega*Re_Y2*Im_Y2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

Im_Yin = (Im_Y2*(1-omega*Im_Y2*L1)-omega*Re_Y2^2*L1)/((1-omega*Im_Y2*L1)^2 + (omega*Re_Y2*L1)^2);

x2 = (-Re_Yin^2+Y0^2-Im_Yin^2)/((Re_Yin+Y0)^2 + Im_Yin^2);

y2 = (-2*Y0*Im_Yin)/((Re_Yin+Y0)^2 + Im_Yin^2);
xc = x1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));
yc = y1*(x2^2-x1^2+y2^2-y1^2)/(2*(-x1^2-y1^2+x1*x2+y1*y2));

radius = sqrt((xc-x1)^2+(yc-y1)^2);
G_Circle_2 = radius*cos(arg_angle)+xc+1i*(radius*sin(arg_angle)+yc);
[Line_handle, smith_handle] = smithchart(G_Circle_2);
set(Line_handle,'Color','m');
set(smith_handle,'LabelVisible','off');


%----------------Plotting numerical----------------------------------------
[Line_handle, smith_handle] = smithchart(G1);
set(Line_handle,'Color','r','LineWidth',2);
set(smith_handle,'LabelVisible','off');
 hold on
[Line_handle, smith_handle] = smithchart(G2);
set(Line_handle,'Color','r','LineWidth',2);
set(smith_handle,'LabelVisible','off');
[Line_handle, smith_handle] = smithchart(G3);
set(Line_handle,'Color','r','LineWidth',2);
set(smith_handle,'LabelVisible','off');
[Line_handle, smith_handle] = smithchart(G4);
set(Line_handle,'Color','r','LineWidth',2);
set(smith_handle,'LabelVisible','off');


