.. 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_vectorial_functions_plot_symbolic_function.py:
Create a symbolic function
==========================
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.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)
create a symbolic function
.. code-block:: default
function = ot.SymbolicFunction(['x0', 'x1'],
['-(6 + x0^2 - x1)'])
print(function)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[x0,x1]->[-(6 + x0^2 - x1)]
evaluate function
.. code-block:: default
x = [2.0, 3.0]
print('x=', x, 'f(x)=', function(x))
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
x= [2.0, 3.0] f(x)= [-7]
show gradient
.. code-block:: default
print(function.getGradient())
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
| d(y0) / d(x0) = -2*x0
| d(y0) / d(x1) = 1
use gradient
.. code-block:: default
print('x=', x, 'df(x)=', function.gradient(x))
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
x= [2.0, 3.0] df(x)= [[ -4 ]
[ 1 ]]
draw isocontours of f around [2,3]
.. code-block:: default
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:: /auto_functional_modeling/vectorial_functions/images/sphx_glr_plot_symbolic_function_001.png
:alt: y0 as a function of (x0,x1)
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.115 seconds)
.. _sphx_glr_download_auto_functional_modeling_vectorial_functions_plot_symbolic_function.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_symbolic_function.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_symbolic_function.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_