% =================================================================
% 3D Impedance Landscape - Derived from Bode Plot Data
% =================================================================
clear; clc; close all;

% --- 1. Axes (Matching the Bode Plot Bounds) ---
f_vec = logspace(3, 6, 100); % 1 kHz to 1 MHz
m_vec = linspace(0, 80, 100); % 0% to 80% coating/moisture
[F, M] = meshgrid(f_vec, m_vec);

% --- 2. Physics Model (Sigmoid Resistance Drop) ---
% Capacitance remains constant (~1.5 pF based on the 0% curve slope)
C_sensor = 1.5e-12; 

% Resistance crashes sharply between 15% and 35%
R_max = 1e8; 
R_min = 50;  
M_mid = 25;       % Midpoint of the resistance crash
steepness = 0.25; % Controls how sudden the drop is

% Sigmoid function for the sharp transition
R_s = R_min + (R_max - R_min) ./ (1 + exp(steepness * (M - M_mid)));

% --- 3. Impedance Calculation ---
Z_complex = 1 ./ (1./R_s + 1j*2*pi*F*C_sensor);
Z_mag = abs(Z_complex);
Phase = angle(Z_complex) * (180/pi);

% --- 4. Generate Surface ---
figure('Color', 'w', 'Position', [100 100 800 600]);
surf(F, M, log10(Z_mag), Phase);
shading interp; 
colormap(jet); 
caxis([-90 0]);

% --- 5. Aesthetics & Perspective ---
% A standard isometric-style view for clear axis reading
view(135, 30); 

set(gca, 'XScale', 'log', 'FontName', 'Times New Roman', 'FontSize', 12);
xlabel('Frequency (Hz)');
ylabel('Moisture (%)');
zlabel('log_{10} |Z| (\Omega)');

% Lock Z-axis to match Bode plot log scale (10^0 to 10^8)
zlim([0 8]);
set(gca, 'ZTick', 0:2:8, 'TickLabelInterpreter', 'tex');
set(gca, 'ZTickLabel', {'10^0','10^2','10^4','10^6','10^8'});

% Colorbar
c = colorbar;
ylabel(c, 'Phase Angle (\circ)');
c.Ticks = [-90 -45 0];
c.TickLabels = {'-90 (Capacitive)','-45 (Transition)','0 (Resistive)'};

grid on; box on;