# Create a symbolic function¶

In this basic example we are going to create a function from mathematical formulas:

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.

[30]:

from __future__ import print_function
import openturns as ot
import math as m

[31]:

# create a symbolic function
function = ot.SymbolicFunction(['x0', 'x1'],
['-(6 + x0^2 - x1)'])
print(function)

[x0,x1]->[-(6 + x0^2 - x1)]

[32]:

# evaluate function
x = [2.0, 3.0]
print('x=', x, 'f(x)=', function(x))

x= [2.0, 3.0] f(x)= [-7]

[33]:

# show gradient


| d(y0) / d(x0) = -2*x0
| d(y0) / d(x1) = 1


[34]:

# use gradient

x= [2.0, 3.0] df(x)= [[ -4 ]

[45]:

# draw isocontours of f around [2,3]

[45]: