.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/general_purpose_metamodels/plot_taylor_approximation.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_meta_modeling_general_purpose_metamodels_plot_taylor_approximation.py: Taylor approximations ===================== .. GENERATED FROM PYTHON SOURCE LINES 7-26 In this example we build a local approximation of a model using the Taylor decomposition with the :class:`~openturns.LinearTaylor` class. We consider the function :math:`\vect{h} : \Rset^2 \rightarrow \Rset^2` defined by: .. math:: \vect{h}(\vect{x}) = \Tr{\left( \cos(x_1 + x_2), (x_2 + 1) e^{x_1 - 2 x_2} \right)}. for any :math:`\vect{x} \in \Rset^2`. The metamodel :math:`\widehat{\vect{h}}` is an approximation of the model :math:`\vect{h}`: .. math:: \vect{y} \approx \widehat{\vect{h}}(\vect{x}) for any :math:`\vect{x} \in \Rset^2`. We note :math:`h_k`, :math:`k=1, 2` the marginal outputs In this example, we consider two different approximations: - the first order Taylor expansion, - the second order Taylor expansion. .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python import openturns as ot import openturns.viewer as viewer ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 34-36 Define the model ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 38-39 Prepare some data. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python formulas = ["cos(x1 + x2)", "(x2 + 1) * exp(x1 - 2 * x2)"] model = ot.SymbolicFunction(["x1", "x2"], formulas) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Center of the approximation. .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python x0 = [-0.4, -0.4] .. GENERATED FROM PYTHON SOURCE LINES 47-48 Drawing bounds. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python a = -0.4 b = 0.0 .. GENERATED FROM PYTHON SOURCE LINES 52-54 First order Taylor expansion ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 56-65 Let :math:`\vect{x}_0 \in \Rset^2` be a reference point where the linear approximation is evaluated. The first order Taylor expansion of the output marginal :math:`h_k` is: .. math:: \widehat{h_k}(\vect{x}) = h_k(\vect{x}_0) + \sum_{i=1}^{2} \frac{\partial h_k}{\partial x_i}(\vect{x}_0) \left(x_i - x_{0,i} \right) for any :math:`\vect{x} \in \Rset^2`. .. GENERATED FROM PYTHON SOURCE LINES 68-69 Create a linear (first-order) Taylor approximation. .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. code-block:: Python algo = ot.LinearTaylor(x0, model) algo.run() responseSurface = algo.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 74-75 Plot the second output of our model with :math:`x_1=x_{0,1}`. .. GENERATED FROM PYTHON SOURCE LINES 77-91 .. code-block:: Python graph = ot.ParametricFunction(model, [0], [x0[1]]).getMarginal(1).draw(a, b) graph.setLegends(["Model"]) curve = ( ot.ParametricFunction(responseSurface, [0], [x0[1]]) .getMarginal(1) .draw(a, b) .getDrawable(0) ) curve.setLegend("Taylor") curve.setLineStyle("dashed") graph.add(curve) graph.setLegendPosition("upper right") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_taylor_approximation_001.png :alt: y1 as a function of x2 :srcset: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_taylor_approximation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-94 Second order Taylor expansion ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 96-108 Let :math:`\vect{x}_0 \in \Rset^2` be a reference point where the quadratic approximation is evaluated. The second order Taylor expansion of the output marginal :math:`h_k` is: .. math:: \widehat{h_k}(\vect{x}) = h_k(\vect{x}_0) + \sum_{i=1}^{2} \frac{\partial h_k}{\partial x_i}(\vect{x}_0) \left(x_i - x_{0,i} \right) + \frac{1}{2} \sum_{i,j=1}^{2} \frac{\partial^2 h_k}{\partial x_i \partial x_j}(\vect{x}_0) \left(x_i - x_{0,i} \right) \left(x_j - x_{0,j} \right) for any :math:`\vect{x} \in \Rset^2`. .. GENERATED FROM PYTHON SOURCE LINES 110-111 Create a quadratic (second-order) Taylor approximation. .. GENERATED FROM PYTHON SOURCE LINES 113-117 .. code-block:: Python algo = ot.QuadraticTaylor(x0, model) algo.run() responseSurface = algo.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 118-119 Plot second output of our model with :math:`x_1=x_{0,1}`. .. GENERATED FROM PYTHON SOURCE LINES 121-135 .. code-block:: Python graph = ot.ParametricFunction(model, [0], [x0[1]]).getMarginal(1).draw(a, b) graph.setLegends(["Model"]) curve = ( ot.ParametricFunction(responseSurface, [0], [x0[1]]) .getMarginal(1) .draw(a, b) .getDrawable(0) ) curve.setLegend("Taylor") curve.setLineStyle("dashed") graph.add(curve) graph.setLegendPosition("upper right") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_taylor_approximation_002.png :alt: y1 as a function of x2 :srcset: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_taylor_approximation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 136-137 .. code-block:: Python view.ShowAll() .. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_taylor_approximation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_taylor_approximation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_taylor_approximation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_taylor_approximation.zip `