% Data dependent knobs

source = '546nm.txt';

N_i = 1; % Want to plot only the region of small current

         % Set N_i = 1 to see all data



% Use data points N_fi:N_ff for the linear fit

N_fi = 1;

N_ff = 6;



% To move around the text

xoffset = 2;



%------------------------------------------------------------

% Data set independent

%------------------------------------------------------------

data = load(source);

vr = data(:,1);

N = length(vr);

I = zeros(N,1);

sig = zeros(N,1);



for i = 1:N

    I(i) = mean(data(i,2:5));

    sig(i) = sqrt(var(data(i,2:5)));

end



% systematic error due to ambient current

ambient;

I = I - ac(1,1); % Subtract ambient current from data

I = I - min(I); % Offset the current curve

sig = sig + ac(1,2); % Add systematic error varation



% Rescale to I_0 = 1nA

scale_factor = 1000/I(1);

I = scale_factor*I;

sig = scale_factor*sig;



hold off; subplot(224);

errorbar(vr(N_i:N),I(N_i:N),sig(N_i:N),'o','markersize',6);

grid on;

xlabel('Retarding voltage V_R in volts');

ylabel('Photocurrent in pA');

headline = sprintf('Photocurrent at %s incident light',source(1:5));

title(headline);



[a aerr chisq yfit] = fitlin(vr(N_fi:N_ff),I(N_fi:N_ff),sig(N_fi:N_ff));

hold on;

plot(vr(N_fi:N_ff),yfit,'r');

text(vr(N_ff+xoffset),yfit(N_fi),'Fit: y = a + bx');

str1 = [num2str(a(1),3) '\pm' num2str(aerr(1),2)];

str2 = [num2str(a(2),3) '\pm' num2str(aerr(2),2)];

text(vr(N_ff+xoffset),yfit(N_fi+1),['a = ' str1]);

text(vr(N_ff+xoffset),yfit(N_fi+2),['b = ' str2]);