.. 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_04_coastline_paradox.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_04_coastline_paradox.py: How long is the coast of Britain? The divider method ========================================================== Walks a pair of dividers along a smooth circle and along the Koch curve with ever smaller openings. The circle's measured length converges, but the Koch curve's grows as a power of the ruler, the scaling Richardson observed for real coastlines and Mandelbrot interpreted as a fractal dimension. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import divider_length, koch_curve .. GENERATED FROM PYTHON SOURCE LINES 19-21 Measured length against ruler size ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-40 .. code-block:: Python t = np.linspace(0, 2 * np.pi, 20001) circle = np.column_stack([np.cos(t), np.sin(t)]) / (2 * np.pi) # circumference 1 koch = koch_curve(8) rulers = 3.0 ** -np.arange(1, 7) circle_lengths = [divider_length(circle, r) for r in rulers] koch_lengths = [divider_length(koch, r) for r in rulers] slope = np.polyfit(np.log(rulers), np.log(koch_lengths), 1)[0] print(f"Koch curve: L ~ ruler^{slope:.4f}, so D = 1 - slope = {1 - slope:.4f} (log 4 / log 3 = {np.log(4) / np.log(3):.4f})") fig, ax = plt.subplots() ax.loglog(rulers, circle_lengths, "o-", label="circle (smooth)") ax.loglog(rulers, koch_lengths, "s-", label="Koch curve (fractal)") ax.set_xlabel("ruler length") ax.set_ylabel("measured length") ax.invert_xaxis() ax.legend() ax.set_title("Richardson plot") .. image-sg:: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_04_coastline_paradox_001.png :alt: Richardson plot :srcset: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_04_coastline_paradox_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Koch curve: L ~ ruler^-0.2619, so D = 1 - slope = 1.2619 (log 4 / log 3 = 1.2619) Text(0.5, 1.0, 'Richardson plot') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.773 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_04_coastline_paradox.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_coastline_paradox.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_coastline_paradox.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_coastline_paradox.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_