%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Eksperimentel Fysik, Niels Bohr Institutet, Københavns Universitet
% Opgaver tirsdag d. 26. april og torsdag d. 28. april (uge 2), 2011
%
% Dette dokument indeholder løsninger til opgaven med Saras data. Disse opgaver
% blev skrevet af Kim Lefmann og lagt op som et PDF dokument på Absalon.
% 
% Pia Jensen, bozack@bozack.dk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all                  %tømmer alle gemte variable
close all                  %luk alle eksterne vinduer (figurer o.lign.)
clc                        %rens kommando-vinduet
datadir = 'data/saras/';   %mit data-directory


%% Originale vejledningskommandoer til dataindlæsning ---------------------------

% Importerer data til en matrix: 14 toplinier (headerlines) fjernes og 17 søjler 
% læses. Navne på søjlerne Kan ses i datafilen - søjle 4 (temperatur) og 9 
% (spændingen) er interessante for os
fid = fopen([datadir,'2010_01_0097.dat']);
Data0097 = cell2mat(textscan(fid, ...
                    '%n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n', ...
                    'headerlines',14));
fclose(fid)


%% Datafil nr. 97 ---------------------------------------------------------------

% Indlæser data
fid = fopen([datadir,'2010_01_0097.dat']);
Data0097 = cell2mat(textscan(fid, ...
                    '%n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n', ...
                    'headerlines',14));
fclose(fid);

% Plotter kolonne 4 (temperatur) vs. kolonne 9 (spænding)
figure
plot( Data0097(1:end,4),Data0097(1:end,9),'.','MarkerFaceColor', ...
      [0.4 0.4 0.8],'MarkerEdgeColor',[0.4 0.4 0.8] )
hold on
title('Originale data','FontWeight','b','FontSize',12)
xlabel('Temperatur','FontSize',12)
ylabel('Spænding','FontSize',12)
axis([0 4.2 9.45e-7 9.8e-7])
hold off

% Fjerner fejlmålte punkter
k=0;
for n = 1:length( Data0097(1:end,4) )
    if Data0097(n,4) < 1.5
        if Data0097(n,9) > 9.64*10^(-7)
            if Data0097(n,9) < 9.85*10^(-7)
                k = k + 1;
                DataCorrected(k,2) = Data0097(n,9);
                DataCorrected(k,1) = Data0097(n,4);
            end
        end
    else 
        k = k + 1;
        DataCorrected(k,2) = Data0097(n,9);
        DataCorrected(k,1) = Data0097(n,4);
    end
end

% Plotter de nye korrigerede data
figure
plot( DataCorrected(1:end,1),DataCorrected(1:end,2),'.','MarkerFaceColor', ...
      [0.4 0.4 0.8],'MarkerEdgeColor',[0.4 0.4 0.8] )
hold on
title('Korrigeret data','FontWeight','b','FontSize',12)
xlabel('Temperatur','FontSize',12)
ylabel('Spænding','FontSize',12)
axis([0 4.2 9.45e-7 9.8e-7])
hold off

% Sorterer de korrigerede data så de kan binnes nemt
DataCorrectedSorted = sortrows(DataCorrected);

% Binner de korrigerede (og sorterede) data
bin = 100;  %binning størrelse                  

% Finder de første værdier manuelt
% bemærk 1/sqrt(bin), så det er SDOM og ikke SD
T(1) = sum( DataCorrectedSorted(1:bin,1) )/bin;
T_u(1) = std( DataCorrectedSorted(1:bin,1) )/sqrt(bin); 
U(1) = sum( DataCorrectedSorted(1:bin,2) )/bin;
U_u(1) = std( DataCorrectedSorted(1:bin,2) )/sqrt(bin);

% Finder resten af værdierne
number_of_datapoints = length( DataCorrectedSorted(1:end,1) );
number_of_datapoints_fixed = fix( length( DataCorrectedSorted(1:end,2) )/bin );
for n = 2:number_of_datapoints_fixed - 1
    T(n)   = sum( DataCorrectedSorted(n*bin+1:(n+1)*bin,1) )/bin;
    T_u(n) = std( DataCorrectedSorted(n*bin+1:(n+1)*bin,1) )/sqrt(bin);
    U(n)   = sum( DataCorrectedSorted(n*bin+1:(n+1)*bin,2) )/bin;
    U_u(n) = std( DataCorrectedSorted(n*bin+1:(n+1)*bin,2) )/sqrt(bin);
