//TP07.sci

rand('seed',getdate('s'));

function Xdot = ProiePredateur(t,X)
    Xdot = zeros(2,1);
    Xdot(1) = X(1)*(1-X(2));
    Xdot(2) = alpha*X(2)*(X(1)-1);
endfunction

function Xdot = Chemostat(t,X)
    Xdot = zeros(2,1);
    Xdot(1) = c_beta*X(1)*X(2)/(1+X(2))-X(1);
    Xdot(2) = -X(1)*X(2)/(1+X(2))-X(2)+c_gamma;
endfunction

function Xdot = Symbiose(t,X)
    Xdot = zeros(2,1);
    Xdot(1) = X(1).*(1-X(1)+c_alpha*X(2));
    Xdot(2) = c_rho*X(2)*(1-X(2)+c_beta*X(1));
endfunction

function test01()
    alpha = 1;
    N0 = 5;
    u0 = grand(1,N0,'unf',0,3);
    v0 = grand(1,N0,'unf',0,3);
    T = 10;
    tt = 0:0.01:T;
    clf();
    for ii = 1:N0
        X = ode([u0(ii);v0(ii)],0,tt,ProiePredateur);
        plot(X(1,:),X(2,:),'-k',"thickness",2)
    end
    plot(1,1,"MarkStyle",".","MarkerSize",2)
    a = gca();
    a.font_size = 4;
    a.x_label.text = "u : proies";
    a.x_label.font_size = 4;
    a.y_label.text = "v : prédateurs";
    a.y_label.font_size = 4;
    xs2pdf(gcf(),'../Images/imgTP07_01.pdf')
    xs2gif(gcf(),'../Images/imgTP07_01.png')
endfunction

function test02()
    alpha = 1;
    N0 = 5;
    u0 = grand(1,N0,'unf',0,3);
    v0 = grand(1,N0,'unf',0,3);
    T = 20;
    tt = 0:0.01:T;
    clf();
    X0 = [0.2;0.2];
    X = ode(X0,0,tt,ProiePredateur);
    plot(tt,X(1,:),'-k','thickness',2);
    plot(tt,X(2,:),':k','thickness',2);
    a = gca();
    a.font_size = 4;
    a.x_label.text = "t : temps";
    a.x_label.font_size = 4;
    a.y_label.text = "u(t), v(t)";
    a.y_label.font_size = 4;
    legend(['u(t)','v(t)']);
    xs2pdf(gcf(),'../Images/imgTP07_02.pdf')
    xs2gif(gcf(),'../Images/imgTP07_02.png')
endfunction

function test03()
    c_beta = 2;
    c_gamma = 3;
    n0 = 0;
    c0 = c_gamma;
    n1 = c_beta*(c_gamma-1/(c_beta-1));
    c1 = 1/(c_beta-1);
    c2 = 0.3:0.01:4;
    n2 = (c_gamma-c2).*(1+c2)./c2;
    n3 = linspace(-0.8,4.8,15);
    c3 = linspace(-0.8,4.8,15);
    [nn3,cc3] = meshgrid(n3,c3);
    nn3dot = c_beta*nn3.*cc3./(1+cc3)-nn3;
    cc3dot = -nn3.*cc3./(1+cc3)-cc3+c_gamma;
    tt = 0:0.01:50;
    X0 = [ 0.2,-0.8 ;
             2,-0.8;
             4,-0.8;
          -0.2,-0.8;
          -0.2,4.8;
           0.2,4.8;
             2,4.8;
             4,4.8];
    X0 = X0';
    clf();
    plot([n0;n1],[c0;c1], "Linestyle","none",...
         "Marker", ".", "MarkerSize",6, 'MarkerEdgeColor','red')
    plot([0;0],[-1;5])
    plot([-1;5],[c1;c1])
    plot(n2,c2)
    champ(n3,c3,nn3dot',cc3dot',arfact=1.5)
    for ii=1:length(X0(1,:))
        X = ode(X0(:,ii),0,tt,Chemostat)
        plot(X(1,:),X(2,:),'-g','thickness',2)
    end
    a = gca();
    a.data_bounds = [-1,-0.1;5,5];
    a.font_size = 4;
    a.x_label.text = "n : bactéries";
    a.x_label.font_size = 4;
    a.y_label.text = "v : nutriments";
    a.y_label.font_size = 4;
    xs2pdf(gcf(),'../Images/imgTP07_03.pdf')
    xs2gif(gcf(),'../Images/imgTP07_03.png')
