% File: test_lookupVGS_eldo.m
% Usage examples for lookup_VGS function

clearvars;
clc;
close all;

addpath('/usr/class/gmidLUTs;/usr/class/gmidTECHs')
addpath('/usr/local/MATLAB/HspiceToolbox')

%addpath ..
load('/usr/class/gmidTECHs/180nch_e.mat')

%% Example with unknown source voltage
gm_ID = 15;
VGB   = .9;
VDB   = .7;

% find VGS and VS
VGS = look_upVGS(nch, 'GM_ID', gm_ID,'VDB',VDB,'VGB',VGB)
VSB  = VGB - VGS  % VS does not change much with VDB 

% now check to make sure the same gm/ID comes our from forward
% interpolation
gmID = look_up(nch,'GM_ID','VGS',VGS,'VDS',VDB-VSB,'VSB',VSB)
    
%% Example with vector input for L

gm_ID = 15;
L = min(nch.L):0.1:0.5;
VGS = look_upVGS(nch, 'GM_ID', gm_ID, 'L', L)

%% Example with vector input for gm/ID

gm_ID = 5:10;
VGS = look_upVGS(nch, 'GM_ID', gm_ID, 'L', 0.25)


%% Example with ID_W as input

gm_ID =10:12;
ID_W = look_up(nch,'ID_W','GM_ID', gm_ID)
VGS = look_upVGS(nch, 'GM_ID', gm_ID)
VGS = look_upVGS(nch, 'ID_W', ID_W)

    
%% Example with known and positive VSB
% The issue is that gm/ID drops again for VGS near zero and the function
% may then catch the wrong intercept. To fix this, the function looks only
% to the right of the maximum value for gm/ID

VSB = 0.8;
VDS = 0.2;
gm_ID = look_up(nch,'GM_ID','VGS',nch.VGS,'VSB',VSB, 'VDS', VDS);
gmid = max(gm_ID)-0.05;
VGS = look_upVGS(nch, 'GM_ID', gmid,'VSB',VSB,'VDS',VDS)
display('Plot Figure 1')
plot(nch.VGS, gm_ID, VGS, gmid, 'o')


%% Example with vector input for mode 2 -- L

VSB = 0.8;
VDS = 0.2;
VGS = look_upVGS(nch, 'GM_ID', gmid,'VSB',VSB,'VDS',VDS, 'L', min(nch.L):0.1:0.2)


%% Example with vector input for mode 2 -- VDS

VSB = 0.8;
VDS = 0.2:0.1:0.5;
VGS = look_upVGS(nch, 'GM_ID', gmid,'VSB',VSB,'VDS',VDS)

%% Example with values exceeding maximum

gm_ID = 50;
VGS = look_upVGS(nch, 'GM_ID', gm_ID, 'L', 0.25)

