{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1>Puissances en régime sinusoïdal (version animée)</h1>\n<p>Th. G © 2022, <a href=\"https://ensciences.fr\">ensciences.fr</a></p><br>"},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"border-left:5px solid #047200;\">\n    <p>Ce notebook a pour objectif d'illustrer la notion de <strong>déphasage entre courant et tension</strong> et son impact sur la puissance en régime sinusoïdal.\n       </p>\n</div>"},{"metadata":{},"cell_type":"markdown","source":"On commence par importer les librairies :"},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation","execution_count":1,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"On prépare ensuite la zone graphique pour l'animation : définition des courbes, limites des axes, titres..."},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"fig, (ax, ax1) = plt.subplots(2)\n\nl1, = ax.plot([], [], lw=2, label=\"Tension (en V)\", color=\"tab:blue\")\nl2, = ax.plot([], [], lw=2, label=\"Intensité (en A)\", color=\"tab:green\")\nl3, = ax1.plot([], [], lw=2, label=\"Puissance instantannée\", color=\"tab:red\")\nl4, = ax1.plot([], [], lw=2, label=\"Puissance active\", color=\"tab:orange\")\nl5, = ax1.plot([], [], lw=2, label=\"Puissance apparente\", color=\"tab:purple\")","execution_count":2,"outputs":[]},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"ax.set_xlim(0, 1)\nax.set_ylim(-6,6)\n\nax1.set_xlim(0, 1)\nax1.set_ylim(-6,6)\n\nax1.set_xlabel(\"Temps (en s)\")\nax1.set_ylabel(\"Puissance (W)\")\nax1.grid()\n\nax.set_xlabel(\"Temps (en s)\")\nax.set_ylabel(\"Amplitude\")\nax.grid()\n\nax.legend(loc='center left', bbox_to_anchor=(1.02, 0.5),\n          fancybox=True, shadow=True, ncol=1)\n\nax1.legend(loc='center left', bbox_to_anchor=(1.02, 0.5),\n          fancybox=True, shadow=True, ncol=1)","execution_count":9,"outputs":[{"data":{},"execution_count":9,"metadata":{},"output_type":"execute_result"}]},{"metadata":{},"cell_type":"markdown","source":"On initialise ensuite les valeurs pour chaque courbe :"},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"# Initialisation\ndef init():\n    l1.set_data([], [])\n    l2.set_data([], [])\n    l3.set_data([], [])\n    l4.set_data([], [])\n    l5.set_data([], [])\n    return (l1,l2,l3,l4,l5)","execution_count":6,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Puis on crée la fonction <code>animation</code> dont le rôle sera de calculer les valeurs associées à chaque courbe. Pour"},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"# Animation\ndef animate(frame):\n    t = np.arange(0.0, 1.0, 0.001)\n    u0 = 5\n    i0 = 1\n    f0 = 3\n    s1 = u0 * np.sin(2 * np.pi * f0 * t)\n    l1.set_data(t, s1)\n    \n    phi = frame*0.1\n    s2 = i0 * np.sin(2 * np.pi * f0 * t + phi)\n    l2.set_data(t, s2)\n    \n    s3=[s1[i]*s2[i] for i in range(len(s1))]\n    l3.set_data(t, s3)\n                                   \n    moyenne = np.mean(s3)\n    s4 = [moyenne] * len(s1)\n    l4.set_data(t, s4)\n    \n    moy_apparente = np.mean(u0 * np.sin(2 * np.pi * f0 * t) * i0 * np.sin(2 * np.pi * f0 * t))\n    s5 = [moy_apparente] * len(s1) \n    l5.set_data(t, s5)\n\n    return (l1,l2,l3,l4,l5)","execution_count":7,"outputs":[]},{"metadata":{"trusted":false,"vscode":{"languageId":"python"}},"cell_type":"code","source":"# Affichage de l'animation\nfig.tight_layout()\nanim = animation.FuncAnimation(fig, animate, init_func=init, interval=100)\nplt.show()\n","execution_count":8,"outputs":[{"data":{"application/javascript":"element.append(window._basthonDomNodeBus.pop(0));"},"metadata":{},"output_type":"display_data"}]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":2}