Exploring the Lorenz System of Differential Equations

In this Notebook we explore the Lorenz system of differential equations:

$$ \begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned} $$

This is one of the classic systems in non-linear differential equations. It exhibits a range of different behaviors as the parameters ($\sigma$, $\beta$, $\rho$) are varied.

Imports

First, we import the needed things from IPython, NumPy, Matplotlib and SciPy.


In [1]:
%matplotlib inline

In [2]:
from ipywidgets import interact, interactive
from IPython.display import clear_output, display, HTML

In [3]:
import numpy as np
from scipy import integrate

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation

Computing the trajectories and plotting the result

We define a function that can integrate the differential equations numerically and then plot the solutions. This function has arguments that control the parameters of the differential equation ($\sigma$, $\beta$, $\rho$), the numerical integration (N, max_time) and the visualization (angle).


In [4]:
def solve_lorenz(N=10, angle=0.0, max_time=4.0, sigma=10.0, beta=8./3, rho=28.0):

    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1], projection='3d')
    ax.axis('off')

    # prepare the axes limits
    ax.set_xlim((-25, 25))
    ax.set_ylim((-35, 35))
    ax.set_zlim((5, 55))
    
    def lorenz_deriv(x_y_z, t0, sigma=sigma, beta=beta, rho=rho):
        """Compute the time-derivative of a Lorenz system."""
        x, y, z = x_y_z
        return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

    # Choose random starting points, uniformly distributed from -15 to 15
    np.random.seed(1)
    x0 = -15 + 30 * np.random.random((N, 3))

    # Solve for the trajectories
    t = np.linspace(0, max_time, int(250*max_time))
    x_t = np.asarray([integrate.odeint(lorenz_deriv, x0i, t)
                      for x0i in x0])
    
    # choose a different color for each trajectory
    colors = plt.cm.viridis(np.linspace(0, 1, N))

    for i in range(N):
        x, y, z = x_t[i,:,:].T
        lines = ax.plot(x, y, z, '-', c=colors[i])
        plt.setp(lines, linewidth=2)

    ax.view_init(30, angle)
    plt.show()

    return t, x_t

Let's call the function once to view the solutions. For this set of parameters, we see the trajectories swirling around two points, called attractors.


In [7]:
import seaborn


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-085c0287ecb5> in <module>()
----> 1 import seaborn

ImportError: No module named 'seaborn'

In [5]:
t, x_t = solve_lorenz(angle=0, N=10)


Using IPython's interactive function, we can explore how the trajectories behave as we change the various parameters.


In [6]:
w = interactive(solve_lorenz, angle=(0.,360.), max_time=(0.1, 4.0), 
                N=(0,50), sigma=(0.0,50.0), rho=(0.0,50.0))
display(w)


