

%% fit the sigmoid

clear 
close all

load('iv_curve.mat')

figure()
plot(voltage, current, 'o')
xlabel('voltate [mV]')
ylabel('current [pA]')

% amplitude, slope, x-shift, y-shift
%parameter = [10 0.25 -50, 2.5];
parameter = [20 0.5 -50, 2.5];

eps = 0.1;
% do the descent
gradient = [];
steps = 0;
error = [];

while isempty(gradient) || norm(gradient) > 0.01
    steps = steps + 1; 
    gradient = lsq_gradient_sigmoid(parameter, voltage, current);
    error(steps) = lsq_sigmoid_error(parameter, voltage, current);
    parameter = parameter - eps .* gradient; 
end
plot(1:steps, error)

disp('gradient descent done!')
disp(strcat('final position: ', num2str(parameter)))
disp(strcat('final error: ', num2str(error(end))))

%% use fminsearch
parameter = [10 0.5 -50, 2.5];

objective_function = @(p)lsq_sigmoid_error(p, voltage, current);
param = fminunc(objective_function, parameter);
disp(param)
param1 = fminsearch(objective_function, parameter);
disp(param1)
