function [ Vout ] = Diode_Clipper_AA_1( Vin )
% Diode Clipper Antialiased using the 1st-order antiderivative form

% Output Vector
Vout = zeros(size(Vin));

% Components and Parameters
R = 2.2e3;
Is = 2.52e-9;
VT = 25.85e-3;
N = 1.752;

% Reusable constants
NVT = N*VT;
one_NVT = 1/(NVT);

% State variables
Fn1 = 0;
xn1 = 0;

% Error threshold for ill-conditioning
thresh = 10e-10;

% Compute alfa
alfa = (R*Is)*one_NVT;


for n=1:length(Vin)

    xn = Vin(n);
    
    lambda = sign(xn);
    
    beta = lambda*one_NVT;

    % Compute Lambert First
    Lu = Lambert_W_Fritsch(alfa*exp(beta*xn));
    
    % Evaluate antiderivative
    Fn = xn^2/2 - lambda*NVT*( (1/(2*beta)) * Lu * (Lu + 2) ); 
    
    % Central difference
    xn_xn1 = xn-xn1;
    
    % Check for ill-conditioning
    if abs(xn-xn1)<thresh
        
        % First-order escape rule
        x_avg = 0.5*(xn + xn1);
        % Compute FIRST antiderivative (at x_avg)
        lambda = sign(x_avg);
        beta = lambda*one_NVT;
        Lu = Lambert_W_Fritsch(alfa*exp(beta*x_avg));
        Vout(n) = x_avg - lambda*NVT*Lu;
        
    else
    
        % Implement method
        Vout(n) = (Fn - Fn1)/xn_xn1;
    
    end
    
    % Update state variables
    Fn1 = Fn;
    xn1 = xn;

end


end

