Created
May 4, 2017 16:10
-
-
Save odarbelaeze/234f96edfed9032b417829b2c1a5ed8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"\"\"\"\n", | |
"=========================\n", | |
"Simple animation examples\n", | |
"=========================\n", | |
"\n", | |
"This example contains two animations. The first is a random walk plot. The\n", | |
"second is an image animation.\n", | |
"\"\"\"\n", | |
"\n", | |
"%matplotlib notebook\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.animation as animation\n", | |
"\n", | |
"\n", | |
"def update_line(num, data, line):\n", | |
" line.set_data(data[..., :num])\n", | |
" return line,\n", | |
"\n", | |
"fig1 = plt.figure()\n", | |
"\n", | |
"data = np.random.rand(2, 25)\n", | |
"l, = plt.plot([], [], 'r-')\n", | |
"plt.xlim(0, 1)\n", | |
"plt.ylim(0, 1)\n", | |
"plt.xlabel('x')\n", | |
"plt.title('test')\n", | |
"line_ani = animation.FuncAnimation(fig1, update_line, 100, fargs=(data, l),\n", | |
" interval=50, blit=True)\n", | |
"\n", | |
"# To save the animation, use the command: line_ani.save('lines.mp4')\n", | |
"\n", | |
"fig2 = plt.figure()\n", | |
"\n", | |
"x = np.arange(-9, 10)\n", | |
"y = np.arange(-9, 10).reshape(-1, 1)\n", | |
"base = np.hypot(x, y)\n", | |
"ims = []\n", | |
"for add in np.arange(15):\n", | |
" ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))\n", | |
"\n", | |
"im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,\n", | |
" blit=True)\n", | |
"# To save this second animation with some metadata, use the following command:\n", | |
"# im_ani.save('im.mp4', metadata={'artist':'Guido'})\n", | |
"\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib notebook\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.animation as animation\n", | |
"\n", | |
"def update_line(num, line1, line2):\n", | |
" x = np.linspace(0, 10 * np.pi, 1000)\n", | |
" y1 = np.cos(x - 0.1 * num)\n", | |
" y2 = np.cos(x + 0.1 * num)\n", | |
" line1.set_data(x, y1)\n", | |
" line2.set_data(x, y2)\n", | |
" return line1, line2,\n", | |
"\n", | |
"fig1 = plt.figure()\n", | |
"\n", | |
"line1, line2 = plt.plot([], [], 'r-', [], [], 'b-')\n", | |
"plt.xlim(0, 10 * np.pi)\n", | |
"plt.ylim(-1.1, 1.1)\n", | |
"plt.xlabel('x')\n", | |
"plt.title('test')\n", | |
"plt.grid()\n", | |
"\n", | |
"line_ani = animation.FuncAnimation(\n", | |
" fig1, update_line, 1000, fargs=(line1, line2),\n", | |
" interval=10 #, blit=True\n", | |
")\n", | |
"\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"M1 = 1\n", | |
"M2 = 1\n", | |
"K1 = 50\n", | |
"K2 = 50\n", | |
"RO1 = 1\n", | |
"RO2 = 1\n", | |
"G = 9.8" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def aceleracion(R):\n", | |
" r1, r2 = R\n", | |
" f1 = (\n", | |
" - M1 * G\n", | |
" - K1 * (np.abs(r1) - RO1) * r1 / np.abs(r1)\n", | |
" + K2 * (np.abs(r2 - r1) - RO2) * (r2 - r1) / np.abs(r2 - r1)\n", | |
" )\n", | |
" f2 = (\n", | |
" - M2 * G\n", | |
" - K2 * (np.abs(r2 - r1) - RO2) * (r2 - r1) / np.abs(r2 - r1)\n", | |
" )\n", | |
" return np.array([f1/M1, f2/M1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def primer_paso(R, V, delta_t):\n", | |
" R = np.array(R)\n", | |
" V = np.array(V)\n", | |
" return R + V * delta_t + aceleracion(R) * delta_t ** 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def otros_pasos(R, R_anterior, delta_t):\n", | |
" R = np.array(R)\n", | |
" R_anterior = np.array(R_anterior)\n", | |
" return 2 * R - R_anterior + aceleracion(R) * delta_t ** 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def integracion(R, V, delta_t, t_max):\n", | |
" R_anterior = np.array(R)\n", | |
" R = primer_paso(R, V, delta_t)\n", | |
" tiempo = delta_t\n", | |
" ts = [0]\n", | |
" r1, r2 = R_anterior\n", | |
" r1s = [r1]\n", | |
" r2s = [r2]\n", | |
" while tiempo < t_max:\n", | |
" r1, r2 = R\n", | |
" ts.append(tiempo)\n", | |
" r1s.append(r1)\n", | |
" r2s.append(r2)\n", | |
" temporal = R\n", | |
" R = otros_pasos(R, R_anterior, delta_t)\n", | |
" R_anterior = temporal\n", | |
" tiempo = tiempo + delta_t\n", | |
" return np.array(ts), np.array(r1s), np.array(r2s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"t, r1, r2 = integracion((-1, -2.5), (0, 0), 1e-4, 10)\n", | |
"plt.figure()\n", | |
"plt.plot(t, r1, t, r2)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib notebook\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.animation as animation\n", | |
"\n", | |
"t, r1, r2 = integracion((-1, -1.5), (0, 0), 1e-4, 10)\n", | |
"\n", | |
"def update_line(num, line1, line2, t, r1, r2):\n", | |
" x = t[:num]\n", | |
" y1 = r1[:num]\n", | |
" y2 = r2[:num]\n", | |
" line1.set_data(x, y1)\n", | |
" line2.set_data(x, y2)\n", | |
" return line1, line2,\n", | |
"\n", | |
"fig1 = plt.figure()\n", | |
"\n", | |
"line1, line2 = plt.plot([], [], 'r-', [], [], 'b-')\n", | |
"plt.xlim(0, 10)\n", | |
"plt.ylim(-4, 0)\n", | |
"plt.xlabel('x')\n", | |
"plt.title('test')\n", | |
"plt.grid()\n", | |
"\n", | |
"line_ani = animation.FuncAnimation(\n", | |
" fig1, update_line, range(0, 100000, 100),\n", | |
" fargs=(line1, line2, t, r1, r2),\n", | |
" interval=10, blit=True\n", | |
")\n", | |
"\n", | |
"# line_ani.save('animation.mp4', metadata={'author': 'Oscar'})\n", | |
"\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import serial\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"!ls /dev/tty.*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"s = serial.Serial('/dev/tty.usbmodem1421')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"s.readline().decode('ascii')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def voltaje(s):\n", | |
" while True:\n", | |
" string = s.readline().decode('ascii')\n", | |
" voltaje = float(string.strip()[2:-2])\n", | |
" yield voltaje" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"a = [1, 2, 3]\n", | |
"a[-4:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib notebook\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.animation as animation\n", | |
"import serial\n", | |
"\n", | |
"s = serial.Serial('/dev/tty.usbmodem1421')\n", | |
"\n", | |
"def voltaje(s):\n", | |
" while True:\n", | |
" try:\n", | |
" string = s.readline().decode('ascii')\n", | |
" voltaje = float(string.strip()[2:-2])\n", | |
" yield voltaje\n", | |
" except:\n", | |
" pass\n", | |
"\n", | |
"def update_voltaje(num, line, buffer, voltajes):\n", | |
" buffer.append(next(voltajes))\n", | |
" buffer = buffer[-100:]\n", | |
" line.set_data(range(len(buffer)), buffer)\n", | |
" return line,\n", | |
"\n", | |
"buffer = []\n", | |
"voltajes = voltaje(s)\n", | |
"\n", | |
"fig = plt.figure()\n", | |
"line, = plt.plot([], [], 'r-',)\n", | |
"\n", | |
"plt.xlim(0, 100)\n", | |
"plt.ylim(0, 4)\n", | |
"plt.xlabel('x')\n", | |
"plt.title('test')\n", | |
"plt.grid()\n", | |
"\n", | |
"line_ani = animation.FuncAnimation(\n", | |
" fig, update_voltaje, 100000,\n", | |
" fargs=(line, buffer, voltajes),\n", | |
" interval=10, blit=True\n", | |
")\n", | |
"\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"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.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment