%clear all;
connor_data; % load data

%data = data_h;
N = length(data(:,1));

errorbar(data(:,1),data(:,2),data(:,3),'o','markersize',4); hold on;
fs = 14;
xlabel('Initial quantum number, n_i','fontsize',fs);
ylabel('Inverse wavelength, {\lambda}^{-1} (inv A)','fontsize',fs);
title('Rydberg fit of hydrogen Balmer lines','fontsize',fs);
text(6,2.3e-4,'R_H = (1.09711\pm0.00002)\times10^{7}m','fontsize',fs);
text(6,2.2e-4,'\chi^2_{\nu-1} = 0.65','fontsize',fs);
% Attempted fit
function_ryd = @(x,a) (a(1)*(1/4 - 1./x.^2));
a0 = [1.096e-3]; % actual Rydberg
dYda = {@(x,a) 1}; sgn = 0;
[a,aerr,chisq,yfit,corr] = levmar(data(:,1),data(:,2),data(:,3),...
    function_ryd,a0,dYda,sgn);

M = 1000;
x = linspace(data(1,1),data(N,1),M);
y = function_ryd(x,a);
plot(x,y,'r','linewidth',2);
legend('Measured data',...
       'Fitted Rydberg curve',4);


% Results
chisqdof = chisq/6;
fprintf(1,'Normalized chi sq is %f\n', chisqdof);