end

% Plotter de nye forsimplede data
figure(2)
hold on
errorbar( T, U, U_u,'.','MarkerFaceColor',[0.1 0.1 0.6],...
          'MarkerEdgeColor',[0.1 0.1 0.6] )
title(['Korrigeret data (bin = ' num2str(bin) ')'],'FontWeight','b', ...
      'FontSize',12)
hold off

% Laver pænere udseende af de to figurer
figure(1)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(2)
ah2 = gca;
set(ah2,'Box','off')
set(ah2,'XGrid','on')
set(ah2,'YGrid','on')
set(ah2,'YColor',[0.0 0.0 0.0])
set(ah2,'XColor',[0.0 0.0 0.0])
set(ah2,'GridLineStyle',':')
set(ah2,'FontSize',8)


%% Datafil nr. 12, 13 og 14 -----------------------------------------------------

% Indlæser data
fid12 = fopen([datadir,'2010_01_0012.dat']);
fid13 = fopen([datadir,'2010_01_0013.dat']);
fid14 = fopen([datadir,'2010_01_0014.dat']);
Data0012 = cell2mat(textscan(fid12, ...
                    '%n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n', ...
                    'headerlines',14));
Data0013 = cell2mat(textscan(fid13, ...
                    '%n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n', ...
                    'headerlines',14));
Data0014 = cell2mat(textscan(fid14, ...
                    '%n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n %n', ...
                    'headerlines',14));
fclose(fid12); fclose(fid13); fclose(fid14);

% Plotter kolonne 2 (B-felt) vs. kolonne 9 (spænding)
figure
plot( Data0012(1:end,2),Data0012(1:end,9),'.','MarkerFaceColor', ...
      [0.4 0.4 0.8],'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 12)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Spænding','FontSize',12)
axis([-1.5 1.5 0.9e-6 1.3e-6])

figure
plot( Data0013(1:end,2),Data0013(1:end,9),'.','MarkerFaceColor', ...
      [0.4 0.4 0.8],'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 13)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Spænding','FontSize',12)
axis([-1.5 1.5 0.9e-6 1.3e-6])

figure
plot( Data0014(1:end,2),Data0014(1:end,9),'.','MarkerFaceColor', ...
      [0.4 0.4 0.8],'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 14)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Spænding','FontSize',12)
axis([-1.5 1.5 0.9e-6 1.3e-6])

% Udregner magnetiseringerne M = tau / B
M12 = Data0012(1:end,9) ./ Data0012(1:end,2);
M13 = Data0013(1:end,9) ./ Data0013(1:end,2);
M14 = Data0014(1:end,9) ./ Data0014(1:end,2);

% Plotter kolonne 2 (B-felt) vs. magnetisering
figure
semilogy( Data0012(1:end,2),M12,'.','MarkerFaceColor', [0.4 0.4 0.8],...
          'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 12)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Magnetisering','FontSize',12)

figure
semilogy( Data0013(1:end,2),M13,'.','MarkerFaceColor', [0.4 0.4 0.8],...
          'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 13)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Magnetisering','FontSize',12)

figure
semilogy( Data0014(1:end,2),M14,'.','MarkerFaceColor', [0.4 0.4 0.8],...
          'MarkerEdgeColor',[0.4 0.4 0.8] )
title('Originale data (datasæt 14)','FontWeight','b','FontSize',12)
xlabel('Magnetfelt','FontSize',12)
ylabel('Magnetisering','FontSize',12)

% Laver pænere udseende af de seks figurer
figure(1)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(2)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(3)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(4)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(5)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

figure(6)
ah1 = gca;
set(ah1,'Box','off')
set(ah1,'XGrid','on')
set(ah1,'YGrid','on')
set(ah1,'YColor',[0.0 0.0 0.0])
set(ah1,'XColor',[0.0 0.0 0.0])
set(ah1,'GridLineStyle',':')
set(ah1,'FontSize',8)

