clear all 
close all 
clc

global V L

% initial conditions span
x10 = -4:0.5:4;%-4:2:4;
x20 = -6:0.5:6;


Vsela = [1 1;-1.56 2.56];
Vno = [1 1;-0.62 1.62];
L = [-3.56 0;0 -0.56];

for i=1:length(x10)
    for j=1:length(x20)
        x0 = [x10(i);x20(j)]; 
        [t,x] = ode45(@plano_nao_linear,[0:0.1:10],x0); % time simulation
        figure(1)
        plot(t,x(:,1),t,x(:,2));
        %axis([0 20 -30 30])
        hold on
        
        figure(2)
        plot(x(:,1),x(:,2),'-b','LineWidth',1)
        axis([-4 4 -6 6])

        hold on
    end
end

plot([0 2 1],[0 4 2],'rx','markersize',20)
return


y1 = Vsela(:,1)*[-4:0.01:4];
y2 = Vsela(:,2)*[-4:0.01:4];
plot(y1(1,:),y1(2,:),'--r','Linewidth',3)
hold on 
plot(y2(1,:),y2(2,:),'--r','Linewidth',3)

y1 = Vsela(:,1)*[-4:0.01:4];
y2 = Vsela(:,2)*[-4:0.01:4];
plot(y1(1,:)+2,y1(2,:)+4,'--r','Linewidth',3)
hold on 
plot(y2(1,:)+2,y2(2,:)+4,'--r','Linewidth',3)


y1 = Vno(:,1)*[-4:0.01:4];
y2 = Vno(:,2)*[-4:0.01:4];
plot(y1(1,:)+1,y1(2,:)+2,'--k','Linewidth',3)
hold on 
plot(y2(1,:)+1,y2(2,:)+2,'--k','Linewidth',3)
return

%%
%Pto (0,0) e (2,4)
function xdot = plano_P1(t,x)
global V L


x1 = x(1);
x2 = x(2);


xdot = [-2 1;4 -1]*x
x1dot = xdot(1);
x2dot = xdot(2);

xdot = [x1dot;x2dot];
end

%Pto (1,2) 
function xdot = plano_P2(t,x)
global V L


x1 = x(1);
x2 = x(2);

xdot = [-2 1;1 -1]*x;
x1dot = xdot(1);
x2dot = xdot(2);

xdot = [x1dot;x2dot];
end
%%

function xdot = plano_nao_linear(t,x)
global V L


x1 = x(1);
x2 = x(2);

x1dot = -2*x1+x2;
x2dot = x1*(x1^2-3*x1+4)-x2;

xdot = [x1dot;x2dot];
end
