
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/02-production/2.5-plot_random_size.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_02-production_2.5-plot_random_size.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_02-production_2.5-plot_random_size.py:


Plot a distribution of random sizes
===================================

.. rubric:: By David Minton

This example demonstrates the generation of random impact sizes, which is used to sample production function SFDs. In this example, we just draw from a simple power law.

.. GENERATED FROM PYTHON SOURCE LINES 10-52



.. image-sg:: /auto_examples/02-production/images/sphx_glr_2.5-plot_random_size_001.png
   :alt: 2.5 plot random size
   :srcset: /auto_examples/02-production/images/sphx_glr_2.5-plot_random_size_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from cratermaker.utils.montecarlo_utils import get_random_size

    num_realizations = 100
    nbins = 10
    size = 100000
    Dhi = 100.0
    p = 2.0
    C = Dhi**p
    Dlo = (size / C) ** (-1.0 / p)
    diameters = np.exp(np.linspace(np.log(Dlo), np.log(100 * Dhi), nbins))
    # Test a simple power law SFD
    cdf = C * diameters**-p

    fig = plt.figure(figsize=(4, 3))
    ax = fig.add_subplot(1, 1, 1)

    ax.set_xlabel("$D$")
    ax.set_ylabel("$N_{>D}$")
    ax.set_xscale("log")
    ax.set_yscale("log")
    ax.set_ylim([1.0, 1.1 * size])

    for i in range(num_realizations):
        new_diameters = get_random_size(diameters, cdf, mu=size)
        new_diameters = np.sort(new_diameters)[::-1]
        nval = np.arange(1, new_diameters.size + 1)
        if i == 0:
            label = "Sampled SFD"
        else:
            label = None

        ax.plot(new_diameters, nval, label=label, alpha=0.25, color="cornflowerblue")

    ax.plot(diameters, cdf, label="Model SFD", color="black")
    ax.legend(loc="upper right")

    plt.tight_layout()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 3.810 seconds)


.. _sphx_glr_download_auto_examples_02-production_2.5-plot_random_size.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 2.5-plot_random_size.ipynb <2.5-plot_random_size.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 2.5-plot_random_size.py <2.5-plot_random_size.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 2.5-plot_random_size.zip <2.5-plot_random_size.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
