.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/vectorial_functions/plot_symbolic_function.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_vectorial_functions_plot_symbolic_function.py: Create a symbolic function ========================== .. GENERATED FROM PYTHON SOURCE LINES 6-12 In this example we are going to create a function from mathematical formulas: .. math:: f(x_1, x_2) = -(6 + x_0^2 - x_1) Analytical expressions of the gradient and hessian are automatically computed except if the function is not differentiable everywhere. In that case a finite difference method is used. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python 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 21-22 create a symbolic function .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python function = ot.SymbolicFunction(["x0", "x1"], ["-(6 + x0^2 - x1)"]) print(function) .. rst-class:: sphx-glr-script-out .. code-block:: none [x0,x1]->[-(6 + x0^2 - x1)] .. GENERATED FROM PYTHON SOURCE LINES 26-27 evaluate function .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python x = [2.0, 3.0] print("x=", x, "f(x)=", function(x)) .. rst-class:: sphx-glr-script-out .. code-block:: none x= [2.0, 3.0] f(x)= [-7] .. GENERATED FROM PYTHON SOURCE LINES 31-32 show gradient .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python print(function.getGradient()) .. rst-class:: sphx-glr-script-out .. code-block:: none | d(y0) / d(x0) = -2*x0 | d(y0) / d(x1) = 1 .. GENERATED FROM PYTHON SOURCE LINES 35-36 use gradient .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python print("x=", x, "df(x)=", function.gradient(x)) .. rst-class:: sphx-glr-script-out .. code-block:: none x= [2.0, 3.0] df(x)= [[ -4 ] [ 1 ]] .. GENERATED FROM PYTHON SOURCE LINES 39-40 draw isocontours of f around [2,3] .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python graph = function.draw(0, 1, 0, [2.0, 3.0], [1.5, 2.5], [2.5, 3.5]) view = viewer.View(graph) plt.show() .. image-sg:: /auto_functional_modeling/vectorial_functions/images/sphx_glr_plot_symbolic_function_001.png :alt: y0 as a function of (x0,x1) :srcset: /auto_functional_modeling/vectorial_functions/images/sphx_glr_plot_symbolic_function_001.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_functional_modeling_vectorial_functions_plot_symbolic_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_symbolic_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_symbolic_function.py `