function mosp_usd;
%Calculation of MOSFET output and transfer charactersitics
%from SPICE LEVEL 3 parameters.
%(Section 5.4.1 in "Understanding Semiconductor Devices")

%   Sima Dimitrijev, October 20, 1999.
%   Copyright (c) 2000, Oxford University Press.

global conW_usd13; global conNP_usd13; 
global con_go_usd13; global conLIN_usd13;

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Section 5.4.1: MOSFET Calculator (SPICE LEVEL3 Model)',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

hold on;
Ha0=gca;
set(Ha0,'Xlim',[0 1],'Ylim',[0 1],'Visible','off',...
   'Position',[0.0 0.0 1.0 1.0]);

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 5.4.1: MOSFET Calculator (SPICE LEVEL3 Model)' ;...
      ' ';...
      'Set input parameters to see the effects on transfer and output MOSFET ';...
      'characteristics. To visualize the relationships between mutually';...
      'dependent and inconsistent parameters, the following color code is used';
      ' ';...
      '  RED:    default value  ';...
      '  BLACK:  set value ';...
      '  GREEN:  resultant value';...
      ' ';...
      '  BLUE background: parameters that need Nsub     '};   

Ht_intro=text(0.1,0.55,char(introtext),'Units','normalized');
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.1 0.1 0.7 0.1],...
   'Style','pushbutton','String','CLICK HERE TO CONTINUE!','CallBack','con_go_usd13=1;');
con_go_usd13=0;
while con_go_usd13<1; 
   pause(0.2);  
   if ishandle(Hf)==0;
      return; 
   end;
end
con_go_usd13=0;
set(Ht_intro,'Visible','off');
set(Hp_cont,'Visible','off','Enable','off');
drawnow;



conW_usd13=0; conNP_usd13=0; con_go_usd13=0; conLIN_usd13=0;

Hframe=patch([0.46 1 1 0.46 0.46],[0.01 0.01 0.99 0.99 0.01],[0.75 0.75 0.75]);
set(Hframe,'FaceColor',[0.75 0.75 0.75],'EdgeColor',[0 0 0]);
%set(Ha0,'Xlim',[0 1],'Ylim',[0 1],'Visible','off','Color',[0.8 0.8 0.8]);
hold off;

%NMOS/PMOS buttons
Hr_nmos(1)=uicontrol(Hf,'Units','normalized','BackgroundColor',[.75 .75 .75],...
   'Position',[0.63 0.95 0.15 0.03],'ForegroundColor',[0 0.6 0],...
   'Style','radio','String','NMOS','CallBack','conNP_usd13=1;','Value',1);
Hr_nmos(2)=uicontrol(Hf,'Units','normalized','BackgroundColor',[.75 .75 .75],...
   'Position',[0.81 0.95 0.15 0.03],'ForegroundColor',[0.8 0 0],...
   'Style','radio','String','PMOS','CallBack','conNP_usd13=2;','Value',0);

Ht_txt(1)=text(0.74,0.89,'GEOMETRIC VARIABLES','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(1)=text(0.56,0.84,'L=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(1)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.84 0.10 0.04],...
   'Style','edit','String','100e-6','CallBack','conW_usd13=1;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(1)=text(0.665,0.84,'m','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(2)=text(0.82,0.84,'W=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(2)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.84 0.10 0.04],...
   'Style','edit','String','100e-6','CallBack','conW_usd13=2;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(2)=text(0.925,0.84,'m','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(2)=text(0.74,0.77,'PRINCIPAL PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');

Ht_par(3)=text(0.56,0.72,'Vto=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(3)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.72 0.10 0.04],...
   'Style','edit','String','0','CallBack','conW_usd13=3;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(3)=text(0.665,0.72,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(4)=text(0.82,0.72,'KP=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(4)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.72 0.10 0.04],...
   'Style','edit','String','20.7e-6','CallBack','conW_usd13=4;',...
   'BackgroundColor',[1 1 1],'ForegroundColor',[0 0.6 0],'FontWeight','bold');
