function p11_tst(tstId)
% CEE 2016

% April 2016, April 2017, J. Gaspar

if nargin<1
    tstId= [0 1 2]; %2;
end
if length(tstId)>1
    for i=1:length(tstId), p11_tst(tstId(i)); drawnow; end
    return
end
switch tstId
    case 0, tst1_v0
    case 1, tst1_v1
    case 2, tst2
    otherwise
        error('inv tstId')
end


function tst1_v0

[n,p]= meshgrid(0:.1:1.1, 0:.1:2.1);
n= n(:); p=p(:);

% the nonlinear SYSTEM:
u= n.*(1-n-p);
v= p.*(.5-p/4 -3*n/4);

% clear vectors over p=2-2*n
% as they are large and therefore hide other info
if 1, %0
    ind= find(p>2-2*n); u(ind)=0; v(ind)=0;
end

figure(300); clf; hold on
set(gcf,'position', [21   330   478   365])

% plot local evolution at various initial conditions
quiver(n,p,u,v)
axis equal

% plot equilibrium points
np0= [0 0; 0 2; 1 0; .5 .5];
plot(np0(:,1), np0(:,2), 'or')
xlabel('N'); ylabel('P');

% plot eigenvectors (green=stable, red=unstable)
for i=1:length(np0)
    draw_eigenvectors( np0(i,1), np0(i,2) );
end

return


function tst1_v1
% same as v0, but zooming a region of interest

%[n,p]= meshgrid(0:.1:1.1, 0:.1:2.1);
[n,p]= meshgrid(0.45:.01:.55, 0.45:.01:.55); % zoom one eq point
sz= size(n);
n= n(:); p=p(:);

% the nonlinear SYSTEM:
u= n.*(1-n-p);
v= p.*(.5-p/4 -3*n/4);

figure(301); clf; hold on
set(gcf,'position', [7    39   352   255])
g= 1e3;
quiver(n,p,g*u,g*v)
xlabel('N'); ylabel('P');
axis tight
axis equal

figure(302); clf; hold on
set(gcf,'position', [374    38   416   258])
uv2= reshape(sqrt(u.*u+v.*v), sz);
%mask= (uv2>max(uv2(:,1))); uv2(mask)=max(uv2(:,1)); %0;
%imshow(uint8(inorm(uv2)))
%imagesc(uv2); axis xy
mesh(uv2)
view(24, 44)
xlabel('N'); ylabel('P'); zlabel('deriv vect norm')

return


function tst2

p11_sh_np(0,0)
p11_sh_np(0,2)
p11_sh_np(1,0)
p11_sh_np(.5,.5)


function draw_eigenvectors( N0, P0 )
[A, d, V]= p11_sh_np( N0, P0 );
washold= ishold;
a= axis;
a= 0.1*[a(2)-a(1) a(4)-a(3)];
for i=1:length(d)
    c= 'g';
    if real(d(i))>=0
        c= 'r';
    end
    v= V(:,i);
    plot( N0+a(1)*[-v(1) 0 v(1)], P0+a(2)*[-v(2) 0 v(2)], c )
end
if ~washold
    hold off
end
