.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/wave/plot_02_wave_animation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_pde_wave_plot_02_wave_animation.py: Animated: a plucked string splitting, reflecting, and recombining ================================================================= D'Alembert's solution :math:`u = F(x - ct) + G(x + ct)` says a plucked string's bump splits into two half-height waves running in opposite directions. At the fixed ends each reflects upside down; after one full period :math:`2L/c` the string is back where it started. The leapfrog solve (solid) runs at Courant number 1, where it reproduces d'Alembert's formula (dashed) exactly at the grid points. .. GENERATED FROM PYTHON SOURCE LINES 14-32 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import WaveEquation1D, dalembert_solution from mathematicskit.pde.visualizers import animate_solution # In Jupyter or JupyterLite, show animations as an HTML/JavaScript player # (no ffmpeg needed). plt.rcParams["animation.html"] = "jshtml" def pluck(x): return np.maximum(0.0, 1.0 - np.abs(x - 0.3) / 0.1) string = WaveEquation1D(pluck, n=201, c=1.0) sol = string.solve(2.0, dt=string.dx, method="leapfrog") .. GENERATED FROM PYTHON SOURCE LINES 33-35 One full period of the string ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python anim = animate_solution(sol, exact=lambda x, t: dalembert_solution(pluck, x, t, length=1.0), max_frames=81) anim .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 40-41 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.671 seconds) .. _sphx_glr_download_api_gallery_pde_wave_plot_02_wave_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/pde/wave/plot_02_wave_animation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_wave_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_wave_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_wave_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_