{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Lorenz Attractor\n\nThis is an example of plotting Edward Lorenz's 1963 `\"Deterministic Nonperiodic\nFlow\"`_ in a 3-dimensional space using mplot3d.\n\n https://journals.ametsoc.org/view/journals/atsc/20/2/1520-0469_1963_020_0130_dnf_2_0_co_2.xml\n\n

#### Note

Because this is a simple non-linear ODE, it would be more easily done using\n SciPy's ODE solver, but this approach depends only upon NumPy.

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef lorenz(x, y, z, s=10, r=28, b=2.667):\n \"\"\"\n Given:\n x, y, z: a point of interest in three dimensional space\n s, r, b: parameters defining the lorenz attractor\n Returns:\n x_dot, y_dot, z_dot: values of the lorenz attractor's partial\n derivatives at the point x, y, z\n \"\"\"\n x_dot = s*(y - x)\n y_dot = r*x - y - x*z\n z_dot = x*y - b*z\n return x_dot, y_dot, z_dot\n\n\ndt = 0.01\nnum_steps = 10000\n\n# Need one more for the initial values\nxs = np.empty(num_steps + 1)\nys = np.empty(num_steps + 1)\nzs = np.empty(num_steps + 1)\n\n# Set initial values\nxs, ys, zs = (0., 1., 1.05)\n\n# Step through \"time\", calculating the partial derivatives at the current point\n# and using them to estimate the next point\nfor i in range(num_steps):\n x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])\n xs[i + 1] = xs[i] + (x_dot * dt)\n ys[i + 1] = ys[i] + (y_dot * dt)\n zs[i + 1] = zs[i] + (z_dot * dt)\n\n\n# Plot\nax = plt.figure().add_subplot(projection='3d')\n\nax.plot(xs, ys, zs, lw=0.5)\nax.set_xlabel(\"X Axis\")\nax.set_ylabel(\"Y Axis\")\nax.set_zlabel(\"Z Axis\")\nax.set_title(\"Lorenz Attractor\")\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 0 }