1#! /usr/bin/env python
2
3from __future__ import print_function
4import openturns as ot
5
6ot.TESTPREAMBLE()
7ot.PlatformInfo.SetNumericalPrecision(3)
8
9# linear
10levelFunction = ot.SymbolicFunction(
11    ["x1", "x2", "x3", "x4"], ["x1+2*x2-3*x3+4*x4"])
12# Add a finite difference gradient to the function
13myGradient = ot.NonCenteredFiniteDifferenceGradient(
14    1e-7, levelFunction.getEvaluation())
15print("myGradient = ", repr(myGradient))
16# Substitute the gradient
17levelFunction.setGradient(
18    ot.NonCenteredFiniteDifferenceGradient(myGradient))
19startingPoint = ot.Point(4, 0.0)
20algo = ot.SQP(ot.NearestPointProblem(levelFunction, 3.0))
21algo.setStartingPoint(startingPoint)
22print('algo=', algo)
23algo.run()
24result = algo.getResult()
25print('result=', result)
26
27
28# non-linear
29levelFunction = ot.SymbolicFunction(
30    ["x1", "x2", "x3", "x4"], ["x1*cos(x1)+2*x2*x3-3*x3+4*x3*x4"])
31# Add a finite difference gradient to the function,
32# needs it
33myGradient = ot.NonCenteredFiniteDifferenceGradient(
34    1e-7, levelFunction.getEvaluation())
35# Substitute the gradient
36levelFunction.setGradient(
37    ot.NonCenteredFiniteDifferenceGradient(myGradient))
38startingPoint = ot.Point(4, 0.0)
39algo = ot.SQP(ot.NearestPointProblem(levelFunction, -0.5))
40algo.setStartingPoint(startingPoint)
41print('algo=', algo)
42algo.run()
43result = algo.getResult()
44print('result=', result)
45