endfunction

function test04()
    clear()
    c_rho = 10;
    c_alpha = 0.25;
    c_beta = 2;
    u0 = [0,1,0,(1+c_alpha)/(1-c_alpha*c_beta)];
    v0 = [0,0,1,(1+c_beta)/(1-c_alpha*c_beta)];
    v1 = (-2):0.1:10;
    u1 = 1+c_alpha*v1;
    u2 = (-1):0.1:5;
    v2 = 1+c_beta*u2;
    u3 = (-0.5):0.5:4.5;
    v3 = (-1):9;
    [uu3,vv3] = meshgrid(u3,v3);
    uu3dot = uu3.*(1-uu3+c_alpha*vv3);
    vv3dot = c_rho*vv3.*(1-vv3+c_beta*uu3);
    X0 = [-0.1,9.5;
           0.1,9.5;
           1.1,9.5;
           2.1,9.5;
           3.1,9.5;
           4.9,0.1;
           3.9,0.1;
           2.9,0.1;
           1.9,0.1;
           1.1,0.001;
           0.9,0.001;
           0.1,0.001];
    X0 = X0';
    tt = 0:0.01:2;
          
    clf()
    plot(u0',v0,'LineStyle','none','Marker','.','MarkerSize',6,...
         'MarkerEdgeColor','red')
    plot([-1,5],[0,0])
    plot([0,0],[-2,10])
    plot(u1,v1)
    plot(u2,v2)
    champ(u3,v3,uu3dot',vv3dot',arfact=1.5)
    for ii = 1:length(X0(1,:))
        X = ode(X0(:,ii),0,tt,Symbiose);
        plot(X(1,:),X(2,:),'-g','Thickness',2)
    end
    
    a = gca()
    a.data_bounds = [-1,-2;5,10];
    a.font_size = 4;
    a.x_label.text = "u : première espèce";
    a.x_label.font_size = 4;
    a.y_label.text = "v : deuxième espèce";
    a.y_label.font_size = 4;
    xs2pdf(gcf(),'../Images/imgTP07_04.pdf')
    xs2gif(gcf(),'../Images/imgTP07_04.png')
endfunction


function test05()
    clear()
    c_rho = 10;
    c_alpha = 1;
    c_beta = 2;
    u0 = [0,1,0,(1+c_alpha)/(1-c_alpha*c_beta)];
    v0 = [0,0,1,(1+c_beta)/(1-c_alpha*c_beta)];
    v1 = (-2):0.1:10;
    u1 = 1+c_alpha*v1;
    u2 = (-1):0.1:5;
    v2 = 1+c_beta*u2;
    u3 = (-0.5):0.5:4.5;
    v3 = (-1):9;
    [uu3,vv3] = meshgrid(u3,v3);
    uu3dot = uu3.*(1-uu3+c_alpha*vv3);
    vv3dot = c_rho*vv3.*(1-vv3+c_beta*uu3);
    X0 = [-0.1,9.5;
           0.1,9.5;
           1.1,9.5;
           2.1,9.5;
           3.1,9.5;
           4.9,0.1;
           3.9,0.1;
           2.9,0.1;
           1.9,0.1;
           1.1,0.001;
           0.9,0.001;
           0.1,0.001];
    X0 = X0';
    tt = 0:0.01:1;
          
    clf()
    plot(u0',v0,'LineStyle','none','Marker','.','MarkerSize',6,...
         'MarkerEdgeColor','red')
    plot([-1,5],[0,0])
    plot([0,0],[-2,10])
    plot(u1,v1)
    plot(u2,v2)
    champ(u3,v3,uu3dot',vv3dot',arfact=1.5)
    for ii = 1:length(X0(1,:))
        X = ode(X0(:,ii),0,tt,Symbiose);
        plot(X(1,:),X(2,:),'-g','Thickness',2)
    end
    
    a = gca()
    a.data_bounds = [-1,-2;5,10];
    a.font_size = 4;
    a.x_label.text = "u : première espèce";
    a.x_label.font_size = 4;
    a.y_label.text = "v : deuxième espèce";
    a.y_label.font_size = 4;
    xs2pdf(gcf(),'../Images/imgTP07_05.pdf')
    xs2gif(gcf(),'../Images/imgTP07_05.png')
endfunction

//test01()
//test02()
//test03()
//test04()
test05()
