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$.
from demos.setup import np, tic, toc
from compecon import NLP
np.random.seed(12)
def g(z):
x, y = z
return np.array([x **2 + y ** 3, x * y - 0.5])
problem_as_fixpoint = NLP(g, maxit=1500)
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)
xinit = np.random.randn(2)
t0 = tic()
z1 = problem_as_zero.newton(xinit)
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm
t0 = tic()
z2 = problem_as_zero.broyden(xinit)
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm
t0 = tic()
z3 = problem_as_fixpoint.fixpoint(xinit)
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - z3)
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))