.. 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 Click :ref:`here ` 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-7 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 11-13 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: default from __future__ import print_function from openturns.usecases import logistic_model as logistic_model import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt from numpy import linspace, exp, maximum ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 24-25 We load the logistic model from the usecases module : .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: default lm = logistic_model.LogisticModel() .. GENERATED FROM PYTHON SOURCE LINES 29-30 We get the data from the LogisticModel data class (22 dates with population) : .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: default ustime = lm.data.getMarginal(0) uspop = lm.data.getMarginal(1) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We get the input parameters distribution distX : .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: default distX = lm.distX .. GENERATED FROM PYTHON SOURCE LINES 40-41 We define the model : .. GENERATED FROM PYTHON SOURCE LINES 43-68 .. code-block:: default 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 69-71 Generate a sample from the model -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 73-74 Sample from the model .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default size = 10 inputSample = distX.getSample(size) outputSample = popu(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: default ot.ResourceMap.SetAsUnsignedInteger('Drawable-DefaultPalettePhase', size) .. GENERATED FROM PYTHON SOURCE LINES 84-85 Draw some curves .. GENERATED FROM PYTHON SOURCE LINES 87-98 .. code-block:: default 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('topleft') 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.260 seconds) .. _sphx_glr_download_auto_functional_modeling_field_functions_plot_logistic_growth_model.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_logistic_growth_model.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_logistic_growth_model.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_