<jupyter_start><jupyter_text>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$.<jupyter_code>from demos.setup import np, tic, toc
from compecon import NLP
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))
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))<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


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
{'fraction_non_alphanumeric': 0.17297887836853607, 'fraction_numerical': 0.06336489439184267, 'mean_word_length': 3.226153846153846, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '186128', 'license': 'MIT', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1147, 'n_words': 242, 'path': 'notebooks/slv/04 Compute fixedpoint of f(x1,x2)= [x1 2+x2 3; x1 x2 - 0.5].ipynb', 'repo_name': 'snowdj/CompEcon-python', 'size': '2746'}