(array([ 0.        ,  0.004004  ,  0.00800801,  0.01201201,  0.01601602,
         0.02002002,  0.02402402,  0.02802803,  0.03203203,  0.03603604,
         0.04004004,  0.04404404,  0.04804805,  0.05205205,  0.05605606,
         0.06006006,  0.06406406,  0.06806807,  0.07207207,  0.07607608,
         0.08008008,  0.08408408,  0.08808809,  0.09209209,  0.0960961 ,
         0.1001001 ,  0.1041041 ,  0.10810811,  0.11211211,  0.11611612,
         0.12012012,  0.12412412,  0.12812813,  0.13213213,  0.13613614,
         0.14014014,  0.14414414,  0.14814815,  0.15215215,  0.15615616,
         0.16016016,  0.16416416,  0.16816817,  0.17217217,  0.17617618,
         0.18018018,  0.18418418,  0.18818819,  0.19219219,  0.1961962 ,
         0.2002002 ,  0.2042042 ,  0.20820821,  0.21221221,  0.21621622,
         0.22022022,  0.22422422,  0.22822823,  0.23223223,  0.23623624,
         0.24024024,  0.24424424,  0.24824825,  0.25225225,  0.25625626,
         0.26026026,  0.26426426,  0.26826827,  0.27227227,  0.27627628,
         0.28028028,  0.28428428,  0.28828829,  0.29229229,  0.2962963 ,
         0.3003003 ,  0.3043043 ,  0.30830831,  0.31231231,  0.31631632,
         0.32032032,  0.32432432,  0.32832833,  0.33233233,  0.33633634,
         0.34034034,  0.34434434,  0.34834835,  0.35235235,  0.35635636,
         0.36036036,  0.36436436,  0.36836837,  0.37237237,  0.37637638,
         0.38038038,  0.38438438,  0.38838839,  0.39239239,  0.3963964 ,
         0.4004004 ,  0.4044044 ,  0.40840841,  0.41241241,  0.41641642,
         0.42042042,  0.42442442,  0.42842843,  0.43243243,  0.43643644,
         0.44044044,  0.44444444,  0.44844845,  0.45245245,  0.45645646,
         0.46046046,  0.46446446,  0.46846847,  0.47247247,  0.47647648,
         0.48048048,  0.48448448,  0.48848849,  0.49249249,  0.4964965 ,
         0.5005005 ,  0.5045045 ,  0.50850851,  0.51251251,  0.51651652,
         0.52052052,  0.52452452,  0.52852853,  0.53253253,  0.53653654,
         0.54054054,  0.54454454,  0.54854855,  0.55255255,  0.55655656,
         0.56056056,  0.56456456,  0.56856857,  0.57257257,  0.57657658,
         0.58058058,  0.58458458,  0.58858859,  0.59259259,  0.5965966 ,
         0.6006006 ,  0.6046046 ,  0.60860861,  0.61261261,  0.61661662,
         0.62062062,  0.62462462,  0.62862863,  0.63263263,  0.63663664,
         0.64064064,  0.64464464,  0.64864865,  0.65265265,  0.65665666,
         0.66066066,  0.66466466,  0.66866867,  0.67267267,  0.67667668,
         0.68068068,  0.68468468,  0.68868869,  0.69269269,  0.6966967 ,
         0.7007007 ,  0.7047047 ,  0.70870871,  0.71271271,  0.71671672,
         0.72072072,  0.72472472,  0.72872873,  0.73273273,  0.73673674,
         0.74074074,  0.74474474,  0.74874875,  0.75275275,  0.75675676,
         0.76076076,  0.76476476,  0.76876877,  0.77277277,  0.77677678,
         0.78078078,  0.78478478,  0.78878879,  0.79279279,  0.7967968 ,
         0.8008008 ,  0.8048048 ,  0.80880881,  0.81281281,  0.81681682,
         0.82082082,  0.82482482,  0.82882883,  0.83283283,  0.83683684,
         0.84084084,  0.84484484,  0.84884885,  0.85285285,  0.85685686,
         0.86086086,  0.86486486,  0.86886887,  0.87287287,  0.87687688,
         0.88088088,  0.88488488,  0.88888889,  0.89289289,  0.8968969 ,
         0.9009009 ,  0.9049049 ,  0.90890891,  0.91291291,  0.91691692,
         0.92092092,  0.92492492,  0.92892893,  0.93293293,  0.93693694,
         0.94094094,  0.94494494,  0.94894895,  0.95295295,  0.95695696,
         0.96096096,  0.96496496,  0.96896897,  0.97297297,  0.97697698,
         0.98098098,  0.98498498,  0.98898899,  0.99299299,  0.996997  ,
         1.001001  ,  1.00500501,  1.00900901,  1.01301301,  1.01701702,
         1.02102102,  1.02502503,  1.02902903,  1.03303303,  1.03703704,
         1.04104104,  1.04504505,  1.04904905,  1.05305305,  1.05705706,
         1.06106106,  1.06506507,  1.06906907,  1.07307307,  1.07707708,
         1.08108108,  1.08508509,  1.08908909,  1.09309309,  1.0970971 ,
         1.1011011 ,  1.10510511,  1.10910911,  1.11311311,  1.11711712,
         1.12112112,  1.12512513,  1.12912913,  1.13313313,  1.13713714,
         1.14114114,  1.14514515,  1.14914915,  1.15315315,  1.15715716,
         1.16116116,  1.16516517,  1.16916917,  1.17317317,  1.17717718,
         1.18118118,  1.18518519,  1.18918919,  1.19319319,  1.1971972 ,
         1.2012012 ,  1.20520521,  1.20920921,  1.21321321,  1.21721722,
         1.22122122,  1.22522523,  1.22922923,  1.23323323,  1.23723724,
         1.24124124,  1.24524525,  1.24924925,  1.25325325,  1.25725726,
         1.26126126,  1.26526527,  1.26926927,  1.27327327,  1.27727728,
         1.28128128,  1.28528529,  1.28928929,  1.29329329,  1.2972973 ,
         1.3013013 ,  1.30530531,  1.30930931,  1.31331331,  1.31731732,
         1.32132132,  1.32532533,  1.32932933,  1.33333333,  1.33733734,
         1.34134134,  1.34534535,  1.34934935,  1.35335335,  1.35735736,
         1.36136136,  1.36536537,  1.36936937,  1.37337337,  1.37737738,
         1.38138138,  1.38538539,  1.38938939,  1.39339339,  1.3973974 ,
         1.4014014 ,  1.40540541,  1.40940941,  1.41341341,  1.41741742,
         1.42142142,  1.42542543,  1.42942943,  1.43343343,  1.43743744,
         1.44144144,  1.44544545,  1.44944945,  1.45345345,  1.45745746,
         1.46146146,  1.46546547,  1.46946947,  1.47347347,  1.47747748,
         1.48148148,  1.48548549,  1.48948949,  1.49349349,  1.4974975 ,
         1.5015015 ,  1.50550551,  1.50950951,  1.51351351,  1.51751752,
         1.52152152,  1.52552553,  1.52952953,  1.53353353,  1.53753754,
         1.54154154,  1.54554555,  1.54954955,  1.55355355,  1.55755756,
         1.56156156,  1.56556557,  1.56956957,  1.57357357,  1.57757758,
         1.58158158,  1.58558559,  1.58958959,  1.59359359,  1.5975976 ,
         1.6016016 ,  1.60560561,  1.60960961,  1.61361361,  1.61761762,
         1.62162162,  1.62562563,  1.62962963,  1.63363363,  1.63763764,
         1.64164164,  1.64564565,  1.64964965,  1.65365365,  1.65765766,
         1.66166166,  1.66566567,  1.66966967,  1.67367367,  1.67767768,
         1.68168168,  1.68568569,  1.68968969,  1.69369369,  1.6976977 ,
         1.7017017 ,  1.70570571,  1.70970971,  1.71371371,  1.71771772,
         1.72172172,  1.72572573,  1.72972973,  1.73373373,  1.73773774,
         1.74174174,  1.74574575,  1.74974975,  1.75375375,  1.75775776,
         1.76176176,  1.76576577,  1.76976977,  1.77377377,  1.77777778,
         1.78178178,  1.78578579,  1.78978979,  1.79379379,  1.7977978 ,
         1.8018018 ,  1.80580581,  1.80980981,  1.81381381,  1.81781782,
         1.82182182,  1.82582583,  1.82982983,  1.83383383,  1.83783784,
         1.84184184,  1.84584585,  1.84984985,  1.85385385,  1.85785786,
         1.86186186,  1.86586587,  1.86986987,  1.87387387,  1.87787788,
         1.88188188,  1.88588589,  1.88988989,  1.89389389,  1.8978979 ,
         1.9019019 ,  1.90590591,  1.90990991,  1.91391391,  1.91791792,
         1.92192192,  1.92592593,  1.92992993,  1.93393393,  1.93793794,
         1.94194194,  1.94594595,  1.94994995,  1.95395395,  1.95795796,
         1.96196196,  1.96596597,  1.96996997,  1.97397397,  1.97797798,
         1.98198198,  1.98598599,  1.98998999,  1.99399399,  1.997998  ,
         2.002002  ,  2.00600601,  2.01001001,  2.01401401,  2.01801802,
         2.02202202,  2.02602603,  2.03003003,  2.03403403,  2.03803804,
         2.04204204,  2.04604605,  2.05005005,  2.05405405,  2.05805806,
         2.06206206,  2.06606607,  2.07007007,  2.07407407,  2.07807808,
         2.08208208,  2.08608609,  2.09009009,  2.09409409,  2.0980981 ,
         2.1021021 ,  2.10610611,  2.11011011,  2.11411411,  2.11811812,
         2.12212212,  2.12612613,  2.13013013,  2.13413413,  2.13813814,
         2.14214214,  2.14614615,  2.15015015,  2.15415415,  2.15815816,
         2.16216216,  2.16616617,  2.17017017,  2.17417417,  2.17817818,
         2.18218218,  2.18618619,  2.19019019,  2.19419419,  2.1981982 ,
         2.2022022 ,  2.20620621,  2.21021021,  2.21421421,  2.21821822,
         2.22222222,  2.22622623,  2.23023023,  2.23423423,  2.23823824,
         2.24224224,  2.24624625,  2.25025025,  2.25425425,  2.25825826,
         2.26226226,  2.26626627,  2.27027027,  2.27427427,  2.27827828,
         2.28228228,  2.28628629,  2.29029029,  2.29429429,  2.2982983 ,
         2.3023023 ,  2.30630631,  2.31031031,  2.31431431,  2.31831832,
         2.32232232,  2.32632633,  2.33033033,  2.33433433,  2.33833834,
         2.34234234,  2.34634635,  2.35035035,  2.35435435,  2.35835836,
         2.36236236,  2.36636637,  2.37037037,  2.37437437,  2.37837838,
         2.38238238,  2.38638639,  2.39039039,  2.39439439,  2.3983984 ,
         2.4024024 ,  2.40640641,  2.41041041,  2.41441441,  2.41841842,
         2.42242242,  2.42642643,  2.43043043,  2.43443443,  2.43843844,
         2.44244244,  2.44644645,  2.45045045,  2.45445445,  2.45845846,
         2.46246246,  2.46646647,  2.47047047,  2.47447447,  2.47847848,
         2.48248248,  2.48648649,  2.49049049,  2.49449449,  2.4984985 ,
         2.5025025 ,  2.50650651,  2.51051051,  2.51451451,  2.51851852,
         2.52252252,  2.52652653,  2.53053053,  2.53453453,  2.53853854,
         2.54254254,  2.54654655,  2.55055055,  2.55455455,  2.55855856,
         2.56256256,  2.56656657,  2.57057057,  2.57457457,  2.57857858,
         2.58258258,  2.58658659,  2.59059059,  2.59459459,  2.5985986 ,
         2.6026026 ,  2.60660661,  2.61061061,  2.61461461,  2.61861862,
         2.62262262,  2.62662663,  2.63063063,  2.63463463,  2.63863864,
         2.64264264,  2.64664665,  2.65065065,  2.65465465,  2.65865866,
         2.66266266,  2.66666667,  2.67067067,  2.67467467,  2.67867868,
         2.68268268,  2.68668669,  2.69069069,  2.69469469,  2.6986987 ,
         2.7027027 ,  2.70670671,  2.71071071,  2.71471471,  2.71871872,
         2.72272272,  2.72672673,  2.73073073,  2.73473473,  2.73873874,
         2.74274274,  2.74674675,  2.75075075,  2.75475475,  2.75875876,
         2.76276276,  2.76676677,  2.77077077,  2.77477477,  2.77877878,
         2.78278278,  2.78678679,  2.79079079,  2.79479479,  2.7987988 ,
         2.8028028 ,  2.80680681,  2.81081081,  2.81481481,  2.81881882,
         2.82282282,  2.82682683,  2.83083083,  2.83483483,  2.83883884,
         2.84284284,  2.84684685,  2.85085085,  2.85485485,  2.85885886,
         2.86286286,  2.86686687,  2.87087087,  2.87487487,  2.87887888,
         2.88288288,  2.88688689,  2.89089089,  2.89489489,  2.8988989 ,
         2.9029029 ,  2.90690691,  2.91091091,  2.91491491,  2.91891892,
         2.92292292,  2.92692693,  2.93093093,  2.93493493,  2.93893894,
         2.94294294,  2.94694695,  2.95095095,  2.95495495,  2.95895896,
         2.96296296,  2.96696697,  2.97097097,  2.97497497,  2.97897898,
         2.98298298,  2.98698699,  2.99099099,  2.99499499,  2.998999  ,
         3.003003  ,  3.00700701,  3.01101101,  3.01501502,  3.01901902,
         3.02302302,  3.02702703,  3.03103103,  3.03503504,  3.03903904,
         3.04304304,  3.04704705,  3.05105105,  3.05505506,  3.05905906,
         3.06306306,  3.06706707,  3.07107107,  3.07507508,  3.07907908,
         3.08308308,  3.08708709,  3.09109109,  3.0950951 ,  3.0990991 ,
         3.1031031 ,  3.10710711,  3.11111111,  3.11511512,  3.11911912,
         3.12312312,  3.12712713,  3.13113113,  3.13513514,  3.13913914,
         3.14314314,  3.14714715,  3.15115115,  3.15515516,  3.15915916,
         3.16316316,  3.16716717,  3.17117117,  3.17517518,  3.17917918,
         3.18318318,  3.18718719,  3.19119119,  3.1951952 ,  3.1991992 ,
         3.2032032 ,  3.20720721,  3.21121121,  3.21521522,  3.21921922,
         3.22322322,  3.22722723,  3.23123123,  3.23523524,  3.23923924,
         3.24324324,  3.24724725,  3.25125125,  3.25525526,  3.25925926,
         3.26326326,  3.26726727,  3.27127127,  3.27527528,  3.27927928,
         3.28328328,  3.28728729,  3.29129129,  3.2952953 ,  3.2992993 ,
         3.3033033 ,  3.30730731,  3.31131131,  3.31531532,  3.31931932,
         3.32332332,  3.32732733,  3.33133133,  3.33533534,  3.33933934,
         3.34334334,  3.34734735,  3.35135135,  3.35535536,  3.35935936,
         3.36336336,  3.36736737,  3.37137137,  3.37537538,  3.37937938,
         3.38338338,  3.38738739,  3.39139139,  3.3953954 ,  3.3993994 ,
         3.4034034 ,  3.40740741,  3.41141141,  3.41541542,  3.41941942,
         3.42342342,  3.42742743,  3.43143143,  3.43543544,  3.43943944,
         3.44344344,  3.44744745,  3.45145145,  3.45545546,  3.45945946,
         3.46346346,  3.46746747,  3.47147147,  3.47547548,  3.47947948,
         3.48348348,  3.48748749,  3.49149149,  3.4954955 ,  3.4994995 ,
         3.5035035 ,  3.50750751,  3.51151151,  3.51551552,  3.51951952,
         3.52352352,  3.52752753,  3.53153153,  3.53553554,  3.53953954,
         3.54354354,  3.54754755,  3.55155155,  3.55555556,  3.55955956,
         3.56356356,  3.56756757,  3.57157157,  3.57557558,  3.57957958,
         3.58358358,  3.58758759,  3.59159159,  3.5955956 ,  3.5995996 ,
         3.6036036 ,  3.60760761,  3.61161161,  3.61561562,  3.61961962,
         3.62362362,  3.62762763,  3.63163163,  3.63563564,  3.63963964,
         3.64364364,  3.64764765,  3.65165165,  3.65565566,  3.65965966,
         3.66366366,  3.66766767,  3.67167167,  3.67567568,  3.67967968,
         3.68368368,  3.68768769,  3.69169169,  3.6956957 ,  3.6996997 ,
         3.7037037 ,  3.70770771,  3.71171171,  3.71571572,  3.71971972,
         3.72372372,  3.72772773,  3.73173173,  3.73573574,  3.73973974,
         3.74374374,  3.74774775,  3.75175175,  3.75575576,  3.75975976,
         3.76376376,  3.76776777,  3.77177177,  3.77577578,  3.77977978,
         3.78378378,  3.78778779,  3.79179179,  3.7957958 ,  3.7997998 ,
         3.8038038 ,  3.80780781,  3.81181181,  3.81581582,  3.81981982,
         3.82382382,  3.82782783,  3.83183183,  3.83583584,  3.83983984,
         3.84384384,  3.84784785,  3.85185185,  3.85585586,  3.85985986,
         3.86386386,  3.86786787,  3.87187187,  3.87587588,  3.87987988,
         3.88388388,  3.88788789,  3.89189189,  3.8958959 ,  3.8998999 ,
         3.9039039 ,  3.90790791,  3.91191191,  3.91591592,  3.91991992,
         3.92392392,  3.92792793,  3.93193193,  3.93593594,  3.93993994,
         3.94394394,  3.94794795,  3.95195195,  3.95595596,  3.95995996,
         3.96396396,  3.96796797,  3.97197197,  3.97597598,  3.97997998,
         3.98398398,  3.98798799,  3.99199199,  3.995996  ,  4.        ]),
 array([[[ -2.48933986,   6.6097348 , -14.99656876],
         [ -1.27447027,   6.27778097, -14.88707257],
         [ -0.25722317,   6.12979892, -14.74948804],
         ..., 
         [  5.32036927,   6.22605165,  17.23097523],
         [  5.45539837,   6.39261398,  17.18564024],
         [  5.59508392,   6.56430812,  17.14795467]],
 
        [[ -5.93002282, -10.59732328, -12.22984216],
         [ -6.62900726, -11.51134832, -11.82513862],
         [ -7.36381423, -12.51878894, -11.36636957],
         ..., 
         [ -2.85220055,  -3.17884274,  19.26071859],
         [ -2.90136148,  -3.24571225,  19.09550647],
         [ -2.95314777,  -3.31562455,  18.93347025]],
 
        [[ -9.41219366,  -4.63317819,  -3.09697577],
         [ -8.83423854,  -5.67287479,  -2.87758692],
         [ -8.47187066,  -6.64619187,  -2.63545814],
         ..., 
         [  9.9353102 ,   9.63098629,  27.04185483],
         [  9.88843899,   9.55329252,  27.13693426],
         [  9.83714455,   9.4724735 ,  27.22602599]],
 
        ..., 
        [[ 13.94520141,   4.90324493,   3.65087161],
         [ 12.79576674,   6.07398768,   3.90437431],
         [ 11.95486056,   7.13841575,   4.1885436 ],
         ..., 
         [ -9.68022335, -10.27613923,  23.34360663],
         [ -9.76591775, -10.33918286,  23.4979851 ],
         [ -9.84814472, -10.39674056,  23.65654846]],
 
        [[-11.55762081,  13.48467776,  -1.502636  ],
         [ -8.22824618,  12.33592752,  -1.99268875],
         [ -5.48564653,  11.51766268,  -2.29537794],
         ..., 
         [-10.16394423, -10.12097423,  26.17169753],
         [-10.15492846, -10.07488319,  26.3058404 ],
         [-10.14049547, -10.02362007,  26.43613619]],
 
        [[  2.35168843,  -2.75589592,  -7.88919059],
         [  1.67624957,  -2.47319098,  -7.82728918],
         [  1.12533125,  -2.27483902,  -7.75834743],
         ..., 
         [  8.39101357,   9.03991244,  22.18472012],
         [  8.48609466,   9.1346013 ,  22.25747636],
         [  8.58102139,   9.22780333,  22.33610257]]]))

The object returned by interactive is a Widget object and it has attributes that contain the current result and arguments:


In [7]:
t, x_t = w.result

In [8]:
w.kwargs


Out[8]:
{'N': 10,
 'angle': 0.0,
 'beta': 2.6666666666666665,
 'max_time': 4.0,
 'rho': 28.0,
 'sigma': 10.0}

After interacting with the system, we can take the result and perform further computations. In this case, we compute the average positions in $x$, $y$ and $z$.


In [9]:
xyz_avg = x_t.mean(axis=1)

In [10]:
xyz_avg.shape


Out[10]:
(10, 3)

Creating histograms of the average positions (across different trajectories) show that on average the trajectories swirl about the attractors.


In [11]:
plt.hist(xyz_avg[:,0])
plt.title('Average $x(t)$');



In [12]:
plt.hist(xyz_avg[:,1])
plt.title('Average $y(t)$');



In [ ]: