clc;
clear all;
close all;

% Define component values
Rs = 10;   % Example value for Rs (Ohms)
Rp = 1200;  % Example value for Rp (Ohms)
Cp = 0.8e-6; % Example value for Cp (Farads)

% Calculate critical frequencies
wp = 1/(Cp * Rp);  % Pole at 1/(Cp * Rp) in rad/s
wz = 1/(Cp * (Rs * Rp) / (Rs + Rp));  % Zero at 1/[Cp(Rs || Rp)] in rad/s

% Convert wp and wz to Hz
fp = wp / (2*pi);  % Convert pole frequency to Hz
fz = wz / (2*pi);  % Convert zero frequency to Hz

% Display the values of Rs, Rp, and Cp
disp(['Rs (Ohms): ', num2str(Rs)]);
disp(['Rp (Ohms): ', num2str(Rp)]);
disp(['Cp (Farads): ', num2str(Cp)]);

% Display the values of the pole and zero in rad/s and Hz
disp(['Pole frequency (rad/s): ', num2str(wp)]);
disp(['Zero frequency (rad/s): ', num2str(wz)]);
disp(['Pole frequency (Hz): ', num2str(fp)]);
disp(['Zero frequency (Hz): ', num2str(fz)]);

% Create frequency vector for plot, converted from rad/s to Hz
w = logspace(1, 8, 500);  % Frequency range from 10^1 to 10^6 rad/s
f = w / (2*pi);  % Convert rad/s to Hz

% Calculate the magnitude and phase for the impedance Z(s)
Z = Rs + Rp * ((1 + 1j*w*Cp*(Rp*Rs)/(Rs + Rp)) ./ (1 + 1j*w*Cp*Rp));
magZ = abs(Z);   % Magnitude in linear scale (Ohms)
phaseZ = angle(Z) * (180/pi); % Phase in degrees

% Plotting the magnitude plot (linear scale)
figure;
subplot(2,1,1);
semilogx(f, magZ, 'r', 'LineWidth', 2); % Plot magnitude in linear scale
hold on;

% Set x-axis limits to ensure it goes up to 10^6 Hz
xlim([10^1, 10^6]);

% Manually set y-axis limit to handle small impedance values at high frequency
ylim([0 1500]);  % Adjust y-limit based on expected impedance values

% Annotate Rs and Rs + Rp values in the plot
text(10^0.85, Rs, '$R_s$', 'FontSize', 12, 'Interpreter', 'latex', 'Color', 'magenta');  % Shift Rs annotation
text(10^0.75, Rs + Rp, '$R_s + R_p$', 'FontSize', 12, 'Interpreter', 'latex', 'Color', 'magenta');  % Shift Rs + Rp annotation

% Add horizontal lines to denote Rs and Rs + Rp
yline(Rs, '--k', 'LineWidth', 1.5);    % Rs level
%yline(Rs + Rp, '--k', 'LineWidth', 1.5); % Rs + Rp level

% Mark critical frequencies fp and fz in Hz
xline(fp, '--k', 'LineWidth', 1.5); % Pole frequency in Hz
xline(fz, '--k', 'LineWidth', 1.5); % Zero frequency in Hz

% Add text for the equations at pole and zero frequencies using LaTeX fractions
text(fp, Rs + 400, '$\frac{1}{C_p R_p}$', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top', 'FontSize', 16, 'Interpreter', 'latex', 'Color', 'blue');
text(fz, (Rs + Rp)-800, '$\frac{1}{C_p \left( R_s \parallel R_p \right)}$', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top', 'FontSize', 16, 'Interpreter', 'latex', 'Color', [0, 0.5, 0]); % Dark green

% Labels and grid
title('|Z| (Ohms)');
ylabel('|Z| (Ohms)');
xlabel('Frequency (Hz)');
grid on;

% Plotting the phase plot
subplot(2,1,2);
semilogx(f, phaseZ, 'r', 'LineWidth', 2); % Plot phase in degrees
hold on;

% Set x-axis limits to ensure it goes up to 10^6 Hz
xlim([10^1, 10^6]);

% Mark critical frequencies fp and fz in Hz
xline(fp, '--k', 'LineWidth', 1.5); % Pole frequency in Hz
xline(fz, '--k', 'LineWidth', 1.5); % Zero frequency in Hz

% Add text for the equations at pole and zero frequencies using LaTeX fractions
text(fp, -180 + 120, '$\frac{1}{C_p R_p}$', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top', 'FontSize', 16, 'Interpreter', 'latex', 'Color', 'blue');
text(fz, -180 + 120, '$\frac{1}{C_p \left( R_s \parallel R_p \right)}$', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top', 'FontSize', 16, 'Interpreter', 'latex', 'Color', [0, 0.5, 0]); % Dark green

% Labels and grid
title('Phase (degrees)');
ylabel('Phase (degrees)');
xlabel('Frequency (Hz)');
grid on;
