{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simulating Moving Objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the first few sections are not run in the Jupyter notebook.  You can rund `asteroids.py` from the command line and view that file directly in a text editor."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Digging deeper into Euler’s method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 0\n",
    "s = (0,0)\n",
    "v = (1,0)\n",
    "a = (0,0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt = 2\n",
    "steps = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from vectors import add, scale\n",
    "positions = [s]\n",
    "for _ in range(0,5):\n",
    "    t += 2\n",
    "    s = add(s, scale(dt,v)) \n",
    "    v = add(v, scale(dt,a)) \n",
    "    positions.append(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 600x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from draw2d import *\n",
    "draw2d(Points2D(*positions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19056 characters]\n",
      "text/plain": [
       "<Figure size 432x396 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 26552 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 27148 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def pairs(lst):\n",
    "    return list(zip(lst[:-1],lst[1:]))\n",
    "\n",
    "def eulers_method(s0,v0,a,total_time,step_count):\n",
    "    positions = [s0]\n",
    "    s = s0\n",
    "    v = v0\n",
    "    dt = total_time/step_count\n",
    "    for _ in range(0,step_count):\n",
    "        s = add(s,scale(dt,v))\n",
    "        v = add(v,scale(dt,a))\n",
    "        positions.append(s)\n",
    "    return positions\n",
    "\n",
    "approx5 = eulers_method((0,0),(1,0),(0,0.2),10,5)\n",
    "approx10 = eulers_method((0,0),(1,0),(0,0.2),10,10)\n",
    "approx100 = eulers_method((0,0),(1,0),(0,0.2),10,100)\n",
    "approx1000 = eulers_method((0,0),(1,0),(0,0.2),10,1000)\n",
    "\n",
    "from draw2d import *\n",
    "draw2d(\n",
    "    Points2D(*approx5, color='C0'),\n",
    "    *[Segment2D(t,h,color='C0') for (h,t) in pairs(approx5)],\n",
    "    Points2D(*approx10, color='C1'),\n",
    "    *[Segment2D(t,h,color='C1') for (h,t) in pairs(approx10)]\n",
    "    )\n",
    "\n",
    "from draw2d import *\n",
    "draw2d(\n",
    "    Points2D(*approx5, color='C0'),\n",
    "    *[Segment2D(t,h,color='C0') for (h,t) in pairs(approx5)],\n",
    "    Points2D(*approx10, color='C1'),\n",
    "    *[Segment2D(t,h,color='C1') for (h,t) in pairs(approx10)],\n",
    "    *[Segment2D(t,h,color='C2') for (h,t) in pairs(approx100)],\n",
    "    save_as='../3eulers.svg'\n",
    "    )\n",
    "\n",
    "draw2d(\n",
    "    Points2D(*approx5, color='C0'),\n",
    "    *[Segment2D(t,h,color='C0') for (h,t) in pairs(approx5)],\n",
    "    Points2D(*approx10, color='C1'),\n",
    "    *[Segment2D(t,h,color='C1') for (h,t) in pairs(approx10)],\n",
    "    *[Segment2D(t,h,color='C2') for (h,t) in pairs(approx100)],\n",
    "    *[Segment2D(t,h,color='C3') for (h,t) in pairs(approx1000)],\n",
    "    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
