clear
maps = {'CMS', 'MS'};

taus = zeros(length(dir('data/resistance/resistance*.mat')), 1);
cell_types = cell(size(taus));
count = 1;
size(taus)
for j = 1:length(maps)
    files = dir(strcat('data/resistance/resistance_', maps{j}, '*.mat'));
    for i = 1:length(files)
        load(strcat('data/resistance/', files(i).name));
        disp(files(i).name)
        x = t(t>0.5 & t<15);
        x = x - x(1);
        y = V(t>0.5 & t<15);
        y = y - y(end);
        initial_params = [1, 2.5];
        [params, errors] = gradientDescent(x, y, initial_params);
        disp(count)
        taus(count) = params(2);
        cell_types{count} = maps{j};
        count = count + 1;
        plot_fit(x, y, errors, params);
    end
end


function plot_fit(x, y, errors, parameter)
    figure()
    subplot(2,1,1)
    hold on
    scatter(x,y, 'displayname', 'data')
    f_x = exponentialDecay(parameter, x);
    plot(x, f_x, 'displayname', 'fit')
    xlabel('Input')
    ylabel('Output')
    grid on
    legend show
    subplot(2,1,2)
    plot(errors)
    xlabel('optimization steps')
    ylabel('error')
end