### DEMSLV13
# Simple nonlinear complementarity problem
The problem is to solve 

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

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


```python
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.


```python
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]$


```python
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



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

### Solving the problem with the Jacobian


```python
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])
```


```python
x = F2.zero(x0, transform='minmax', print=True)
printx(x)
```


```python
print('Solution is x = ', x)
```

    Solution is x =  [ 0.7937  1.    ]


a076161ae6f66264cc8f56b5cb1a0375a29cf3f3
ipynb
Jupyter Notebook
notebooks/slv/13 Simple nonlinear complementarity problem.ipynb
daniel-schaefer/CompEcon-python
d3f66e04a7e02be648fc5a68065806ec7cc6ffd6
MIT
notebooks/slv/13 Simple nonlinear complementarity problem.ipynb
daniel-schaefer/CompEcon-python
d3f66e04a7e02be648fc5a68065806ec7cc6ffd6
MIT
notebooks/slv/13 Simple nonlinear complementarity problem.ipynb
daniel-schaefer/CompEcon-python
d3f66e04a7e02be648fc5a68065806ec7cc6ffd6
MIT
2021-06-01T03:47:35.000Z
2021-06-01T03:47:35.000Z
Qwen/Qwen-72B
1. YES
2. YES