
% Device characteristics
K = 1;      % in mA/V^2
V_T = 0.75; % in V
lambda = 0.1; % in V^(-1)
V_DD = 3;

% Precision of grid
N = 100;

% NMOS
for k = 1:9
    v_gs     = 0.75 + 0.25*k;
    v_ds_sat = v_gs - V_T;
    
    v_lin = linspace(0,v_ds_sat,N);
    v_sat = linspace(v_ds_sat,3,N);

    i_lin = K * (v_gs - V_T - v_lin/2) .* v_lin;
    i_sat = K * (v_gs - V_T - v_ds_sat/2) * v_ds_sat * ones(1,length(v_sat));
    
    plot(v_lin,i_lin);
    hold on;
    plot(v_sat,i_sat);
end


% PMOS
for k = 1:9
    v_in     = 2.25 - 0.25*k;
    v_ds_sat = v_in + 0.75;
    
    v_sat = linspace(0,v_ds_sat,N);
    v_lin = linspace(v_ds_sat,3,N);

    i_sat = 1/2*K*((v_in-V_DD)+0.75)^2*ones(1,length(v_sat));
    i_lin = K * ((v_in-V_DD)+0.75-1/2*(v_lin-V_DD)).*(v_lin-V_DD);
    
    plot(v_lin,i_lin,'r');
    hold on;
    plot(v_sat,i_sat,'r');
end

grid on;
xlabel('V_{DS} = v_{out} (in Volts)');
ylabel('I_{D} (in mA)');
title('Problem 3, part (d): Load-line analysis of CMOS');

% 
% % Part (b), load line of pull up MOSFET
% v1 = linspace(0,2.25);
% v2 = linspace(2.25,3);
% i1 = 0.2813*(1+lambda*(2.25-v1));
% i2 = K*(v2/2 - 0.75).*(3-v2);
% plot(v1,i1,'r','LineWidth',2);
% plot(v2,i2,'r','LineWidth',2);
% title('Problem 3, part (c): Early effect included');