%Complete fishery simulation
% kja 11 Jul 06 

aminit;

% Simulation of fishery with unrestricting fishing
global a b c r K k ki kt xr0
tmax=15;
str='b-';
x0=[100];
[t,x] = ode45('fishery_ol', tmax, x0);

subplot(3,1,1)
pl=plot(t,x(:,1),str);
set(pl, 'LineWidth', AM_data_linewidth);

u=max(min(3,x(:,1)-xr0-3),0);
h=a*x(:,1).*u;
g=b*h-c*u;
%axis([0 tmax 0 80]);
ylabel('x');
line([0 tmax], [0 0], 'LineWidth', AM_ref_linewidth);

subplot(3,1,2)
pl=plot(t,u,str);
set(pl, 'LineWidth', AM_data_linewidth);
%axis([0 tmax -0.2 2]);
ylabel('u')

subplot(3,1,3)
pl=plot(t,g,str);
set(pl, 'LineWidth', AM_data_linewidth);
%axis([0 tmax -2.5 5.5]);
ylabel('g');xlabel('t');

xt=x;ut=u;gt=g;tt=t;
n=size(t);n=n(1);tn=t(n);

% Simulation of fishery with a PI controller
global a b c r K k ki kt xr
tmax=35;
str='b-';
x0=[10,0];
[t,x]=ode45('fishery_cl',tmax,x0);
v=k*(xr-x(:,1))+x(:,2);
u=max(0,-v);
h=a*x(:,1).*u;
g=b*h-c*u;

subplot(3,1,1)
pl=plot(t,x(:,1),str);
set(pl,'LineWidth',2);
set(pl, 'LineWidth', AM_data_linewidth);
axis([0 tmax 0 80]);
ylabel('x');
line([0 tmax],[0 0]);
set(pl, 'LineWidth', AM_ref_linewidth);

subplot(3,1,2)
pl=plot(t,u,str);
set(pl,'LineWidth',2);
set(pl, 'LineWidth', AM_data_linewidth);
%axis([0 tmax -0.2 2]);
ylabel('u')

subplot(3,1,3)
pl=plot(t,g,str);
set(pl, 'LineWidth', AM_data_linewidth);
%axis([0 tmax -2.5 5.5]);
ylabel('g');xlabel('t');

v=k*(xr-x(:,1))+x(:,2);
u=max(0,-v);
h=a*x(:,1).*u;
g=b*h-c*u;
xt=[xt;x(:,1)];ut=[ut;u];gt=[gt;g];tt=[tt;t+tn];

subplot(4,1,1,'align');
pl=plot(tt,xt,str);
set(pl, 'LineWidth', AM_data_linewidth);
axis([0 50 0 100]);
ylabel('x');
%line([0 50],[0 0]);

subplot(4,1,2,'align');
pl=plot(tt,ut,str);
set(pl, 'LineWidth', AM_data_linewidth);
axis([0 50 0 4]);
ylabel('u')

subplot(4,1,3,'align');
pl=plot(tt,gt,str);
set(pl, 'LineWidth', AM_data_linewidth);
axis([0 50 0 30]);
ylabel('g');xlabel('t');

amprint('fishery-sim.eps');
