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

**Randall Romero Aguilar, PhD**

This demo is based on the original Matlab demo accompanying the  <a href="https://mitpress.mit.edu/books/applied-computational-economics-and-finance">Computational Economics and Finance</a> 2001 textbook by Mario Miranda and Paul Fackler.

Original (Matlab) CompEcon file: **demslv04.m**

Running this file requires the Python version of CompEcon. This can be installed with pip by running

    !pip install compecon --upgrade

<i>Last updated: 2021-Oct-01</i>
<hr>


## About

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
import numpy as np
import pandas as pd
from compecon import NLP, tic, toc
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))

pd.DataFrame({
    'Time': [t1, t2, t3],
    'Norm of f': [n1, n2, n3],
    'x1': [z1[0], z2[0], z3[0]],
    'x2': [z1[1], z2[1], z3[1]]},
    index=['Newton', 'Broyden', 'Function']
)
```

    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\n\n





<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Time</th>
      <th>Norm of f</th>
      <th>x1</th>
      <th>x2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Newton</th>
      <td>0.102687</td>
      <td>1.665335e-15</td>
      <td>-0.088909</td>
      <td>-0.459175</td>
    </tr>
    <tr>
      <th>Broyden</th>
      <td>0.598288</td>
      <td>8.015544e-10</td>
      <td>-0.088909</td>
      <td>-0.459175</td>
    </tr>
    <tr>
      <th>Function</th>
      <td>0.099659</td>
      <td>8.421821e-09</td>
      <td>-0.088909</td>
      <td>-0.459175</td>
    </tr>
  </tbody>
</table>
</div>



5377aaa63eeeb1a82a33ea98be9d0c749f1e0913
ipynb
Jupyter Notebook
_build/jupyter_execute/notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb
randall-romero/CompEcon-python
c7a75f57f8472c972fddcace8ff7b86fee049d29
MIT
2016-12-14T13:21:27.000Z
2020-08-23T21:04:34.000Z
_build/jupyter_execute/notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb
randall-romero/CompEcon
c7a75f57f8472c972fddcace8ff7b86fee049d29
MIT
2017-09-10T04:48:54.000Z
2018-03-31T01:36:46.000Z
_build/jupyter_execute/notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb
randall-romero/CompEcon-python
c7a75f57f8472c972fddcace8ff7b86fee049d29
MIT
2017-02-25T08:10:38.000Z
2020-05-15T09:49:16.000Z
Qwen/Qwen-72B
1. YES
2. YES