{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lorenz attractor plotting example\n",
    "\n",
    "This example is based on the following original matplotlib's example code:\n",
    "\n",
    "- http://matplotlib.org/examples/mplot3d/lorenz_attractor.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "require 'matplotlib/iruby'\n",
    "Matplotlib::IRuby.activate\n",
    "plt = Matplotlib::Pyplot\n",
    "\n",
    "require 'pycall/import'\n",
    "include PyCall::Import\n",
    "pyimport :numpy, as: :np\n",
    "\n",
    "require 'matplotlib/axes_3d' # enable 3d plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":lorenz"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def lorenz(x, y, z, s: 10, r: 28, b: 2.667)\n",
    "  x_dot = s * (y - x)\n",
    "  y_dot = r * x - y - x * z\n",
    "  z_dot = x * y - b * z\n",
    "  [x_dot, y_dot, z_dot]\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = 0.01\n",
    "stepCnt = 10_000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Need one more for the initial values\n",
    "xs = np.empty([stepCnt + 1])\n",
    "ys = np.empty([stepCnt + 1])\n",
    "zs = np.empty([stepCnt + 1])\n",
    "nil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.0, 1.0, 1.05]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Setting initial values\n",
    "xs[0], ys[0], zs[0] = 0.0, 1.0, 1.05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Stepping through \"time\".\n",
    "stepCnt.times do |i|\n",
    "  # Derivatives of the X, Y, Z state\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",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 176464 characters]",
      "text/plain": [
       "#<Matplotlib::Figure:0x007fd68e27e220 @__pyptr__=#<PyCall::PyPtr:0x007fd68e27e270 type=Figure addr=0x0000010a4fea20>>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "#<Object:0x007fd68e27f800 @__pyptr__=#<PyCall::PyPtr:0x007fd68e27f7d8 type=Text addr=0x0000010a551588>>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.gca(projection: '3d')\n",
    "\n",
    "ax.plot(xs, ys, zs, lw: 0.5)\n",
    "ax.set_xlabel(\"X Axis\")\n",
    "ax.set_ylabel(\"Y Axis\")\n",
    "ax.set_zlabel(\"Z Axis\")\n",
    "ax.set_title(\"Lorenz Attractor\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.4.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
