From b066e4ceaf58b87fbc29016aefeea160cfc4ab1e Mon Sep 17 00:00:00 2001 From: TQ Hirsch Date: Thu, 14 Apr 2022 14:43:33 +0200 Subject: [PATCH] Added section on polynomial evaluation to the notebook --- docs/.envrc | 1 + .../motion-control.ipynb | 53 +++++++++++++++++-- docs/shell.nix | 22 ++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 docs/.envrc rename motion-control.ipynb => docs/motion-control.ipynb (91%) create mode 100644 docs/shell.nix diff --git a/docs/.envrc b/docs/.envrc new file mode 100644 index 0000000..051d09d --- /dev/null +++ b/docs/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" diff --git a/motion-control.ipynb b/docs/motion-control.ipynb similarity index 91% rename from motion-control.ipynb rename to docs/motion-control.ipynb index 174d6e1..36e5be7 100644 --- a/motion-control.ipynb +++ b/docs/motion-control.ipynb @@ -466,7 +466,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "dab40dd2-2b62-4445-9a7a-4b02ae87fef0", "metadata": {}, "outputs": [], @@ -488,7 +488,7 @@ " MP_FRAC = 8 # 8 fractional bits\n", " MP_TICK_PER_SEC = 5000\n", " MP_MM_PER_STEP = 0.000\n", - " MP_AMAX = int(\n", + " MP_AMAX = 1\n", " " ] }, @@ -515,6 +515,53 @@ "Thus, our fundamental distance unit is that such that $j_{max} = 6$" ] }, + { + "cell_type": "markdown", + "id": "8cca2b8a-42a6-41c7-b0c6-eed4fd33342b", + "metadata": {}, + "source": [ + "## Polynomial evaluation\n", + "\n", + "As our position equation is a polynomial, we can evaluate it quickly at successive positions using the method of finite differences.\n", + "A semi-naiive evaluation of the formula would take 6 multiplications and 4 additions, whereas FD results in only the four additions.\n", + "\n", + "The differences can be calculated as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "f8c41a01-fe9c-495c-8644-600ce88ee2e8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle \\left\\{ FD_{0} : \\frac{a_{0}}{2} + \\frac{j}{6} + v_{0}, \\ FD_{1} : a_{0} + j, \\ FD_{2} : j\\right\\}$" + ], + "text/plain": [ + "⎧ a₀ j ⎫\n", + "⎨FD₀: ── + ─ + v₀, FD₁: a₀ + j, FD₂: j⎬\n", + "⎩ 2 6 ⎭" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "eq1 = seg_d()['dp']\n", + "def fd(eqn, var=t):\n", + " return (eqn.subs(var, var+1) - eqn).simplify()\n", + " \n", + "fd1 = fd(eq1)\n", + "fd2 = fd(fd1)\n", + "fd3 = fd(fd2)\n", + "\n", + "{sympy.Symbol(f\"FD_{i}\"): e.subs(t,0).simplify() for i, e in enumerate([fd1, fd2, fd3])}" + ] + }, { "cell_type": "markdown", "id": "d8b4e4c3-f76f-4de7-a570-c28a462f0283", @@ -550,7 +597,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.11" + "version": "3.9.10" } }, "nbformat": 4, diff --git a/docs/shell.nix b/docs/shell.nix new file mode 100644 index 0000000..ed0f982 --- /dev/null +++ b/docs/shell.nix @@ -0,0 +1,22 @@ +{ pkgs ? import {} }: + +let + pythonPackages = pkgs.python3.withPackages (p: with p; [ + ipython + jupyterlab + scipy + sympy + bokeh + bkcharts + ]); +in + +pkgs.mkShell { + + buildInputs = [ + pythonPackages + + # keep this line if you use bash + pkgs.bashInteractive + ]; +}