Ht_un(4)=text(0.925,0.72,'A/V^{2}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(5)=text(0.56,0.66,'Phi=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(5)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.66 0.10 0.04],...
   'Style','edit','String','0.66','CallBack','conW_usd13=5;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(5)=text(0.665,0.66,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(6)=text(0.82,0.66,'Gamma=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(6)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.66 0.10 0.04],...
   'Style','edit','String','0','CallBack','conW_usd13=6;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(6)=text(0.925,0.66,'V^{1/2}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(7)=text(0.56,0.60,'Tox=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(7)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.60 0.10 0.04],...
   'Style','edit','String','100e-9','CallBack','conW_usd13=7;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(7)=text(0.665,0.60,'m','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(8)=text(0.82,0.60,'Uo=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(8)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.60 0.10 0.04],...
   'Style','edit','String','600','CallBack','conW_usd13=8;',...
   'BackgroundColor',[1 1 1],'ForegroundColor',[1 0 0],'FontWeight','bold');
Ht_un(8)=text(0.925,0.60,'cm^{2}/Vs','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(3)=text(0.74,0.53,'CHANNEL-RELATED PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');

Ht_par(9)=text(0.56,0.48,'THETA=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(9)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.48 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=9;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(9)=text(0.665,0.48,'V ^{-1}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(10)=text(0.82,0.48,'Vmax=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(10)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.48 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=10;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(10)=text(0.925,0.48,'m/s','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(11)=text(0.56,0.42,'Nsub=','Color','b','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(11)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.42 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=11;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(11)=text(0.665,0.42,'cm^{-3}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(12)=text(0.82,0.42,'KAPPA=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(12)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.42 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=12;',...
   'BackgroundColor',[0.6 0.6 1],'ForegroundColor','k');
Ht_un(12)=text(0.925,0.42,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(4)=text(0.74,0.35,'DEPLETION-LAYER RELATED PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');

Ht_par(13)=text(0.56,0.30,'ETA=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(13)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.30 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=13;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(13)=text(0.665,0.30,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(14)=text(0.82,0.30,'Nss=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(14)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.30 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=14;',...
   'BackgroundColor',[0.6 0.6 1],'ForegroundColor','k');
Ht_un(14)=text(0.925,0.30,'cm^{-2}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(15)=text(0.56,0.24,'TPG=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(15)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.24 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=15;',...
   'BackgroundColor',[0.6 0.6 1],'ForegroundColor','k');
Ht_un(15)=text(0.665,0.24,'(1/-1/0)','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(16)=text(0.82,0.24,'PB=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(16)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.24 0.10 0.04],...
   'Style','edit','String','0.8','CallBack','conW_usd13=16;',...
   'BackgroundColor',[0.6 0.6 1],'ForegroundColor','r');
Ht_un(16)=text(0.925,0.24,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(17)=text(0.56,0.18,'Xj=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(17)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.18 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=17;',...
   'BackgroundColor',[0.6 0.6 1],'ForegroundColor','k');
Ht_un(17)=text(0.665,0.18,'m','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(18)=text(0.82,0.18,'Ld=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(18)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.18 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=18;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(18)=text(0.925,0.18,'m','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(19)=text(0.56,0.12,'DELTA=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(19)=uicontrol(Hf,'Units','normalized','Position',[0.56 0.12 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=19;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(19)=text(0.665,0.12,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(20)=text(0.82,0.12,'NFS=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(20)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.12 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd13=20;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(20)=text(0.925,0.12,'cm^{-2}','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

%The voltages
%Vsb
Ht_par(21)=text(0.14,0.93,'V_{SB}=','Color',[0 0.6 0],'HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(21)=uicontrol(Hf,'Units','normalized','Position',[0.14 0.93 0.05 0.04],...
   'Style','edit','String','0','CallBack','conW_usd13=21;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
Ht_un(21)=text(0.2,0.93,'V','Color',[0 0.6 0],'HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

%Vdsq
Ht_par(22)=text(0.36,0.40,'V_{DS}=','Color',[0 0.6 0],'HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_edit(22)=uicontrol(Hf,'Units','normalized','Position',[0.36 0.36 0.06 0.04],...
   'Style','edit','String','0.05','CallBack','conW_usd13=22;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
Ht_un(22)=text(0.425,0.36,'V','Color',[0 0.6 0],'HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
%Vdsmax
Ht_edit(23)=uicontrol(Hf,'Units','normalized','Position',[0.38 0.555 0.05 0.04],...
   'Style','edit','String','5','CallBack','conW_usd13=23;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
%Vgstrmin
Ht_edit(24)=uicontrol(Hf,'Units','normalized','Position',[0.08 0.045 0.06 0.04],...
   'Style','edit','String','-0.5','CallBack','conW_usd13=24;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
%Vgstrmax
Ht_edit(25)=uicontrol(Hf,'Units','normalized','Position',[0.32 0.045 0.06 0.04],...
   'Style','edit','String','5','CallBack','conW_usd13=25;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
%Vgsmax
Ht_par(24)=text(0.33,0.93,'V_{GSmax}=','Color',[0 0.6 0],'HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(26)=uicontrol(Hf,'Units','normalized','Position',[0.33 0.93 0.05 0.04],...
   'Style','edit','String','5','CallBack','conW_usd13=26;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
Ht_un(24)=text(0.39,0.93,'V','Color',[0 0.6 0],'HorizontalAlignment','left',...
   'VerticalAlignment','bottom');


%LIN/LOG buttons
Hr_lin(1)=uicontrol(Hf,'Units','normalized','BackgroundColor',[.8 .8 .8],...
   'Position',[0.36 0.27 0.08 0.03],'ForegroundColor','k',...
   'Style','radio','String','LOG','CallBack','conLIN_usd13=1;','Value',1);
Hr_lin(2)=uicontrol(Hf,'Units','normalized','BackgroundColor',[.8 .8 .8],...
   'Position',[0.36 0.23 0.08 0.03],'ForegroundColor','k',...
   'Style','radio','String','LIN','CallBack','conLIN_usd13=2;','Value',0);

%CLOSE
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.56 0.02 0.36 0.07],...
   'Style','pushbutton','String','CLOSE','CallBack','con_go_usd13=1;');


%%%%%%%%%%%%%%%%%%%%%%%
%%%CONSTANTS%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
epssi=11.8*8.85e-12;
epsox=3.9*8.85e-12;
k=8.62e-5;
T=27+273.15;
q=1.6e-19;
Vth=k*T;
ni=1.4578e16;
Eg=1.12;
NMOS=1;            %-1 for PMOS
Vsb=0;
Vdsq0=0.05;
Vdsq=NMOS*Vdsq0;
Vgsmax=str2num(get(Ht_edit(26),'String'));
Vgs0=[0 0.2 0.4 0.6 0.8 1.0]*Vgsmax;
Vgs=NMOS*Vgs0;
Vdsmax=5;   %To become an input parameter
Vds0=[0:(Vdsmax/80):Vdsmax];
Vds=NMOS*Vds0;
Vgstrmin=-0.5; %To become an input parameter
Vgstrmax=5; %To become an input parameter
Vgstr0=[Vgstrmin:(Vgstrmax-Vgstrmin)/100:Vgstrmax];
Vgstr=NMOS*Vgstr0;

%%%%%%%%%%%%%%%%%%%%%
%%%SET THE GRAPHS%%%%
%%%%%%%%%%%%%%%%%%%%%

%Ids-Vds
for i=1:6;
   Id(i,:)=Vds*0;
end   
Ha_idvd=axes('Xlim',[0 Vdsmax],'Ylim',[0 5],'Position',[0.08 0.60 0.34 0.32],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on');
hold on;
xlabel('V_{DS} (V)','Color',[0 0.6 0]); ylabel('I_{D} (mA)','Color',[0 0.6 0]);
for i=1:6;
Hp_o_old(i)=plot(Vds,Id(i,:)*1000,'-k','LineWidth',1);
set(Hp_o_old(i),'Color',[0.5 0.5 0.5]);
end;
for i=1:6;
Hp_o(i)=plot(Vds,Id(i,:)*1000,'-k','LineWidth',2);
end;
hold off;

%Ids-Vgs
Idtr=Vgstr*0+1e-9;
Ha_idvg=axes('Xlim',[-1 Vgsmax],'Ylim',[1e-6 5],'Position',[0.10 0.09 0.25 0.37],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','YScale','log','Box','on');
hold on;
xlabel('V_{GS} (V)','Color',[0 0.6 0]); ylabel('I_{D} (mA)','Color',[0 0.6 0]);
Hp_t_old=plot(Vgstr,Idtr*1000,'-k','LineWidth',1);
set(Hp_t_old,'Color',[0.5 0.5 0.5]);
Hp_t=plot(Vgstr,Idtr*1000,'-k','LineWidth',2);
hold off;


%%%%%%%%%%%%%%%%%%%%%%%
%THE INITIAL PARAMETERS
%%%%%%%%%%%%%%%%%%%%%%%
for i=1:20;
   par{i}=get(Ht_edit(i),'String');
end   
L=str2num(par{1});      %1
W=str2num(par{2});      %2
Vto=str2num(par{3});    %3
KP=str2num(par{4});     %4
Phi=str2num(par{5});    %5
Gamma=str2num(par{6});  %6
Tox=str2num(par{7});    %7
   Cox=epsox/Tox;
Uo=str2num(par{8});     %8
THETA=0;                %9
%Vmax - empty          %10
%Nsub - empty          %11
KAPPA=0;               %12
ETA=0;                 %13
Nss=0;                 %14
%TPG - empty           %15
PB=str2num(par{16});   %16
%Xj - empty            %17
Ld=0;                  %18
DELTA=0;               %19
NFS=0;                 %20


conW_usd13=21; conNP_usd13=0; con_go_usd13=0;
firstgo=1; linlg=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%MAIN LOOP %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while con_go_usd13<1;
   %When changig axes (Vdsmax...) set firstgo=1;
   
%set NMOS/PMOS
if conNP_usd13>0;
   set(Hr_nmos,'Value',0);
   if conNP_usd13==2; %PMOS
      NMOS=-1;
      set(Hr_nmos(2),'Value',1);
      axes(Ha_idvd);
      xlabel('V_{SD} (V)','Color',[0.8 0 0]); ylabel('I_{D} (mA)','Color',[0.8 0 0]);
      axes(Ha_idvg);
      xlabel('V_{SG} (V)','Color',[0.8 0 0]); ylabel('I_{D} (mA)','Color',[0.8 0 0]);
      set(Ht_par(21),'String','V_{BS}=','Color',[0.8 0 0]);
      set(Ht_un(21),'Color',[0.8 0 0]);
      set(Ht_par(22),'String','V_{SD}=','Color',[0.8 0 0]);
      set(Ht_un(22),'Color',[0.8 0 0]);
      set(Ht_par(24),'String','V_{SGmax}=','Color',[0.8 0 0]);
      set(Ht_un(24),'Color',[0.8 0 0]);
    else %NMOS
      NMOS=1;
      set(Hr_nmos(1),'Value',1);
      axes(Ha_idvd);
      xlabel('V_{DS} (V)','Color',[0 0.6 0]); ylabel('I_{D} (mA)','Color',[0 0.6 0]);
      axes(Ha_idvg);
      xlabel('V_{GS} (V)','Color',[0 0.6 0]); ylabel('I_{D} (mA)','Color',[0 0.6 0]);
      set(Ht_par(21),'String','V_{SB}=','Color',[0 0.6 0]);
      set(Ht_un(21),'Color',[0 0.6 0]);
      set(Ht_par(22),'String','V_{DS}=','Color',[0 0.6 0]);
      set(Ht_un(22),'Color',[0 0.6 0]);
      set(Ht_par(24),'String','V_{GSmax}=','Color',[0 0.6 0]);
      set(Ht_un(24),'Color',[0 0.6 0]);
   end   
   Vgs=NMOS*Vgs0;
   Vds=NMOS*Vds0;
   Vgstr=NMOS*Vgstr0;
   Vdsq=NMOS*Vdsq0;
end; %if conNP_usd13>0;

%Set LIN/LOG
if conLIN_usd13>0;
   set(Hr_lin,'Value',0);
   if conLIN_usd13==2;
      set(Ha_idvg,'YScale','lin');
      set(Hr_lin(2),'Value',1);
      linlg=2;
   else
      set(Ha_idvg,'YScale','log');
      set(Hr_lin(1),'Value',1);
      linlg=1;
   end   
end;    

if ((conW_usd13)+(conNP_usd13))>0; %Recalculate the data    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% SET THE PARAMETER VALUE%%%%   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  if firstgo<1;
      Id_old=Id;
      Idtr_old=Idtr;
   end   

   switch conW_usd13;
   case 1; 
      set(Ht_edit(1),'ForegroundColor',[0 0 0]);
      Lold=L;
      L=str2num(get(Ht_edit(1),'String'));
      if isempty(L)==1; 
         L=100e-6; 
         set(Ht_edit(1),'String','100e-6','ForegroundColor',[1 0 0]);
      end
      if L<0; 
         L=100e-6; 
         set(Ht_edit(1),'String','100e-6','ForegroundColor',[1 0 0]);
      end;
      Ld=str2num(get(Ht_edit(18),'String'));
      if isempty(Ld)==1; Ld=0; end;
      Leff=L-2*Ld;
      if Leff<0; 
         L=Lold; 
         astr=num2str(L,'%1.2e');
         if L>0.01; astr=num2str(L,'%1.4f'); end
         set(Ht_edit(1),'String',astr);
         %set(Ht_edit(1),'String',num2str(L));
      end;
       
   case 2; 
      set(Ht_edit(2),'ForegroundColor',[0 0 0]);
      W=str2num(get(Ht_edit(2),'String'));
      if isempty(W)==1; 
         W=100e-6; 
         set(Ht_edit(2),'String','100e-6','ForegroundColor',[1 0 0]);
      end
      if W<0; 
         W=100e-6; 
         set(Ht_edit(2),'String','100e-6','ForegroundColor',[1 0 0]);
      end;
      
   case 3;   
      set(Ht_edit(3),'ForegroundColor',[0 0 0]);
      Vto=str2num(get(Ht_edit(3),'String'));
      if isempty(Vto)==1; 
         Vto=0; 
         set(Ht_edit(3),'String','0','ForegroundColor',[1 0 0]);
      end
      
   case 4;
      set(Ht_edit(4),'ForegroundColor',[0 0 0]);
      KP=str2num(get(Ht_edit(4),'String'));
      if isempty(KP)==1; 
         Uo=str2num(get(Ht_edit(8),'String'));
         Tox=str2num(get(Ht_edit(7),'String'));
         KP=Uo*1e-4*epsox/Tox;
         astr=num2str(KP,'%1.4f');
         if KP<0.01; astr=num2str(KP,'%1.2e'); end
         set(Ht_edit(4),'ForegroundColor',[0 0.6 0],'String',astr);   
         %set(Ht_edit(8),'ForegroundColor',[0 0 0]); 
      end
      if KP<0; 
         Uo=str2num(get(Ht_edit(8),'String'));
         Tox=str2num(get(Ht_edit(7),'String'));
         KP=Uo*1e-4*epsox/Tox;
         astr=num2str(KP,'%1.4f');
         if KP<0.01; astr=num2str(KP,'%1.2e'); end
         set(Ht_edit(4),'ForegroundColor',[0 0.6 0],'String',astr);   
         %set(Ht_edit(8),'ForegroundColor',[0 0 0]); 
      end
      
   case 5;
      set(Ht_edit(5),'ForegroundColor',[0 0 0]);
      Phi=str2num(get(Ht_edit(5),'String'));
      if isempty(Phi)==1;
         Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
         if isempty(Nsub)==1; %Take default for Phi
            Phi=0.6;
            set(Ht_edit(5),'ForegroundColor',[1 0 0],'String','0.6');
         else %Calculate Phi   
            Phi=2*Vth*log(Nsub/ni);
            astr=num2str(Phi,'%1.4f');
            if Phi<1e-2; astr=num2str(Phi,'%1.2e'); end
            set(Ht_edit(5),'ForegroundColor',[0 0.6 0],'String',astr);
         end 
      end
      if Phi<0;
         Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
         if isempty(Nsub)==1; %Take default for Phi
            Phi=0.6;
            set(Ht_edit(5),'ForegroundColor',[1 0 0],'String','0.6');
         else %Calculate Phi   
            Phi=2*Vth*log(Nsub/ni);
            astr=num2str(Phi,'%1.4f');
            if Phi<1e-2; astr=num2str(Phi,'%1.2e'); end
            set(Ht_edit(5),'ForegroundColor',[0 0.6 0],'String',astr);
         end 
      end
      
  case 6;
      set(Ht_edit(6),'ForegroundColor',[0 0 0]);
      Gamma=str2num(get(Ht_edit(6),'String'));
      if isempty(Gamma)==1;
         Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
         if isempty(Nsub)==1; %Take default for Gamma
            Gamma=0;
            set(Ht_edit(6),'ForegroundColor',[1 0 0],'String','0');
         else %Calculate Gamma   
            Gamma=sqrt(2*epssi*q*Nsub)/Cox;
            astr=num2str(Gamma,'%1.4f');
            if Gamma<1e-2; astr=num2str(Gamma,'%1.2e'); end
            set(Ht_edit(6),'ForegroundColor',[0 0.6 0],'String',astr);
         end 
      end
      if Gamma<0;
         Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
         if isempty(Nsub)==1; %Take default for Gamma
            Gamma=0;
            set(Ht_edit(6),'ForegroundColor',[1 0 0],'String','0');
         else %Calculate Gamma   
            Gamma=sqrt(2*epssi*q*Nsub)/Cox;
            astr=num2str(Gamma,'%1.4f');
            if Gamma<1e-2; astr=num2str(Gamma,'%1.2e'); end
            set(Ht_edit(6),'ForegroundColor',[0 0.6 0],'String',astr);
         end 
      end
      
   case 7;   
      set(Ht_edit(7),'ForegroundColor',[0 0 0]);
      Tox=str2num(get(Ht_edit(7),'String'));
      if isempty(Tox)==1; 
         Tox=100e-9; 
         set(Ht_edit(7),'String','100e-9','ForegroundColor',[1 0 0]);
      end
      if Tox<0; 
         Tox=100e-9; 
         set(Ht_edit(7),'String','100e-9','ForegroundColor',[1 0 0]);
      end;
      Cox=epsox/Tox;
      
   case 8;   
      set(Ht_edit(8),'ForegroundColor',[0 0 0]);
      Uo=str2num(get(Ht_edit(8),'String'));
      acol=get(Ht_edit(4),'ForegroundColor');
      KP_fl=(acol(1)+acol(2)+acol(3));
      
      if isempty(Uo)==1; 
         if KP_fl>0; %KP is not set -> put the default for Uo
         Uo=600;
         set(Ht_edit(8),'ForegroundColor',[1 0 0],'String','600');
         else
         KP=str2num(get(Ht_edit(4),'String'));
         Tox=str2num(get(Ht_edit(7),'String'));
         Uo=1e4*KP*Tox/epsox;
         set(Ht_edit(8),'ForegroundColor',[0 0.6 0],'String',num2str(Uo));   
         %set(Ht_edit(4),'ForegroundColor',[0 0 0]); 
         end
      end
      if Uo<0; 
         if KP_fl>0; %KP is not set -> put the default for Uo
         Uo=600;
         set(Ht_edit(8),'ForegroundColor',[1 0 0],'String','600');
         else
         KP=str2num(get(Ht_edit(4),'String'));
         Tox=str2num(get(Ht_edit(7),'String'));
         Uo=1e4*KP*Tox/epsox;
         set(Ht_edit(8),'ForegroundColor',[0 0.6 0],'String',num2str(Uo));   
         %set(Ht_edit(4),'ForegroundColor',[0 0 0]); 
         end
      end

   case 9;
      THETA=str2num(get(Ht_edit(9),'String'));
      if isempty(THETA)==1; 
         THETA=0; 
         set(Ht_edit(9),'String','');
      end
      if THETA<0; 
         THETA=0; 
         set(Ht_edit(9),'String','');
      end;
      
   case 10;
      Vmax=str2num(get(Ht_edit(10),'String'));
      if isempty(Vmax)==1; 
         set(Ht_edit(10),'String','');
      end
      if Vmax<=300; 
         Vmax=str2num(''); 
         set(Ht_edit(10),'String','');
      end;
      if Vmax>1e7;
         Vmax=str2num(''); 
         set(Ht_edit(10),'String','');
      end
      
   case 11;
      Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
      if isempty(Nsub)==1; 
         set(Ht_edit(11),'String','');
         TPG=str2num('');
         Xj=str2num('');
         Nss=0;
         set([Ht_edit(14) Ht_edit(15) Ht_edit(17)],'String','');
         Pb=0.8;
         set(Ht_edit(16),'ForegroundColor',[1 0 0],'String','0.8');   
      end
      if Nsub<1e20; 
         Nsub=1e20; 
         set(Ht_edit(11),'String','1e14');
      end;
      if Nsub>1e27;
         Nsub=1e27;
         set(Ht_edit(11),'String','1e21');
      end   

   case 12;
      KAPPA=str2num(get(Ht_edit(12),'String'));
      if isempty(KAPPA)==1; 
         KAPPA=0; 
         set(Ht_edit(12),'String','');
      end
      if KAPPA<0; 
         KAPPA=0; 
         set(Ht_edit(12),'String','');
      end;
      if KAPPA>100;
         KAPPA=0; 
         set(Ht_edit(12),'String','');
      end;

   case 13;
      ETA=str2num(get(Ht_edit(13),'String'));
      if isempty(ETA)==1; 
         ETA=0; 
         set(Ht_edit(13),'String','');
      end
      if ETA<0; 
         ETA=0; 
         set(Ht_edit(13),'String','');
      end;
      if ETA>100;
         ETA=0;
         set(Ht_edit(13),'String','');
      end;

      
   case 14;
      Nss=str2num(get(Ht_edit(14),'String'))*1e4;
      if isempty(Nss)==1; 
         Nss=0; 
         set(Ht_edit(14),'String','');
      end
      if Nss<0; 
         Nss=0; 
         set(Ht_edit(14),'String','');
      end;
      
   case 15;
      set(Ht_edit(15),'ForegroundColor',[0 0 0]);
      TPG=str2num(get(Ht_edit(15),'String'));
      TPG_fl=0;
      if isempty(TPG)==1; 
        set(Ht_edit(15),'String','1','ForegroundColor',[1 0 0]);
        TPG=1;
      end
      if TPG==1; TPG_fl=1; end;
      if TPG==0; TPG_fl=1; end;
      if TPG==-1; TPG_fl=1; end;
      if TPG_fl<1;
        set(Ht_edit(15),'String','1','ForegroundColor',[1 0 0]);
        TPG=1;
     end;
     
  case 16;
      set(Ht_edit(16),'ForegroundColor',[0 0 0]);
      PB=str2num(get(Ht_edit(16),'String'));
      if isempty(PB)==1; 
         PB=0.8; 
         set(Ht_edit(16),'String','0.8','ForegroundColor',[1 0 0]);
      end
      if PB<=0; 
         PB=0.8; 
         set(Ht_edit(16),'String','0.8','ForegroundColor',[1 0 0]);
      end;
      
   case 17;
      Xj=str2num(get(Ht_edit(17),'String'));
      if isempty(Xj)==1; 
         Xj=str2num(''); 
         set(Ht_edit(17),'String','');
      end
      if Xj<=0; 
         Xj=str2num(''); 
         set(Ht_edit(17),'String','');
      end;
      
  case 18;
      Ldold=Ld;
      Ld=str2num(get(Ht_edit(18),'String'));
      if isempty(Ld)==1; 
         Ld=0; 
         set(Ht_edit(18),'String','');
      end
      if Ld<0; 
         Ld=0; 
         set(Ht_edit(18),'String','');
      end;
      L=str2num(get(Ht_edit(1),'String'));
      if (L-2*Ld)<0;
         Ld=Ldold; 
         if Ld==0;
            set(Ht_edit(18),'String','');
         else;   
         astr=num2str(Ld,'%1.2e');
         if Ld>0.01; astr=num2str(Ld,'%1.4f'); end
         set(Ht_edit(18),'String',astr);
         end
      end;

   case 19;
      DELTA=str2num(get(Ht_edit(19),'String'));
      if isempty(DELTA)==1; 
         DELTA=0; 
         set(Ht_edit(19),'String','');
      end
      if DELTA<0; 
         DELTA=0; 
         set(Ht_edit(19),'String','');
      end;
      
   case 20;
      NFS=str2num(get(Ht_edit(20),'String'));
      if isempty(NFS)==1; 
         NFS=0; 
         set(Ht_edit(20),'String','');
      end
      if NFS<0; 
         NFS=0; 
         set(Ht_edit(20),'String','');
      end;
      
   case 21; %Vsb voltage
      Vsb=str2num(get(Ht_edit(21),'String'));
      if isempty(Vsb)==1; 
         Vsb=0; 
         set(Ht_edit(21),'String','0');
      end;
      if Vsb<0;
         Vsb=0;
         set(Ht_edit(21),'String','0');
      end   
      if NMOS>0;
         set(Ht_par(21),'String','V_{SB}=','Color',[0 0.6 0]);
         set(Ht_un(21),'Color',[0 0.6 0]);
      else
         set(Ht_par(21),'String','V_{BS}=','Color',[0.8 0 0]);
        set(Ht_un(21),'Color',[0.8 0 0]);
      end   
           
   case 22; %Vdsq voltage
      Vdsq0=str2num(get(Ht_edit(22),'String'));
      if isempty(Vdsq0)==1; 
         Vdsq0=0.05; 
         set(Ht_edit(22),'String','0.05');
      end;
      if Vdsq0<0.01;
         Vdsq0=0.05;
         set(Ht_edit(22),'String','0.05');
      end   
      if NMOS>0;
         Vdsq=Vdsq0;
         set(Ht_par(22),'String','V_{DS}=','Color',[0 0.6 0]);
         set(Ht_un(22),'Color',[0 0.6 0]);
      else
         Vdsq=-Vdsq0;
         set(Ht_par(22),'String','V_{SD}=','Color',[0.8 0 0]);
         set(Ht_un(22),'Color',[0.8 0 0]);
      end   
      
   case 23; %Vdsmax voltage
      Vdsmax=str2num(get(Ht_edit(23),'String'));
      if isempty(Vdsmax)==1; 
         Vdsmax=5; 
         set(Ht_edit(23),'String','5');
      end;
      if Vdsmax<0.1;
         Vdsmax=0.1;
         set(Ht_edit(23),'String','0.1');
      end   
      Vdsmax=round(Vdsmax*10)/10;
      set(Ht_edit(23),'String',num2str(Vdsmax));
      Vds0=[0:(Vdsmax/80):Vdsmax];
      Vds=NMOS*Vds0;
      firstgo=1;
      
  case 24; %Vgstrmin voltage
      Vgstrmin=str2num(get(Ht_edit(24),'String'));
      if isempty(Vgstrmin)==1; 
         Vgstrmin=-0.5; 
         set(Ht_edit(24),'String','-0.5');
      end;
      if Vgstrmin>(Vgstrmax-0.01);
         Vgstrmin=Vgstrmax-0.01;
         set(Ht_edit(24),'String',num2str(Vgstrmin));
      end   
      Vgstrmin=round(Vgstrmin*100)/100;
      set(Ht_edit(24),'String',num2str(Vgstrmin));
      Vgstr0=[Vgstrmin:(Vgstrmax-Vgstrmin)/100:Vgstrmax];
      Vgstr=NMOS*Vgstr0;
      firstgo=1;
      
   case 25; %Vgstrmax voltage
      Vgstrmax=str2num(get(Ht_edit(25),'String'));
      if isempty(Vgstrmax)==1; 
         Vgstrmax=5; 
         set(Ht_edit(25),'String','5');
      end;
      if Vgstrmax<(Vgstrmin+0.01);
         Vgstrmax=Vgstrmin+0.01;
         set(Ht_edit(25),'String',num2str(Vgstrmax));
      end   
      Vgstrmax=round(Vgstrmax*100)/100;
      set(Ht_edit(25),'String',num2str(Vgstrmax));
      Vgstr0=[Vgstrmin:(Vgstrmax-Vgstrmin)/100:Vgstrmax];
      Vgstr=NMOS*Vgstr0;
      firstgo=1;
      
   case 26; %Vgsmax   
      Vgsmax=str2num(get(Ht_edit(26),'String'));
      if isempty(Vgsmax)==1;
         Vgsmax=5;
      end
      if abs(Vgsmax)<0.01;
         Vgsmax=5;
      end
      Vgsmax=abs(Vgsmax);
      if Vgsmax>=1;
         Vgsmax=round(Vgsmax);
      else
         Vgsmax=round(Vgsmax*10)/10;
      end
      set(Ht_edit(26),'String',num2str(Vgsmax));
      if NMOS>0;
         set(Ht_par(24),'String','V_{GSmax}=','Color',[0 0.6 0]);
         set(Ht_un(24),'Color',[0 0.6 0]);
      else
         set(Ht_par(24),'String','V_{SGmax}=','Color',[0.8 0 0]);
         set(Ht_un(24),'Color',[0.8 0 0]);
      end   
     Vgs0=[0 0.2 0.4 0.6 0.8 1.0]*Vgsmax;
     Vgs=NMOS*Vgs0;

   end; %switch   
   
   %drawnow;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% THE MODEL %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   Ld=str2num(get(Ht_edit(18),'String'));
   if isempty(Ld)==1; Ld=0; end
Leff=L-2*Ld;


%A. THE THRESHOLD VOLTAGE
%%%%%%%%%%%%%%%%%%%%%%%%%
%A1. Vto
%%%%%%%%%
Nsub=str2num(get(Ht_edit(11),'String'));
if isempty(Nsub)==1; %Nsub is not specified
   %Phi
   acol=get(Ht_edit(5),'ForegroundColor');
   Phi_fl=(acol(1)+acol(2)+acol(3));
   if Phi_fl>0; %Phi is not set -> put the default
      Phi=0.6;
      set(Ht_edit(5),'ForegroundColor',[1 0 0],'String','0.6');
   else
      Phi=str2num(get(Ht_edit(5),'String'));
      %set(Ht_edit(5),'ForegroundColor',[0 0 0]); 
   end 
   %Gamma
   acol=get(Ht_edit(6),'ForegroundColor');
   Gamma_fl=(acol(1)+acol(2)+acol(3)); 
   if Gamma_fl>0;  %Gamma is not set -> put the default
      Gamma=0;
      set(Ht_edit(6),'ForegroundColor',[1 0 0],'String','0');
   else
      Gamma=str2num(get(Ht_edit(6),'String'));
     %set(Ht_edit(6),'ForegroundColor',[0 0 0]);
   end

   Fs=1;
   set(Ht_edit(17),'String',''); %Remove Xj
   
   acol=get(Ht_edit(3),'ForegroundColor');
   Vto_fl=(acol(1)+acol(2)+acol(3));
   if Vto_fl>0; %Vto is not set -> put the default
      Vto=0;
      set(Ht_edit(3),'ForegroundColor',[1 0 0],'String','0');
   else   
      Vto=str2num(get(Ht_edit(3),'String'));
   end 
   
   set(Ht_edit(14),'String',''); %Remove Nss
   set(Ht_edit(15),'String',''); %Remove the TPG   
   
else %Nsub is specified
   Nsub=str2num(get(Ht_edit(11),'String'))*1e6;
   %Phi
   acol=get(Ht_edit(5),'ForegroundColor');
   if (acol(1)+acol(2)+acol(3))>0; %Phi is not set -> change it
      Phi=2*Vth*log(Nsub/ni);
      astr=num2str(Phi,'%1.4f');
      if Phi<1e-2; astr=num2str(Phi,'%1.2e'); end
      set(Ht_edit(5),'ForegroundColor',[0 0.6 0],'String',astr);
   else
      Phi=str2num(get(Ht_edit(5),'String'));
      %set(Ht_edit(5),'ForegroundColor',[0 0 0]); 
   end 
   %Gamma
   acol=get(Ht_edit(6),'ForegroundColor');
   if (acol(1)+acol(2)+acol(3))>0; %Gamma is not set -> change it
      Gamma=sqrt(2*epssi*q*Nsub)/Cox;
      astr=num2str(Gamma,'%1.4f');
      if Gamma<0.01; astr=num2str(Gamma,'%1.2e'); end
      set(Ht_edit(6),'ForegroundColor',[0 0.6 0],'String',astr);
   else
      Gamma=str2num(get(Ht_edit(6),'String'));
      %set(Ht_edit(6),'ForegroundColor',[0 0 0]);
   end
   
   %Fs
   %%%%%%%%%%
   if isempty(str2num(get(Ht_edit(17),'String')))==1; %Xj is not specified
      Fs=1;
   else
      Xj=str2num(get(Ht_edit(17),'String'));   
      wp=sqrt(2*epssi*(PB+Vsb)/(q*Nsub));
      wc=0.0631353*Xj+0.8013929*wp-0.0111077*wp^2/Xj;
      %Ld=str2num(get(Ht_edit(18),'String'));
      %if isempty(Ld)==1; Ld=0; end
      Fs=1-(Xj/Leff)*(((Ld+wc)/Xj)*sqrt(1-wp/(Xj+wp))-Ld/Xj);
      if Fs<0;
         Herd=errordlg({'Xj (Ld) too large or';'Nsub too small.';...
               'Xj will be removed.'},'STOP');  
         
         while ishandle(Herd)==1;
            if ishandle(Hf)==0; close(Herd); return; end
            pause(1);
         end   
               
         Fs=1;
         Xj=str2num(''); 
         set(Ht_edit(17),'String','');
      end   
   end
  
   
   %Vto 
   acol=get(Ht_edit(3),'ForegroundColor');
   if (acol(1)+acol(2)+acol(3))>0; %Vto is not set -> change it
      %Calculate Phims
           %Deal with TPG first
           anum=str2num(get(Ht_edit(15),'String'));
           if isempty(anum)==1; %set the default
             TPG=1; 
             set(Ht_edit(15),'String','1','ForegroundColor',[1 0 0]); 
           else
             TPG=anum;
           end;   
       if TPG==0; %Aluminium
          Phims=4.1-4.61-NMOS*Phi/2;
       else
          if TPG==-1; %Opposite gate
             Phims=NMOS*(Eg-Phi)/2;
          else; %Same gate  
             Phims=-NMOS*(Eg+Phi)/2;
          end;      
       end   
       %Nss
       Nss=str2num(get(Ht_edit(14),'String'))*1e4;
       if isempty(Nss)==1; Nss=0; end
       
     Vto=Phims-q*Nss/Cox+NMOS*Phi+NMOS*Gamma*Fs*sqrt(Phi); 
     set(Ht_edit(3),'ForegroundColor',[0 0.6 0],'String',num2str(Vto));
      
   else %Take the specified Vto
      Vto=str2num(get(Ht_edit(3),'String'));
      %set(Ht_edit(3),'ForegroundColor',[0 0 0]);
      set(Ht_edit(14),'String',''); %Remove Nss
      set(Ht_edit(15),'String',''); %Remove the TPG
   end
end;

%A2. Vt
%%%%%%%%%%%%%%%%
   DELTA=str2num(get(Ht_edit(19),'String'));
   if isempty(DELTA)==1; DELTA=0; end
Fn=DELTA*epssi*pi/(4*Cox*W);
   ETA=str2num(get(Ht_edit(13),'String'));
   if isempty(ETA)==1; ETA=0; end
Sigma_d=8.15e-22*ETA/(Cox*Leff^3);   

Vt=Vto+NMOS*Gamma*Fs*(sqrt(Phi+Vsb)-sqrt(Phi))-Sigma_d*Vds+NMOS*Fn*(Vsb+Phi);
Vtq=Vto+NMOS*Gamma*Fs*(sqrt(Phi+Vsb)-sqrt(Phi))-Sigma_d*Vdsq+NMOS*Fn*(Vsb+Phi);

%B. ns and Vts
%%%%%%%%%%%%%%%%%%%%%%
   NFS=str2num(get(Ht_edit(20),'String'));
   if isempty(NFS)==1; NFS=0; end
ns=1+q*NFS*1e4/Cox+(Gamma*Fs*(Phi+Vsb)^(-0.5)-Fn)/2;   
Vts=Vt+NMOS*ns*Vth;
Vtsq=Vtq+NMOS*ns*Vth;

if NMOS>0;
   for i=1:6;
   Vsubth(i,:)=Vts-Vgs(i);
   end   
   Vsubth=(Vsubth>0).*Vsubth;
   Vsubthtr=Vtsq-Vgstr;
   Vsubthtr=(Vsubthtr>0).*Vsubthtr;
else
   for i=1:6;
   Vsubth(i,:)=Vgs(i)-Vts;
   end   
   Vsubth=(Vsubth>0).*Vsubth;
   Vsubthtr=Vgstr-Vtsq;
   Vsubthtr=(Vsubthtr>0).*Vsubthtr;
end   


%C. FB
%%%%%%%%%%%%%%%%%%%%%%
FB=Gamma*Fs/(4*sqrt(Phi+Vsb))+Fn;

%D. MUs
%%%%%%%%%%%%%%%%%%%%%%%
acol=get(Ht_edit(4),'ForegroundColor');
if (acol(1)+acol(2)+acol(3))>0; %KP is not set
      acol=get(Ht_edit(8),'ForegroundColor');
      if acol(2)>0; %Uo is neither default nor set
         Uo=600;
         set(Ht_edit(8),'ForegroundColor',[1 0 0],'String','600');
      else   
         Uo=str2num(get(Ht_edit(8),'String'));
      end   
      %Tox=str2num(get(Ht_edit(7),'String'));
      KP=1e-4*Uo*epsox/Tox;
      astr=num2str(KP,'%1.4f');
      if KP<0.01; astr=num2str(KP,'%1.2e'); end
      set(Ht_edit(4),'ForegroundColor',[0 0.6 0],'String',astr);   
else 
   acol=get(Ht_edit(8),'ForegroundColor');   
   if (acol(1)+acol(2)+acol(3))>0; %Uo is not set, KP is set 
      KP=str2num(get(Ht_edit(4),'String'));
      Uo=1e4*KP*Tox/epsox;
     % set(Ht_edit(4),'ForegroundColor',[0 0 0],'String',num2str(KP));   
      set(Ht_edit(8),'ForegroundColor',[0 0.6 0],'String',num2str(Uo));   
   else   %Uo and KP set - take KP and change Uo
      KP=str2num(get(Ht_edit(4),'String'));
      Uo=1e4*KP*Tox/epsox;
     % set(Ht_edit(4),'ForegroundColor',[0 0 0],'String',num2str(KP));   
      set(Ht_edit(8),'ForegroundColor',[0 0.6 0],'String',num2str(Uo));   
   end   
end   
   THETA=str2num(get(Ht_edit(9),'String'));
   if isempty(THETA)==1; THETA=0; end
MUstr=Uo./(1+THETA*abs(Vgstr-Vtq));   
for i=1:6;
   MUs(i,:)=Uo./(1+THETA*abs(Vgs(i)-Vt));
end   
   
%E. Vdssat and MUeff
%%%%%%%%%%%%%%%%%%%%%%
for i=1:6;
   %Va(i,:)=(Vgs(i)-Vt)/(1+FB);
   if NMOS>0;
      Va(i,:)=((Vgs(i)*(Vgs(i)>Vts)+Vts.*(Vgs(i)<=Vts))-Vt)/(1+FB);
   else
      Va(i,:)=((Vgs(i)*(Vgs(i)<Vts)+Vts.*(Vgs(i)>=Vts))-Vt)/(1+FB);
   end   
end   
%Vatr=(Vgstr-Vtq)/(1+FB);
if NMOS>0;
   Vatr=((Vgstr.*(Vgstr>Vtsq)+Vtsq.*(Vgstr<=Vtsq))-Vtq)/(1+FB);
else
   Vatr=((Vgstr.*(Vgstr<Vtsq)+Vtsq.*(Vgstr>=Vtsq))-Vtq)/(1+FB);
end
Vmax=str2num(get(Ht_edit(10),'String'));
if isempty(Vmax)==1; 
   Vdssat=Va;
   Vdssattr=Vatr;
   MUeff=MUs;
   MUefftr=MUstr;
else
   Vb=Vmax*Leff./(MUs*1e-4);
   Vbtr=Vmax*Leff./MUstr;
   Vdssat=Va+NMOS*Vb-NMOS*sqrt(Va.^2+Vb.^2);
   Vdssattr=Vatr+NMOS*Vbtr-NMOS*sqrt(Vatr.^2+Vbtr.^2);
   for i=1:6;
   MUeff(i,:)=MUs(i,:)./(1+1e-4*MUs(i,:).*min(abs(Vdssat(i,:)),abs(Vds))./(Vmax*Leff));
   end
   MUefftr=MUstr./(1+1e-4*MUstr.*min(abs(Vdssattr),abs(Vdssattr*Vdsq./Vatr))/(Vmax*Leff));
end   

%F. Beta
%%%%%%%%%%%%%%%%%%%%%%%
if isempty(Nsub)==1;
   Lpinch=(Vdssat)*0;
   Lpinchtr=MUefftr*0;
   set(Ht_edit(12),'String','');
else
   KAPPA=str2num(get(Ht_edit(12),'String'));
   if isempty(KAPPA)==1; KAPPA=0; end
   for i=1:6;
      if NMOS>0;
         La(i,:)=sqrt(KAPPA*2*epssi*abs(Vds-Vdssat(i,:))/(q*Nsub)).*(Vds>Vdssat(i,:));
      else
         La(i,:)=sqrt(KAPPA*2*epssi*abs(Vds-Vdssat(i,:))/(q*Nsub)).*(Vds<Vdssat(i,:));
      end         
   end   
   if NMOS>0;
      Latr=sqrt(KAPPA*2*epssi*abs(Vdsq-Vdssattr)/(q*Nsub)).*(Vdsq>Vdssattr);
   else
      Latr=sqrt(KAPPA*2*epssi*abs(Vdsq-Vdssattr)/(q*Nsub)).*(Vdsq<Vdssattr);
   end   
   if isempty(Vmax)==1; 
      Lpinch=La;
      Lpinchtr=Latr;
   else
      for i=1:6;
         Lpinch(i,:)=sqrt((epssi*Vdssat(i,:)/(q*Nsub*Leff)).^2+La(i,:).^2)-...
            epssi*NMOS*Vdssat(i,:)/(q*Nsub*Leff);
      end   
      Lpinchtr=sqrt((epssi*Vdssattr/(q*Nsub*Leff)).^2+Latr.^2)-epssi*Vdssattr/(q*Nsub*Leff);
   end   
end

for i=1:6;
   Beta(i,:)=1e-4*MUeff(i,:).*Cox*W./(Leff-Lpinch(i,:));
end   
Betatr=1e-4*MUefftr*Cox*W./(Leff-Lpinchtr);

%G. THE CURRENTS
%%%%%%%%%%%%%%%%%%%%%%

for i=1:6;
   Ilin(i,:)=Beta(i,:).*((Vgs(i)-Vt)-0.5*(1+FB)*Vds).*Vds;
end  
Isub_lin=Beta(1,:).*((Vts-Vt)-0.5*(1+FB)*Vds).*Vds;
Ilintr=Betatr.*((Vgstr-Vtq)-0.5*(1+FB)*Vdsq)*Vdsq;
Isub_lintr=Betatr.*((Vtsq-Vtq)-0.5*(1+FB)*Vdsq)*Vdsq;

for i=1:6;
   %Isat(i,:)=0.5*Beta(i,:).*(Vgs(i)-Vt).^2/(1+FB);
   Isat(i,:)=Beta(i,:).*((Vgs(i)-Vt)-0.5*(1+FB)*Vdssat(i,:)).*Vdssat(i,:);
end
%Isub_sat=0.5*Beta(i,:).*(Vts-Vt).^2/(1+FB);
%Isattr=0.5*Betatr.*(Vgstr-Vtq).^2/(1+FB);
%Isub_sattr=0.5*Betatr.*(Vtsq-Vtq).^2/(1+FB);
Isub_sat=Beta(1,:).*((Vts-Vt)-0.5*(1+FB)*Vdssat(i,:)).*Vdssat(i,:);
Isattr=Betatr.*((Vgstr-Vtq)-0.5*(1+FB)*Vdssattr).*Vdssattr;
Isub_sattr=Betatr.*((Vtsq-Vtq)-0.5*(1+FB)*Vdssattr).*Vdssattr;

if NMOS>0;
for i=1:6;
   Iab(i,:)=(Vds<Vdssat(i,:)).*Ilin(i,:)+(Vds>=Vdssat(i,:)).*Isat(i,:);
   Isub(i,:)=(Vds<Vdssat(i,:)).*Isub_lin+(Vds>=Vdssat(i,:)).*Isub_sat;
end;   
Iabtr=(Vdsq<Vdssattr).*Ilintr+(Vdsq>=Vdssattr).*Isattr;
Isubtr=(Vdsq<Vdssattr).*Isub_lintr+(Vdsq>=Vdssattr).*Isub_sattr;
for i=1:6;
   Id(i,:)=Iab(i,:).*(Vgs(i)>Vts)+(Vgs(i)<=Vts).*Isub(i,:).*exp(-Vsubth(i,:)/(ns*Vth));
end
Idtr=Iabtr.*(Vgstr>=Vtsq)+(Vgstr<Vtsq).*Isubtr.*exp(-Vsubthtr/(ns*Vth));
else %it is a PMOS
for i=1:6;
   Iab(i,:)=(Vds>Vdssat(i,:)).*Ilin(i,:)+(Vds<=Vdssat(i,:)).*Isat(i,:);
   Isub(i,:)=(Vds>Vdssat(i,:)).*Isub_lin+(Vds<=Vdssat(i,:)).*Isub_sat;
end;   
Iabtr=(Vdsq>Vdssattr).*Ilintr+(Vdsq<=Vdssattr).*Isattr;
Isubtr=(Vdsq>Vdssattr).*Isub_lintr+(Vdsq<=Vdssattr).*Isub_sattr;
for i=1:6;
   Id(i,:)=Iab(i,:).*(Vgs(i)<Vts)+(Vgs(i)>=Vts).*Isub(i,:).*exp(-Vsubth(i,:)/(ns*Vth));
end
Idtr=Iabtr.*(Vgstr<Vtsq)+(Vgstr>=Vtsq).*Isubtr.*exp(-Vsubthtr/(ns*Vth));
end;   

%conW_usd13=0;
end; %if conW_usd13>0;


%%%%%%%%%%%%%%%%%%%%%
%%%RESET THE GRAPHS %
%%%%%%%%%%%%%%%%%%%%%
if ((conW_usd13>0)+(firstgo>0)+(conNP_usd13>0)+(conLIN_usd13>0));

if firstgo>0;
   Id_old=Id;
   Idtr_old=Idtr;
   firstgo=0;
end   

Idmax=max(1000*Id(6,:));
Ndig=10^floor(log10(Idmax));
Idmaxgr=Ndig*1*ceil((Idmax/Ndig)/1);
axes(Ha_idvd);
   set(Ha_idvd,'Xlim',[0 Vdsmax],'Ylim',[0 Idmaxgr]);
for i=1:6;
   set(Hp_o_old(i),'XData',Vds*NMOS,'YData',Id_old(i,:)*1000);
end   
for i=1:6;
   set(Hp_o(i),'XData',Vds*NMOS,'YData',Id(i,:)*1000);
end   

Idtrmax=max(1000*Idtr);
axes(Ha_idvg);
%set(Ha_idvg,'Ylim',[0 Idtrmaxgr]);
if linlg==2;
Ndig=10^floor(log10(Idtrmax));
Idtrmaxgr=Ndig*1*ceil((Idtrmax/Ndig)/1);
set(Ha_idvg,'Xlim',[Vgstrmin Vgstrmax],'Ylim',[0 Idtrmaxgr],'YScale','lin');
else   
Ndig=10^floor(log10(Idtrmax));
Idtrmaxgr=Ndig*10*ceil((Idtrmax/Ndig)/10);
set(Ha_idvg,'Xlim',[Vgstrmin Vgstrmax],'Ylim',[Idtrmaxgr/1e4 Idtrmaxgr],'YScale','log');
end
set(Hp_t_old,'XData',Vgstr*NMOS,'YData',abs(Idtr_old*1000));
set(Hp_t,'XData',Vgstr*NMOS,'YData',abs(Idtr*1000));

conW_usd13=0; conNP_usd13=0; conLIN_usd13=0;

if ishandle(Hf)==0;
   return;
end   
drawnow;

end; %if ((conW_usd13>0)+(firstgo>0)+(conNP_usd13>0));

if ishandle(Hf)==0;
   return;
end   
pause(1);
if ishandle(Hf)==0;
   return;
end   

end; %while con_go_usd13<1;
close;