### DEMSLV04

# Compute fixedpoint of $f(x, y)= [x^2 + y^3; xy - 0.5]$

Compute fixedpoint of 

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

using Newton, Broyden, and function iteration methods.

Initial values generated randomly.  Some algorithms may fail to converge, depending on the initial value.

True fixedpoint is $x = -0.09$,  $y=-0.46$.


```python
from demos.setup import np, tic, toc
from compecon import NLP
np.random.seed(12)
```

### Set up the problem


```python
def g(z):
    x, y = z
    return np.array([x **2 + y ** 3, x * y - 0.5])

problem_as_fixpoint = NLP(g, maxit=1500)
```

### Equivalent Rootfinding Formulation


```python
def f(z):
    x, y = z
    fval = [x - x ** 2 - y ** 3,
            y - x * y + 0.5]
    fjac = [[1 - 2 * x, -3 * y **2],
            [-y, 1 - x]]

    return np.array(fval), np.array(fjac)

problem_as_zero = NLP(f, maxit=1500)
```

### Randomly generate starting point


```python
xinit = np.random.randn(2)
```

### Compute fixed-point using Newton method


```python
t0 = tic()
z1 = problem_as_zero.newton(xinit)
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm
```

### Compute fixed-point using Broyden method


```python
t0 = tic()
z2 = problem_as_zero.broyden(xinit)
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm
```

### Compute fixed-point using function iteration


```python
t0 = tic()
z3 = problem_as_fixpoint.fixpoint(xinit)
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - z3)
```




```python
print('Hundredths of seconds required to compute fixed-point of ')
print('\n\t\tg(x1,x2)=[x1^2+x2^3; x1*x2-0.5]')
print('\nusing Newton, Broyden, and function iteration methods, starting at')
print('\n\t\tx1 = {:4.2f}  x2 = {:4.2f}\n\n'.format(*xinit))
print('Method       Time   Norm of f         x1       x2\n', '-' * 48)
print('Newton   {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t1, n1, *z1))
print('Broyden  {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t2, n2, *z2))
print('Function {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t3, n3, *z3))
```

    Hundredths of seconds required to compute fixed-point of 
    
    		g(x1,x2)=[x1^2+x2^3; x1*x2-0.5]
    
    using Newton, Broyden, and function iteration methods, starting at
    
    		x1 = 0.47  x2 = -0.68
    
    
    Method       Time   Norm of f         x1       x2
     ------------------------------------------------
    Newton       0.80       2e-15     -0.09     -0.46
    Broyden      0.10       8e-10     -0.09     -0.46
    Function     0.05       8e-09     -0.09     -0.46


4e3b5263bc7c3f27bfe80a7159bd93dcd5565196
ipynb
Jupyter Notebook
notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb
daniel-schaefer/CompEcon-python
d3f66e04a7e02be648fc5a68065806ec7cc6ffd6
MIT
notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb
daniel-schaefer/CompEcon-python
d3f66e04a7e02be648fc5a68065806ec7cc6ffd6
MIT
notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].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