% Enter the data (these are dummy results)
% Skript for oppgave 3, Halveringstiden, i str?lings?velsen
% FYS1000, v?ren 2011
%
% Make sure you have the same number of points in the vectors.
% t is the time you measured, most conveniently in seconds.
% n is the counting rate (can be counts per 10 second or Bq).
% background is the background rate (same unit as n!!) before
% activating the source.

t = [60,  80,100,120,140,160,180,200,220,240,260,280,300]
n = [381,311,264,228,197,132,118,112,105, 83, 61, 56, 45]
background = 25
np = n - background

% Fit the data to an exponetial function.
% The resulting coefficients are stored in coefs.
% The starting values are just a very rough guess to help
% speed up the fitting procedure.
fun = @(p,t)p(1)*exp(-t*p(2));
startingValues = [400,0.01];
coeffs = nlinfit(t,np,fun,startingValues)
n0 = coeffs(1)
lambda = coeffs(2)

% Plot the data as o's and the fitted function as a curve
plot(t,np,'o')
hold on
fittedfun = @(t)n0*exp(-t*lambda)
fplot(fittedfun,xlim)

legend('data','tilpasset kurve')

% Choose your own histogram axis labels and title
xlabel('x-akse (enhet)')
ylabel('y-akse (enhet)')
title('Figur N: Min tittel')

% You may uncomment the following code if you understand
% how to compute the half-life from the decay constant lambda.
%
%xlimits=xlim
%ylimits=ylim
%halfstarting = 0.5*fittedfun(xlimits(1))
%line(xlim,[halfstarting,halfstarting])
%halflife = log(2)/lambda
%xvertical=xlimits(1)+halflife
%line([xvertical,xvertical],ylim)