莱斯利Leslie种群模型 python
sympy
import numpy as np
import sympy as sp
X0 = np.array([500, 1000, 500])
L = np.array([[0, 4, 3], [0.5, 0, 0], [0, 0.25, 0]])
X1 = L @ X0
X2 = L @ X1
X3 = L @ X2
Ls = sp.Matrix([[0, 4, 3], [sp.Rational(1, 2), 0, 0], [0, sp.Rational(1, 4), 0]])
lamda = sp.var('lamda')
p = Ls.charpoly(lamda)
w1 = sp.roots(p)
w2 = Ls.eigenvals()
v = Ls.eigenvects()
P, D = Ls.diagonalize()
Pinv = P.inv()
Pinv = sp.simplify(Pinv)
cc = Pinv @ X0
k = sp.var('k', positive=True, integer=True)
xk = P @ (D ** k) @ Pinv @ sp.Matrix(X0)
s = sp.simplify(xk[0])
print(s.subs(k, 2).n())