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

Compute fixedpoint of

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

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$.

In [1]:
from demos.setup import np, tic, toc
from compecon import NLP
np.random.seed(12)


### Set up the problem¶

In [2]:
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¶

In [3]:
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¶

In [4]:
xinit = np.random.randn(2)


### Compute fixed-point using Newton method¶

In [5]:
t0 = tic()
z1 = problem_as_zero.newton(xinit)
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm


### Compute fixed-point using Broyden method¶

In [6]:
t0 = tic()
z2 = problem_as_zero.broyden(xinit)
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm


### Compute fixed-point using function iteration¶

In [7]:
t0 = tic()
z3 = problem_as_fixpoint.fixpoint(xinit)
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - z3)

In [8]:
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