plotter_experiments/ode.ipynb

109 lines
116 KiB
Plaintext
Raw Permalink Normal View History

2022-12-11 12:58:10 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"from lib import plot\n",
"import IPython\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"297.637795pt\" height=\"419.527559pt\" viewBox=\"0 0 297.637795 419.527559\" version=\"1.1\">\n<g id=\"surface41\">\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 7.999512 7.999512 L 97.000109 7.999512 L 97.000109 140.000412 L 7.999512 140.000412 L 7.999512 7.999512 \" transform=\"matrix(2.834646,0,0,2.834646,0,0)\"/>\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(100%,0%,100%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 85.875206 74.000651 L 85.629915 73.948286 L 85.386003 73.897298 L 85.140712 73.846311 L 84.895421 73.796701 L 84.651508 73.747092 L 84.160927 73.647873 L 83.915636 73.599642 L 83.670345 73.550033 L 83.425054 73.503179 L 83.179763 73.454948 L 82.934473 73.408095 L 82.687804 73.361241 L 82.442513 73.314388 L 82.197222 73.268913 L 81.950553 73.223437 L 81.705263 73.177962 L 81.459972 73.133865 L 80.966634 73.045671 L 80.721343 73.002951 L 79.981337 72.874794 L 79.487999 72.792112 L 78.501324 72.632259 L 78.254655 72.593674 L 78.007986 72.556467 L 77.759939 72.517882 L 77.51327 72.480675 L 77.265224 72.444846 L 77.018555 72.407639 L 76.770508 72.37181 L 76.523839 72.337359 L 76.027745 72.268457 L 75.781076 72.234006 L 75.53303 72.200933 L 75.284983 72.169238 L 75.036936 72.136165 L 74.788889 72.105849 L 74.540842 72.074154 L 74.292795 72.043837 L 74.044748 72.014898 L 73.796701 71.984581 L 73.547276 71.95702 L 73.29923 71.928082 L 73.051183 71.901899 L 72.801758 71.874338 L 72.553711 71.848155 L 72.305664 71.823351 L 72.056239 71.797168 L 71.806814 71.773741 L 71.558767 71.750315 L 71.309342 71.726888 L 71.061296 71.704839 L 70.811871 71.682791 L 70.313021 71.64145 L 69.814171 71.602865 L 69.564746 71.58495 L 68.816471 71.535341 L 68.567046 71.520182 L 67.818772 71.478841 L 67.569347 71.467817 L 67.318544 71.456793 L 67.069119 71.445768 L 66.819694 71.436122 L 66.570269 71.427854 L 66.319466 71.420964 L 65.820616 71.407183 L 65.569813 71.403049 L 65.320388 71.398915 L 65.069586 71.396159 L 64.820161 71.393403 L 64.570736 71.392025 L 64.319933 71.392025 L 64.070508 71.393403 L 63.819705 71.396159 L 63.57028 71.398915 L 63.320855 71.403049 L 63.070052 71.408561 L 62.820627 71.415451 L 62.569824 71.422342 L 62.070974 71.441634 L 61.821549 71.454036 L 61.570747 71.466439 L 61.321322 71.480219 L 61.071897 71.495378 L 60.573047 71.531207 L 60.325 71.551877 L 60.075575 71.572548 L 59.82615 71.595974 L 59.578103 71.620779 L 59.328678 71.646962 L 59.080632 71.674523 L 58.832585 71.704839 L 58.584538 71.736534 L 58.336491 71.770985 L 58.089822 71.806814 L 57.841775 71.845399 L 57.595106 71.885362 L 57.348437 71.928082 L 57.103147 71.972179 L 56.857856 72.02041 L 56.612565 72.07002 L 56.368652 72.122385 L 56.12474 72.178885 L 55.882205 72.23814 L 55.63967 72.300152 L 55.398513 72.366298 L 55.158735 72.4352 L 54.918956 72.509614 L 54.681934 72.586784 L 54.446289 72.670844 L 54.212023 72.759039 L 53.980512 72.852745 L 53.751758 72.95472 L 53.527138 73.063585 L 53.306651 73.180718 L 53.091678 73.308876 L 52.88635 73.450814 \" transform=\"matrix(2.834646,0,0,2.834646,0,0)\"/>\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(100%,0%,100%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 84.637728 83.004753 L 84.415864 82.887619 L 84.195378 82.771864 L 83.751649 82.540354 L 83.529785 82.425977 L 83.306543 82.311599 L 82.862815 82.082845 L 82.193088 81.743848 L 81.969846 81.632227 L 81.746604 81.519227 L 81.521984 81.407606 L 81.298741 81.297363 L 80.849501 81.076877 L 80.626259 80.966634 L 80.40026 80.856391 L 79.95102 80.638661 L 79.499023 80.423687 L 79.274403 80.3162 L 79.048405 80.208713 L 78.821029 80.102604 L 78.59503 79.996495 L 78.369032 79.891764 L 77.686903 79.577572 L 77.004774 79.267513 L 76.77602 79.165538 L 76.547266 79.064941 L 76.318511 78.962967 L 75.861003 78.761773 L 75.632248 78.66255
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = plot.A6_PORTRAIT\n",
"p_svg = plot.SVGPlotter('plots/ode.svg', a)\n",
"p_hpgl = plot.HPGLPlotter(a, 'plots/ode_{index}.hpgl')\n",
"p = plot.MultiPlotter()\n",
"p.register_plotter(p_svg)\n",
"p.register_plotter(p_hpgl)\n",
"\n",
"p.move_to(a.bottom_left())\n",
"p.line_to(a.bottom_right())\n",
"p.line_to(a.top_right())\n",
"p.line_to(a.top_left())\n",
"p.line_to(a.bottom_left())\n",
"\n",
"A = np.array([[-0.4, -0.2], [0.1, -0.3]])\n",
"A = np.random.normal(size=(2, 2))\n",
"\n",
"def plot_ode(A):\n",
" dia = np.min(a.size()) * 0.75\n",
" # step_size = 0.01 / np.max(np.abs(np.linalg.eig(A)[0]))\n",
"\n",
" states = np.linspace(0, 2 * np.pi, 24)\n",
" states = np.vstack([np.cos(states), np.sin(states)])\n",
" for s in states.T:\n",
" p.move_to(s * dia / 2 + a.centre())\n",
" for _ in range(100000):\n",
" step = A @ s\n",
" step *= (0.5 / dia) / np.linalg.norm(step)\n",
" s = step + s\n",
" n = s * dia / 2 + a.centre()\n",
" if not a.is_within(n):\n",
" break\n",
" if np.linalg.norm(s * dia / 2) < 0.5:\n",
" break\n",
" p.line_to(n)\n",
"\n",
"p.add_layer([1, 0, 1, 0.5])\n",
"plot_ode(A)\n",
"p.add_layer([1, 0, 0, 0.5])\n",
"plot_ode(-A)\n",
"# A = np.linalg.inv(A)\n",
"# plot_ode(A)\n",
"# plot_ode(-A)\n",
"\n",
"p.finalise()\n",
"\n",
"IPython.display.SVG(filename=p_svg.file_name)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.15 64-bit",
"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.9.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "1dc4d77b1edb83bef89f833b7ed5251134c6a4899ef5e2c90c44e9927b4ae63a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}