%% Compute Optimal Rfb Based on Input Conditions

Rin  = 10e9;        % Input resistance (Ohms)
Cin  = 1.2e-12;     % Input capacitance (Farads)
fsig = 100e3;         % Signal frequency (Hz)
Asig = 0.5;         % Signal peak amplitude (Volts)
Vmax = 1.2;         % Max allowable output swing (Volts)

% --- Determine if input is resistive or capacitive ---
Xc = 1 / (2 * pi * fsig * Cin);   % Capacitive reactance

if Rin < Xc
    mode = 'Resistive';
    Iin_peak = Asig / Rin;
else
    mode = 'Capacitive';
    Iin_peak = 2 * pi * fsig * Asig * Cin;
end

% --- Compute Rfb limit ---
Rfb_max = Vmax / Iin_peak;

% --- Display Results ---
fprintf('--- Optimal Rfb Calculation ---\n');
fprintf('Input Mode: %s-dominated\n', mode);
fprintf('Peak Input Current: %.3e A\n', Iin_peak);
fprintf('Max Rfb: %.3e Ohms\n', Rfb_max);

