clear all;

%spektrum
%daten einlesen
%t=10sec
r=0.034;
m0=9.109 * 10^(-31);  %kg
c= 300000000;

d=load('spektrum1.csv');
B1=d(:,1);
n1=d(:,2:6);

%korrektur:
gt=1./(1 - n1.*(1.6.*10.^(-004)));
n1=n1.*gt*10;

nn1 = 1/5* (n1(:,1)+n1(:,2)+n1(:,3)+n1(:,4)+n1(:,5));
dn1=sqrt(1/5*((nn1-n1(:,1)).^2 + (nn1-n1(:,2)).^2 + (nn1-n1(:,3)).^2 + (nn1-n1(:,4)).^2 + (nn1-n1(:,5)).^2));

d=load('spektrum2.csv');
B2=d(:,1);
n2=d(:,2:11);

%korrektur:
gt=1./(1 - n2.*(1.6.*10.^(-004)));
n2=n2.*gt*10;

nn2 = 1/10* (n2(:,1)+n2(:,2)+n2(:,3)+n2(:,4)+n2(:,5)+n2(:,6)+n2(:,7)+n2(:,8)+n2(:,9)+n2(:,10));
dn2=sqrt(1/10*((nn2-n2(:,1)).^2 + (nn2-n2(:,2)).^2 + (nn2-n2(:,3)).^2 + (nn2-n2(:,4)).^2 + (nn2-n2(:,5)).^2 + (nn2-n2(:,6)).^2 + (nn2-n2(:,7)).^2 + (nn2-n2(:,8)).^2 + (nn2-n2(:,9)).^2 + (nn2-n2(:,10)).^2));

B=[B1;B2];
n=[nn1;nn2];
dn=[dn1;dn2];

%hintergrund abziehen
t=3600+12*60+31;
zk=40593/t;
gt=1./(1 - zk.*(1.6.*10.^(-004)));
nh=zk*gt*10;
dnh=sqrt(zk*gt*t)/t*10;

n=n-nh;
dn = sqrt(dn.^2+dnh.^2);

%impuls
p= (1.6021 * 10^(-19)) * r * B;
E = sqrt(p.^2 * c^2 + m0^2*c^4) - m0 * c^2;
%kurie=sqrt(n/(sqrt(E.^2+2.*m0.*c^2.*E).*(E+m0*c^2)));
kurie=sqrt(n)/p;
hold on;
plot(E,kurie);
%errorbar(E,n,dn,'.');

%spektrumfit
% % n=a(1) * p^2 *(a(2) -Es)^2
     a=[60,1*10^58];
% 	eps = 0.05;     %max relativer fehler
% 	
% 	ndata = numel(p);	
% 	ma = 2;
% 	
% 	[a,sd_a,var,covar,c] = mrqmin(p,n,dn,ndata,ma,a,eps,'funcs_spektrum');
%     disp('n=a(1) * exp(-0.5* (cps-a(2)).^2/a(3)^2)');
%     disp(a);
%     disp(sd_a);
    
    pp = linspace(min(p)*.9,max(p)*1.1);
    Es = sqrt(pp.^2 * c^2 + m0^2*c^4) - m0 * c^2;
    n_calc1=a(1) * pp.^2 .*(a(2) - Es).^2;
    
    %plot(pp,n_calc);
    hold off;

