%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load Line Analysis of a diode circuit
% vsaxena@uidaho.edu, Jan 2018
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc; close all; clear all;

mA = 1e-3;

%% Device parameters
KPn = 200e-6;
VTHN = 0.4;
lambda = 0.1;
W_by_L = 1.8/0.18;

%% Circuit parameters
VDD = 1.8;
VGS = 1.2;
VDS_sat = VGS-VTHN;
R = 1e3;

%% Load Line Analysis
V=[0:0.01:VDD];

% MOSFET equations 
f1=@(V) (KPn*W_by_L*((VGS-VTHN)*V-V.^2/2).*(V<VDS_sat) + 0.5*KPn*W_by_L*((VGS-VTHN).^2).*(1+lambda*(V-VDS_sat)).*(V>=VDS_sat));

% Load line equation
f2=@(V) (VDD-V)/R;

% Graphical solution
VD = fzero(@(V)f1(V)-f2(V),0.5); %starts search from V==0.5
ID = f1(VD)/mA;

% Plot the curves
figure();
plot(V, f1(V)/mA, 'r', 'linewidth', 2);
hold on; grid on;
plot(V, f2(V)/mA, 'b',  'linewidth', 2);

% Add marker and text
plot(VD, ID,'kO')
txt2 = ['V_D = ', num2str(VD), 'V, I_D = ', num2str(ID), ' mA'];
text(VD+0.1,ID+0.1,txt2)
VD,ID

title('Load Line Analysis');
xlabel('V_{DS}, V')
ylabel('I_D, mA')

xlim([0 VDD]);
ylim([0 max(1.2*f1(VDD)/mA, VDD/R/mA)]);

