%% PROGRAM ABOUT DEER AND THEIR OFSPRING
% TASK 4.8 IN MMG400
% WRITTEN BY JOHANNES BORGQVIST, 2016-09-27
format short % FOR PRINING WITH FOUR DECIMALS
% THE MATRIX WHICH DETERMINES DYNAMICS
A = [0, 0.6; 0.5, 0.7];
% THE INITIAL CONDITIONS
x0 = [100; 100];
% THE CALCULATED DIAGONALIZATION OF THE MATRIX
P =  [3, -2; 5, 1];
Pinv = (1/13).*[1, 2; -5, 3];
D = [1, 0; 0, -0.3];
% THE FIRST SOLUTION USING MATRIX A
sol1 = (A^10)*x0
% THE SECOND SOLUTION USING DIAGONALIZATION
sol2 = P*(D^10)*Pinv*x0

%% PLOTTING STUFF NICELY
nuOfGen = 11; % NUMBER OF GENERATIONS 
sol = zeros(2,nuOfGen); % THE SOLUTIONS TO THE EQUATIONS
% INITIAL CONDITIONS 
sol(:,1) = x0;
% SOLVE SYSTEM FOR THE COMING GENERATIONS 
for index = 2:nuOfGen
    sol(:,index) = (A^(index-1))*x0;
end

% GENERATIONVECTOR
generation = zeros(1,nuOfGen);
% WE START WITH INITAL GENERATION
generation(1,1) = 0; 
% FILL THE VECTOR WITH THE COMING TEN GENERATIONS
generation(1,2:nuOfGen) = (1:10);

fig = figure('units','normalized','outerposition',[0 0 1 1],'PaperPositionMode','auto');
clf
p = plot(generation,sol(1,:),'red',generation,sol(2,:),'blue');
set(p,'Linewidth',2)
grid on 
leg = legend('\texttt{Kid}','\texttt{R\aa djur}');
set(leg,'interpreter','latex','Fontsize',30,'Location','East')
ax = gca;
set(ax,'Ticklabelinterpreter','latex','Fontsize',25);
title({'\textbf{\textsf{MMG400 Uppgift 4.8: DISKRET DYNAMISKT SYSTEM}}','$\;\;\;\;\;\;\;\;\;\;$\textup{ANTAL R\AA DJUR SAMT KID SOM EN}','$\;\;\;\;\;\;\;\;\;\;$\textup{ FUNKTION AV ANTAL GENERATIONER}'},...
    'interpreter','latex','Fontsize',40)
ylabel('\textup{ANTAL DJUR}','interpreter','latex','Fontsize',35);
xlabel('\textup{ANTAL GENERATIONER}','interpreter','latex','Fontsize',35);
print(fig, '-depsc', '-r300', '20160927.eps')
