# Simple nonlinear complementarity problem¶

The problem is to solve

$$f(x, y) = \begin{bmatrix}1+xy -2x^3-x\\ 2x^2-y\end{bmatrix}$$

subject to $0 \leq x, y \leq 1$

In [2]:
from compecon import MCP, jacobian
import numpy as np

x0 = [0.5, 0.5]


### Solving the problem without the Jacobian¶

To solve this problem we create a MCP object using a lambda function.

In [3]:
def func(z):
x, y = z
return np.array([1 + x*y - 2*x**3 - x, 2*x**2 - y])

F = MCP(func, [0, 0], [1,1])


Solve for initial guess $x_0 = [0.5, 0.5]$

In [4]:
x = F.zero(x0, transform='minmax', print=True)

Using the MINMAX transformation
Solving nonlinear equations by Broyden's method
it    bstep  change
--------------------
0     1  1.56e-01
1     1  1.14e-01
2     1  1.01e-01
3     0  3.66e-02
4     0  1.33e-03
5     0  1.86e-05
6     0  2.02e-07
7     0  1.13e-09

In [ ]:
# x = F.zero(x0, transform='ssmooth', print=True)
# printx(x)
# FIXME: generates error


### Solving the problem with the Jacobian¶

In [5]:
def func2(z):
x, y = z
f = [1 + x*y - 2*x**3 - x, 2*x**2 - y]
J = [[y-6*x**2-1, x],[4*x, -1]]
return np.array(f), np.array(J)

F2 = MCP(func2, [0, 0], [1,1])

In [6]:
x = F2.zero(x0, transform='minmax', print=True)
printx(x)

Using the MINMAX transformation
Solving nonlinear equations by Newton's method
it    bstep  change
--------------------
0     1  1.56e-01
1     0  9.84e-03
2     0  3.19e-05
3     0  3.40e-10

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-f444775cee1a> in <module>()
1 x = F2.zero(x0, transform='minmax', print=True)
----> 2 printx(x)

NameError: name 'printx' is not defined
In [7]:
print('Solution is x = ', x)

Solution is x =  [ 0.7937  1.    ]