function [t] = stari_(N_tN,r)
% [t] = stari_(N_tN,r)
%
% Funkce zjisti, zda je obraz se zadanymi parametry stary 20 ci 300 let.
% Pokles poctu atomu Pb-210 v 1 gramu oxidu olovnateho popisujeme 
% rovnici N'(t) = -lambda*N(t) + r s pocatecni podminkou N(0) = N0 na <0,tN> 
%
% N_tN  ... pocet atomu Pb-210 v 1 gramu oxidu olovnateho v case tN
% r ... pocet atomu Pb-210, ktere vzniknou v 1 gramu oxidu olovnateho za 1 minutu rozpadem Ra-226 
%
% t ... zjistene stari obrazu (20 nebo 300 let)

p_r = 22; % polocas rozpadu (v letech)
lambda = log(2)/(p_r*525600);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%%% Test pro stari 300 let

tN = 300;
n = 10000; % pocet deleni <0,tN>

tN = tN * 525600;

h = tN / n;
t = 0 : h : tN;

N0 = 1e7;

if N0 < N_tN  % N(0) nemuze byt mensi nez N(tN)
    N0 = N_tN + 1e7;
end

N(n+1) = N_tN + 2e7; % volba vysledne hodnoty N tak, aby "reseni ulohy hned neskoncilo"

while (abs(N(n+1) - N_tN) > 1e7) & (N0 <= 5e11)
    
    N0 = N0 + 1e7;
    
    N(1) = N0;
    for i = 1:n
        N(i+1) = N(i) + h*(-lambda*N(i) + r);
    end
end

if abs(N(n+1) - N_tN) <= 1e7
    t = 300;
    disp(['Obraz je pravdepodobne stary 300 let a N(0) = ', num2str(N0,'%1.2g'),'.'])
    return
end
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%%% Test pro stari 20 let


tN = 20;
n = 10000;

tN = tN * 525600;

h = tN / n;
t = 0 : h : tN;

N0 = 1e7;

if N0 < N_tN   % N(0) nemuze byt mensi nez N(tN)
    N0 = N_tN + 1e7;
end

N(n+1) = N_tN + 2e7; % volba vysledne hodnoty N tak, aby "reseni ulohy hned neskoncilo"

while (abs(N(n+1) - N_tN) > 1e7) & (N0 <= 5e11)
    
    N0 = N0 + 1e7;
    
    N(1) = N0;
    for i = 1:n
        N(i+1) = N(i) + h*(-lambda*N(i)+r);
    end
end

if abs(N(n+1) - N_tN) <= 1e7
    t = 20;
    disp(['Obraz je pravdepodobne stary 20 let a N(0) = ', num2str(N0,'%1.2g'),'.'])
    return
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

error(['Nejsme schopni rozhodnout, zda je obraz stary 300 nebo 20 let.'])

% konec
