function fit_diode_curve_sem_drain_body()
global data
data = readmatrix("DRAIN_BODY2023_02_10_150122.csv", "Delimiter",";");
global I_L
I_L = 10e-11;
fprintf("Light generated current I_L = %g\n", I_L);
shifted_current = data(:, 2) - I_L;
[I_S, n] = fit_diode_forward(data(:, 1), shifted_current, 0.0);
fprintf("I_S = %g\n", I_S);
fprintf("n = %f\n", n);
[a, b] = fit_diode_reverse(data(:, 1), shifted_current, I_S);
fprintf("a = %g b = %g\n", a, b);
semilogy(data(:,1), abs(data(:,2)), ".r")
hold on
plot_domain = linspace(min(data(:,1)), 10.0, length(data(:,1)));
semilogy(plot_domain, abs(shockley(plot_domain, I_S, n, a, b)+I_L), "-b")
legend('Experimental Data', 'Fitted Model', 'Location','northwest')
hold off
xlabel("Voltage / V")
ylabel("Current / A")
title("Characteristic Curve of p-MOSFET")
grid()
return;
function current = shockley(voltage, I_S, n, a, b)
current = I_S.*(exp(1.60217e-19*voltage./(n.*293.15.*1.3806e-23))-1.0);
current(voltage<a) = current(voltage<a);
function [I_sat, n] = fit_diode_forward(voltage, current, resistance)
threshold_voltage = 4.2;
upper_voltage_threshold = 10.0;
k_B = physconst('Boltzmann');
global I_L
voltage = voltage - (current+I_L).*resistance;
current = current(voltage>threshold_voltage);
voltage = voltage(voltage>threshold_voltage);
current = current(voltage<upper_voltage_threshold);
voltage = voltage(voltage<upper_voltage_threshold);
p = polyfit(voltage, log(current), 1);
I_sat = exp(p(2));
n = 1./(293.15*k_B*p(1)./(1.602e-19));
function [a, b] = fit_diode_reverse(voltage, current, I_S)
k_B = physconst('Boltzmann');
T = 293.15;
threshold_voltage = -0.1;
current = current(voltage<threshold_voltage);
voltage = voltage(voltage<threshold_voltage);
current_hat = current + I_S;
p12 = polyfit(voltage, current_hat.^2, 1);
p13 = polyfit(voltage, current_hat.^3, 1);
b = -p12(1);
a = p12(2)./b;
function light_current = find_light_generated_current(voltage, current)
zero_volt_index = find(min(abs(voltage))==abs(voltage));
if voltage(zero_volt_index)<0.0
voltage1 = voltage(zero_volt_index);
current1 = current(zero_volt_index);
voltage2 = voltage(zero_volt_index+1);
current2 = current(zero_volt_index+1);
else
voltage1 = voltage(zero_volt_index-1);
current1 = current(zero_volt_index-1);
voltage2 = voltage(zero_volt_index);
current2 = current(zero_volt_index);
end
light_current = (current1.*(voltage2-0.0)-current2.*(voltage1))...
./(voltage2-voltage1);
Light generated current I_L = 1e-10
I_S = 4.61933e-12
n = 29.503181
a = -1.62781 b = 6.1705e-20