%-----------------------------------------
%   ExtendDemographicVariables_S_Module
%---------------------------------------
%   This module extend fertility and poulation profiles 
%   till age 110
%-----------------------------------------
%   Extends population by 10 years (UN provides till age 100)
%-------------------------------------
ratio = popul(end-11:end-1)/sum(popul(end-11:end-1));
newSeg = ratio*popul(end);              % highest age is distributed into several ages
populN = [popul(1:end-1) newSeg];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Extend ll, expec, ll_f
%-------------------------------------
ages_ll = [0 1 5:5:85];                 % ages for which UN provides l(x)
ages_e = [0 1 5:5:100];                 % ages for which UN provides e(x)
q = ll(2:end)./ll(1:end-1);
q_f = ll_f(2:end)./ll_f(1:end-1);
qq = q./(ones(size(q))-q);
qq_f = q_f./(ones(size(q_f))-q_f);
[Nages_ll, qqN] = extend(ages_ll, qq, 5, 3, 5);    %extend(vector_x, vector_y, korak, N_lastPoints, N_newPoints)
[Nages_ll_f, qq_fN] = extend(ages_ll, qq_f, 5, 3, 5);
[Nages_e, expecN] = extend(ages_e, expec, 5, 6, 2);
q_fN = qq_fN./(ones(size(qq_fN))+qq_fN);
qN = qqN./(ones(size(qqN))+qqN);
llN = ll;
ll_fN = ll_f;
for a = numel(ll):numel(qN)
    llN(a+1) = llN(a)*qN(a);
    ll_fN(a+1) = ll_fN(a)*q_fN(a);
end
Nages_ll = [Nages_ll Nages_ll(end)+5];
Nages_ll_f = [Nages_ll_f Nages_ll_f(end)+5];
