【发布时间】:2019-03-23 02:46:00
【问题描述】:
我有一个常微分方程组,它有两个吸引子,一个在 (1, 0),另一个在 (-1, 0)。我想在笛卡尔坐标中绘制一个吸引盆,其中有两种颜色,显示随着时间趋于正无穷大,每个坐标点上的一个点最终会成为哪个吸引子。但是,我不知道如何用 matplotlib 绘制这样的图。 这是我现在所做的:
from scipy.integrate import ode
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import norm
"""
The system of ODE:
x' = y
y' = x - x**3 - gamma*y
"""
# The system of equation
def f(t, r, arg):
return [r[1], r[0] - r[0] ** 3 - arg * r[1]]
# The Jacobian matrix
def jac(t, r, arg):
return [[0, 1], [1 - 3 * r[0] ** 2, -arg]]
# r is the vector (x,y)
# Initial condition, length of time evolution, time step, parameter gamma
def solver(r0, t0, t1, dt, gamma):
solution = ode(f, jac).set_integrator('dopri5')
# Set the value of gamma
solution.set_initial_value(r0, t0).set_f_params(gamma).set_jac_params(gamma)
return solution
# The function to find the fixed point each starting point ends at
def find_fp(r0, t0, t1, dt, gamma):
solution = solver(r0, t0, t1, dt, gamma)
error = 0.01
while solution.successful():
if norm(np.array(solution.integrate(solution.t+dt)) - np.array([1, 0])) < error:
return 1
elif norm(np.array(solution.integrate(solution.t+dt)) - np.array([-1, 0])) < error:
return -1
def fp(i, j, gamma):
t0, t1, dt = 0, 10, 0.1
return find_fp([i, j], t0, t1, dt, gamma)
我已经定义了几个函数。 f 是一个定义方程组的函数,jac 系统的雅可比矩阵,用作使用scipy.integrate.ode 的dopri5 方法(Kutta-Runge 方法)求解 ODE 的参数。 find_fp函数被定义为返回相空间中一个点将结束的吸引子,返回值1表示该点将结束于(1, 0),-1为(-1 , 0)。到目前为止,这些功能似乎运行良好。但是,我不知道如何使用我对matplotlib 模块所做的事情来绘制一个吸引盆地。有什么好的想法吗?
【问题讨论】:
-
对于您的问题:
plt.plot(i,j,'.b',ms=5)应该在坐标(i,j)处绘制一个大蓝点。如果增加探测点的密度,请缩小标记尺寸。
标签: python numpy matplotlib scipy differential-equations