.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/curves/plot_01_weierstrass_function.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_fractals_chaos_curves_plot_01_weierstrass_function.py: The Weierstrass function: continuous, nowhere differentiable ================================================================== Plots the Weierstrass function sum a^n cos(b^n pi x) at several zoom levels. The graph looks equally rough at every scale, difference quotients grow without bound as the step shrinks, and the graph's measured length keeps growing as it is sampled more finely. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import weierstrass_function .. GENERATED FROM PYTHON SOURCE LINES 18-20 Zooming in never smooths the graph ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(12, 3.5)) for ax, width in zip(axes, (2.0, 0.2, 0.02)): x = np.linspace(0.3 - width / 2, 0.3 + width / 2, 4000) ax.plot(x, weierstrass_function(x), lw=0.7) ax.set_title(f"window width {width}") fig.suptitle("W(x) with a = 0.5, b = 7") .. image-sg:: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_01_weierstrass_function_001.png :alt: W(x) with a = 0.5, b = 7, window width 2.0, window width 0.2, window width 0.02 :srcset: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_01_weierstrass_function_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 0.98, 'W(x) with a = 0.5, b = 7') .. GENERATED FROM PYTHON SOURCE LINES 29-31 Difference quotients diverge ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python for h in (1e-1, 1e-2, 1e-3, 1e-4, 1e-5): slope = (weierstrass_function(0.3 + h) - weierstrass_function(0.3)) / h print(f"h = {h:.0e}: difference quotient {float(slope):12.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none h = 1e-01: difference quotient -9.78 h = 1e-02: difference quotient 5.53 h = 1e-03: difference quotient -119.59 h = 1e-04: difference quotient -504.59 h = 1e-05: difference quotient 1190.24 .. GENERATED FROM PYTHON SOURCE LINES 37-39 The graph has infinite length ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: Python for n in (10**3, 10**4, 10**5, 10**6): x = np.linspace(0, 1, n) y = weierstrass_function(x) length = np.sum(np.hypot(np.diff(x), np.diff(y))) print(f"{n:>8d} samples: polygonal length of the graph over [0, 1] = {length:8.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none 1000 samples: polygonal length of the graph over [0, 1] = 96.8 10000 samples: polygonal length of the graph over [0, 1] = 343.7 100000 samples: polygonal length of the graph over [0, 1] = 2101.0 1000000 samples: polygonal length of the graph over [0, 1] = 10109.4 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.236 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_01_weierstrass_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_weierstrass_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_weierstrass_function.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_weierstrass_function.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_