使用Matplotlib画洛仑兹吸引子

美国气象学家洛伦兹(E.N.Lorenz,不要和提出洛伦兹变换的那位搞混)是混沌理论的奠基者之一。20世纪50年代末到60年代初,他的主要工作目标是从理论上对天气情况进行长期预报。在使用计算机模拟天气时,他意外地发现,对于天气系统,哪怕初始条件的微小改变也会显著影响运算结果。随后,他在同事工作的基础上化简了自己先前的模型,得到了有3个变量的一阶微分方程组,由它描述的运动中存在一个奇异吸引子,即洛伦兹吸引子。

洛伦兹的工作结果最初在1963年发表,论文题目为Deterministic Nonperiodic Flow,发表在Journal of the Atmospheric Sciences杂志上。如今,这一方程组已成为混沌理论的经典,也是“巴西蝴蝶扇动翅膀在美国引起德克萨斯的飓风“(蝴蝶效应)一说的肇始。

下面,我们使用Python的绘图库Matplotlib来绘制一下洛伦兹吸引子曲线。代码如下,非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
 
"Lorenz's strange attractor"
 
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
#import numpy as np
import matplotlib.pyplot as plt
 
xs, ys, zs = [], [], []
 
def mkPoints():
    a, b, c = 10.0, 28.0, 8.0 / 3.0
    h = 0.01
    x0, y0, z0 = 0.1, 0, 0
    for i in xrange(10000):
        x1 = x0 + h * a * (y0 - x0)
        y1 = y0 + h * (x0 * (b - z0) - y0)
        z1 = z0 + h * (x0 * y0 - c * z0)
        x0, y0, z0 = x1, y1, z1
        xs.append(x0)
        ys.append(y0)
        zs.append(z0)
 
if __name__ == "__main__":
    mpl.rcParams["legend.fontsize"] = 10
    fig = plt.figure()
    ax = Axes3D(fig)
 
    mkPoints()
    ax.plot(xs, ys, zs, label = "Lorenz's strange attractor")
    ax.legend()
 
    plt.show()

最后得到的图形如下:

Lorenz's strange attractor

这个图形是三维的,在Matplotlib界面上,你可以用鼠标拖拽的方式来旋转它,以便从各个不同的角度观察它。