如何使用matplotlib为逻辑方程K = 1生成矢量场图?

时间:2014-11-09 22:42:33

标签: python matplotlib numeric

我正在经历Strogatz的非线性动力学和混乱,我在第2章练习2.8.1中遇到了障碍。 (教育家的旗帜:我已经毕业,所以这不是一个班级,我只是想重新回到微分方程的数值求解中)这是一个非常简单的微分方程式和我可以根据不同的初始条件绘制单个解决方案曲线,但我尝试使用箭头或streamplot将各个解决方案叠加在矢量场之上。

我的问题在于理解如何将发现here的dy / dx形式中的类似问题的矢量场图转换为主要在Strogatz中处理的dx / dt形式。书。

鉴于逻辑函数中定义的x向量只有一维,我很难推断出如何表达u和v在quiver或streamplot中流动,因为问题似乎只是有流动。它可能非常简单并且被过度思考,但任何指导或帮助都会非常感激!

到目前为止,我有以下内容:

# 2.8.1
# Plot the vector field and some trajectories for xdot = x(1-x) given 
# some different initial conditions for the logistic equation with carrying 
# capacity K = 1

# dx/dt = x(1-x)  

# Imports:
from __future__ import division
from scipy import *
import numpy as np
import pylab
import matplotlib as mp
from matplotlib import pyplot as plt  
import sys
import math as mt

def logistic(x,t):
    return np.array([x[0]*(1-x[0])])

def RK4(t0 = 0, x0 = np.array([1]), t1 = 5 , dt = 0.01, ng = None):  
    tsp = np.arange(t0, t1, dt)
    Nsize = np.size(tsp)
    X = np.empty((Nsize, np.size(x0)))
    X[0] = x0

    for i in range(1, Nsize):
        k1 = ng(X[i-1],tsp[i-1])
        k2 = ng(X[i-1] + dt/2*k1, tsp[i-1] + dt/2)
        k3 = ng(X[i-1] + dt/2*k2, tsp[i-1] + dt/2)
        k4 = ng(X[i-1] + dt*k3, tsp[i-1] + dt)
        X[i] = X[i-1] + dt/6*(k1 + 2*k2 + 2*k3 + k4)
    return X

def tplot():
    t0 = 0
    t1 = 10
    dt = 0.02
    tsp = np.arange(t0,t1,dt)
    X = RK4(x0 = np.array([2]), t1 = 10,dt = 0.02, ng = logistic)
    Y = RK4(x0 = np.array([0.01]), t1 = 10,dt = 0.02, ng = logistic)
    Z = RK4(x0 = np.array([0.5]), t1 = 10,dt = 0.02, ng = logistic)
    P = RK4(x0 = np.array([3]), t1 = 10,dt = 0.02, ng = logistic)
    Q = RK4(x0 = np.array([0.1]), t1 = 10,dt = 0.02, ng = logistic)
    R = RK4(x0 = np.array([1.5]), t1 = 10,dt = 0.02, ng = logistic)
    O = RK4(x0 = np.array([1]), t1 = 10,dt = 0.02, ng = logistic)
    pylab.figure()
    pylab.plot(tsp,X)
    pylab.plot(tsp,Y)
    pylab.plot(tsp,Z)
    pylab.plot(tsp,P)
    pylab.plot(tsp,Q)
    pylab.plot(tsp,R)
    pylab.plot(tsp,O)
    pylab.title('Logistic Equation - K=1')
    pylab.xlabel('Time')
    pylab.ylabel('Xdot')
    pylab.show()

print tplot()

image here

2 个答案:

答案 0 :(得分:1)

要从导数(例如, dx / dt )绘制斜率图,您可以先找到 dx / dt ,然后使用固定的 dt < / em>计算 dx 。然后,在每个感兴趣的(t,x)处,绘制从(t,x)(t + dt,x + dx)的小线段

以下是等式 dx / dt = x(1-x)的示例。 (Strogatz图片没有箭头,所以我也删除了它们。)

import numpy as np
import matplotlib.pyplot as plt

times = np.linspace(0, 10,  20)
x = np.linspace(0 ,2, 20)
T, X = np.meshgrid(times, x)   # make a grid that roughly matches the Strogatz grid  

dxdt = X*(1-X)            # the equation of interest
dt = .5*np.ones(X.shape)  # a constant value (.5 is just so segments don't run into each other -- given spacing of times array
dx = dxdt * dt            # given dt, now calc dx for the line segment

plt.quiver(T, X, dt, dx, headwidth=0., angles='xy', scale=15.)
plt.show()

enter image description here

答案 1 :(得分:0)

wonkybadonk:由于绘制轨迹的斜率差异和绘制的矢量场似乎是由于您的矢量场不够陡峭的事实。确保这一点          dx = dxdt * dt; (逐点乘法,而不是点积)          并且您添加了&#34;角度=&#39; xy&#39;&#34;作为一个箭袋的论点。 (见tom10 post)。