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

% The following computes the stopping voltage and error

% based on the fitting coefficients

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

% a = 3.13e3;

% siga = 40;

% b = -3.77e3;

% sigb = 69;

% 

% Vs = -a/b

% sigvs = sqrt(1/b^2*siga^2+(a/b^2)^2*sigb^2)



% Now we want to do a fit of stopping voltage vs. frequency

data = [8.21E14 0.83 0.02;

        7.42E14 0.66 0.01;

        6.88E14 0.53 0.01;

        5.49E14 0.156 0.005];

hold off;

errorbar(data(:,1),data(:,2),data(:,3),'o','markersize',10);

%grid on;

xlabel('Incident light frequency in Hz');

ylabel('Stopping voltage in volts');

title('Stopping voltage as a function of incident light freq.');

ylim([0 1.1]);

xlim([4.5E14 8.5E14]);



[a aerr chisq yfit] = fitlin(data(:,1),data(:,2),data(:,3));

hold on;

plot(data(:,1),yfit,'r','linewidth',2);

text(5.3E14,0.9,'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(5.5E14,0.84,['a = ' str1]);

text(5.5E14,0.79,['b = ' str2]);

text(5.5E14,0.74,['\chi^2/dof = ' num2str(chisq/2,2)]);