import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint

# Les coefficients de la matrice A
a11, a12 = -1, 0
a21, a22 = 0, -3

def syst(y, t) :
    x1, x2 = y
    dydt = [a11*x1 + a12*x2, a21*x1 + a22*x2]
    return dydt

# Intervalle de temps, subdivisé
t = np.linspace (0, 2, 51)

# Conditions initiales
CI = [[1,1], [1,0.5], [1,0], [1,-0.5], [1,-1],
    [0.67,1], [0.33,1], [0,1], [-0.33,1], [-0.67,1],
    [0.67,-1], [0.33,-1], [0,-1], [-0.33,-1], [-0.67,-1],
    [-1,1], [-1,0.5], [-1,0], [-1,-0.5], [-1,-1]]

# Tracé des trajectoires
for y0 in CI :
    solp = odeint (syst, y0, t) # solution en temps positif
    plt.plot (solp[:, 0], solp[:, 1], "b")
    soln = odeint (syst, y0, -t) # solution en temps negatif
    plt.plot (soln[:, 0], soln[:, 1], "b")

# Configuration du repère
plt.grid()
plt.xlabel("x1")
plt.ylabel("x2")
plt.axis([-1, 1, -1, 1])

# Representation des tangentes aux trajectoires
g1 = np.linspace(-1, 1, 15)
g2 = np.linspace(-1, 1, 15)
A1, A2 = np.meshgrid(g1, g2)
V1, V2 = syst ((A1, A2) ,t)
r = np.sqrt(V1**2+V2**2)+1E-3
plt.quiver(A1, A2, V1/r, V2/r)
plt.show()