.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/field_functions/plot_logistic_growth_model.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_auto_functional_modeling_field_functions_plot_logistic_growth_model.py: Logistic growth model ===================== .. GENERATED FROM PYTHON SOURCE LINES 6-9 In this example, we use the :ref:`logistic growth model ` in order to show how to define a function which has a vector input and a field output. We use the `OpenTURNSPythonPointToFieldFunction` class to define the derived class and its methods. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python from openturns.usecases import logistic_model import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 25-26 We load the logistic model from the usecases module : .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python lm = logistic_model.LogisticModel() .. GENERATED FROM PYTHON SOURCE LINES 30-31 We get the data from the LogisticModel data class (22 dates with population) : .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python ustime = lm.data.getMarginal(0) uspop = lm.data.getMarginal(1) .. GENERATED FROM PYTHON SOURCE LINES 36-37 We get the input parameters distribution distX : .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python distX = lm.distX .. GENERATED FROM PYTHON SOURCE LINES 41-42 We define the model : .. GENERATED FROM PYTHON SOURCE LINES 45-69 .. code-block:: Python class Popu(ot.OpenTURNSPythonPointToFieldFunction): def __init__(self, t0=1790.0, tfinal=2000.0, nt=1000): grid = ot.RegularGrid(t0, (tfinal - t0) / (nt - 1), nt) super(Popu, self).__init__(3, grid, 1) self.setInputDescription(["y0", "a", "b"]) self.setOutputDescription(["N"]) self.ticks_ = [t[0] for t in grid.getVertices()] self.phi_ = ot.SymbolicFunction(["t", "y", "a", "b"], ["a*y - b*y^2"]) def _exec(self, X): y0 = X[0] a = X[1] b = X[2] phi_ab = ot.ParametricFunction(self.phi_, [2, 3], [a, b]) phi_t = ot.ParametricFunction(phi_ab, [0], [0.0]) solver = ot.RungeKutta(phi_t) initialState = [y0] values = solver.solve(initialState, self.ticks_) return values * [1.0e-6] F = Popu(1790.0, 2000.0, 1000) popu = ot.PointToFieldFunction(F) .. GENERATED FROM PYTHON SOURCE LINES 70-72 Generate a sample from the model -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 74-75 Sample from the model .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: Python size = 10 inputSample = distX.getSample(size) outputSample = popu(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("Drawable-DefaultPalettePhase", size) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Draw some curves .. GENERATED FROM PYTHON SOURCE LINES 88-100 .. code-block:: Python graph = outputSample.drawMarginal(0) graph.setTitle("US population") graph.setXTitle(r"$t$ (years)") graph.setYTitle(r"$N$ (millions)") cloud = ot.Cloud(ustime, uspop) cloud.setPointStyle("circle") cloud.setLegend("Data") graph.add(cloud) graph.setLegendPosition("upper left") view = viewer.View(graph) plt.show() .. image-sg:: /auto_functional_modeling/field_functions/images/sphx_glr_plot_logistic_growth_model_001.png :alt: US population :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_logistic_growth_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-102 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 102-103 .. code-block:: Python ot.ResourceMap.Reload() .. _sphx_glr_download_auto_functional_modeling_field_functions_plot_logistic_growth_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_logistic_growth_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_logistic_growth_model.py `