<jupyter_start><jupyter_text>Compute fixedpoint of $f(x, y)= [x^2 + y^3; xy - 0.5]$**<NAME>, PhD**This demo is based on the original Matlab demo accompanying the  Computational Economics and Finance 2001 textbook by <NAME> and <NAME>.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 --upgradeLast updated: 2021-Oct-01  AboutCompute 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$.<jupyter_code>import numpy as np
import pandas as pd
from compecon import NLP, tic, toc
np.random.seed(12)<jupyter_output><empty_output><jupyter_text>Set up the problem<jupyter_code>def g(z):
    x, y = z
    return np.array([x **2 + y ** 3, x * y - 0.5])

problem_as_fixpoint = NLP(g, maxit=1500)<jupyter_output><empty_output><jupyter_text>Equivalent Rootfinding Formulation<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Randomly generate starting point<jupyter_code>xinit = np.random.randn(2)<jupyter_output><empty_output><jupyter_text>Compute fixed-point using Newton method<jupyter_code>t0 = tic()
z1 = problem_as_zero.newton(xinit)
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm<jupyter_output><empty_output><jupyter_text>Compute fixed-point using Broyden method<jupyter_code>t0 = tic()
z2 = problem_as_zero.broyden(xinit)
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm<jupyter_output><empty_output><jupyter_text>Compute fixed-point using function iteration<jupyter_code>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))

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']
)<jupyter_output>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
{'fraction_non_alphanumeric': 0.15010956902848796, 'fraction_numerical': 0.04711468224981739, 'mean_word_length': 4.682572614107884, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '289941', 'license': 'MIT', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1087, 'n_words': 264, 'path': 'notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb', 'repo_name': 'randall-romero/CompEcon-python', 'size': '2767'}