close all
Vp = -4;
IDss = 8;

Vgs_vect = Vp:0;
Vds_vect = 0:0.1:20;

iD_vect = zeros(length(Vgs_vect),length(Vds_vect));

for ii = 1:length(Vgs_vect)
    
    Vgs = Vgs_vect(ii);
    
    for jj = 1:length(Vds_vect)
        
        Vds = Vds_vect(jj);

        
        if Vgs <= Vp
        % cutoff
        iD_vect(ii,jj) = 0;
        
        elseif (Vgs > Vp) & (Vds <= Vgs-Vp)
        
        %triode region
        iD_vect(ii,jj) = IDss * (2*(1-Vgs/Vp)*Vds/-Vp - (Vds/Vp)^2);
        
        elseif (Vgs > Vp) & (Vds > Vgs-Vp)
        
        % saturation region
        iD_vect(ii,jj) = IDss * (1-Vgs/Vp)^2;
        
        end
        
    end
end


figure

plot(Vds_vect,iD_vect.')
xlabel('V_D_S')
ylabel('I_D')


hold on

iD_borderTriodeSat= IDss/Vp^2 * Vds_vect(Vds_vect<= abs(Vp)).^2;

plot(Vds_vect(Vds_vect<= abs(Vp)),iD_borderTriodeSat,'--k')