Understanding the birds and the foxes with eigenvalues

In [2]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
In [24]:
A = np.array([[0.2, -1], [0.9, 0.3]])
A
Out[24]:
array([[ 0.2, -1. ],
       [ 0.9,  0.3]])
In [4]:
eigenvalues, eigenvectors = la.eig(A)
In [5]:
eigenvalues
Out[5]:
array([ 0.25+0.94736477j,  0.25-0.94736477j])

Let's make a time variable:

In [6]:
t = np.linspace(0, 10, 1000)

And compute a solution for each eigenvalue/eigenvector pair as sol1 and sol2, where each is a $1000\times 2$ array (time steps $\times$ number of species):

In [9]:
sol1 = np.exp(eigenvalues[0] * t.reshape(-1, 1)) * eigenvectors[:, 0]
In [19]:
sol2 = np.exp(eigenvalues[1] * t.reshape(-1, 1)) * eigenvectors[:, 1]

And plot the time behavior of the solution for both eigenvalue/eigenvector pairs:

In [29]:
plt.plot(t, sol1[:, 0].imag, label="birds")
plt.plot(t, sol1[:, 1].imag, label="foxes")
plt.legend()
Out[29]:
<matplotlib.legend.Legend at 0x7fe273bb1668>
In [30]:
plt.plot(t, sol2[:, 0].imag, label="birds")
plt.plot(t, sol2[:, 1].imag, label="foxes")
plt.legend()
Out[30]:
<matplotlib.legend.Legend at 0x7fe273b20c50>
In [ ]: