{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Animation d'un onde le long d'une corde\n", "\n", "Ce notebook est long à charger, patientez :)\n" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ":download:`Télécharger le pdf <./animation_onde_corde_long_a_charger.pdf>`\n", "\n", ":download:`Télécharger le notebook <./animation_onde_corde_long_a_charger.ipynb>`\n", "\n", ":download:`Lancer le notebook sur binder (lent) `" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from matplotlib import animation, rc\n", "\n", "# Fonction permettant l'animation d'une onde\n", "# d'amplitude Ymax (en m), de période T (en secondes)\n", "# et de longueur d'onde lamb (en m). L'affichage sur x\n", "# se fait entre 0 et xmax (xmax = 3 longueurs d'onde\n", "# si le paramètre n'est pas fourni)\n", "def onde_corde(Ymax, T, lamb, xmax=None):\n", " print(\"Calcul de l'animation en cours, \"\n", " \"merci de patienter...\")\n", " xmin=0\n", " if xmax is None:\n", " xmax=3*lamb\n", " nbx=100\n", "\n", " fig=plt.figure(figsize=(12,10))\n", " line = plt.plot([], [],'bo-')\n", " plt.xlim(xmin,xmax)\n", " plt.ylim(-Ymax,Ymax)\n", " plt.grid()\n", " plt.xlabel(\"x(m)\")\n", " plt.ylabel(\"y(m)\")\n", " plt.title(\"animation : propagation d'une \"\n", " \"onde le long d'une corde\")\n", "\n", " def init():\n", " line[0].set_data([], [])\n", " return (line)\n", "\n", " def animate(i):\n", " dt=0.03 \n", " t=i*dt\n", " x = np.linspace(xmin, xmax, nbx)\n", " y = Ymax*np.cos(2 * np.pi * (x/lamb - t/T))\n", " line[0].set_data(x, y)\n", " return (line)\n", "\n", " anim = animation.FuncAnimation(\n", " fig,\n", " animate,\n", " init_func=init,frames=50,\n", " interval=30,\n", " blit=True,\n", " repeat=False)\n", "\n", " plt.close()\n", "\n", " # lignes de code à remplacer par plt.show() \n", " # sur un éditeur python (spyder...)\n", " rc('animation', html='jshtml') \n", " print(\"Calcul terminé, affichage en cours de téléchargement...\")\n", " return anim" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calcul de l'animation en cours, merci de patienter...\n", "Calcul terminé, affichage en cours de téléchargement...\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Once \n", " Loop \n", " Reflect \n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Utilisation, animation d'une onde d'amplitude\n", "# Ymax=0,2m, de période T=1s et de longueur d'onde\n", "# lamb=0,4m, pour un affichage jusqu'à xmax=2m\n", "# Il faut être patient, c'est un peu long à s'afficher\n", "onde_corde(Ymax=0.2, T=1, lamb=0.4, xmax=2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calcul de l'animation en cours, merci de patienter...\n", "Calcul terminé, affichage en cours de téléchargement...\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Once \n", " Loop \n", " Reflect \n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Utilisation, animation d'une onde d'amplitude\n", "# Ymax=0,2m, de période T=1s et de longueur d'onde\n", "# lamb=0,9m, pour un affichage jusqu'à xmax=2m\n", "# Il faut être patient, c'est un peu long à s'afficher\n", "onde_corde(Ymax=0.2, T=1, lamb=0.9, xmax=2)" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }