.. 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 Click :ref:`here ` 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 6-18 In this example we are going to build a local approximation of a model using the Taylor decomposition: Here is the decomposition at the first order: .. math:: \vect{y} \, \approx \, \widehat{h}(\vect{x}) \, = \, h(\vect{x}_0) \, + \, \sum_{i=1}^{n_{X}} \; \frac{\partial h}{\partial x_i}(\vect{x}_0).\left(x_i - x_{0,i} \right) Here :math:`h(x) = [cos(x_1 + x_2), (x_2 + 1) e^{x_1 - 2 x_2}]`. .. GENERATED FROM PYTHON SOURCE LINES 20-36 .. code-block:: default import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # Prepare some data. formulas = ['cos(x1 + x2)', '(x2 + 1) * exp(x1 - 2 * x2)'] model = ot.SymbolicFunction(['x1', 'x2'], formulas) # Center of the approximation. x0 = [-0.4, -0.4] # Drawing bounds. a = -0.4 b = 0.0 .. GENERATED FROM PYTHON SOURCE LINES 37-38 Create a linear (first-order) Taylor approximation. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default algo = ot.LinearTaylor(x0, model) algo.run() responseSurface = algo.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 43-44 Plot the second output of our model with :math:`x_1=x_{0,1}`. .. GENERATED FROM PYTHON SOURCE LINES 44-55 .. code-block:: default graph = ot.ParametricFunction( responseSurface, [0], [x0[1]]).getMarginal(1).draw(a, b) graph.setLegends(['taylor']) curve = ot.ParametricFunction(model, [0], [x0[1]]).getMarginal( 1).draw(a, b).getDrawable(0) curve.setColor('red') curve.setLegend('model') graph.add(curve) graph.setLegendPosition('topright') 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 56-63 Here is the decomposition at the second order: .. math::\vect{y} \, \approx \, \widehat{h}(\vect{x}) \, = \, h(\vect{x}_0) \, + \, \sum_{i=1}^{n_{X}} \; \frac{\partial h}{\partial x_i}(\vect{x}_0).\left(x_i - x_{0,i} \right) \, + \, \frac{1}{2} \; \sum_{i,j=1}^{n_X} \; \frac{\partial^2 h}{\partial x_i \partial x_j}(\vect{x}_0).\left(x_i - x_{0,i} \right).\left(x_j - x_{0,j} \right):math:`` .. GENERATED FROM PYTHON SOURCE LINES 65-66 Create a quadratic (second-order) Taylor approximation. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default algo = ot.QuadraticTaylor(x0, model) algo.run() responseSurface = algo.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 71-72 Plot second output of our model with :math:`x_1=x_{0,1}`. .. GENERATED FROM PYTHON SOURCE LINES 72-83 .. code-block:: default graph = ot.ParametricFunction( responseSurface, [0], [x0[1]]).getMarginal(1).draw(a, b) graph.setLegends(['taylor']) curve = ot.ParametricFunction(model, [0], [x0[1]]).getMarginal( 1).draw(a, b).getDrawable(0) curve.setColor('red') curve.setLegend('model') graph.add(curve) graph.setLegendPosition('topright') view = viewer.View(graph) plt.show() .. 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.143 seconds) .. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_taylor_approximation.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_taylor_approximation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_taylor_approximation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_