{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Compute root of Rosencrantz function\n", "\n", "**Randall Romero Aguilar, PhD**\n", "\n", "This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.\n", "\n", "Original (Matlab) CompEcon file: **demslv02.m**\n", "\n", "Running this file requires the Python version of CompEcon. This can be installed with pip by running\n", "\n", " !pip install compecon --upgrade\n", "\n", "Last updated: 2022-Sept-04\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## About\n", "\n", "Compute root of \n", "\n", "\\begin{equation}\n", "f(x_1,x_2)= \\begin{bmatrix}200x_1(x_2-x_1^2) + 1-x_1 \\\\ 100(x_1^2-x_2)\\end{bmatrix}\n", "\\end{equation}\n", "\n", "using Newton and Broyden methods. Initial values generated randomly. True root is $x_1=1, \\quad x_2=1$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from compecon import NLP, tic, toc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up the problem" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def f(x):\n", " x1, x2 = x\n", " fval = [200 * x1 * (x2 - x1 ** 2) + 1 - x1, 100 * (x1 ** 2 - x2)]\n", " fjac = [[200 * (x2 - x1 ** 2) - 400 * x1 ** 2 - 1, 200 * x1],\n", " [200 * x1, -100]]\n", " return fval, fjac\n", "\n", "problem = NLP(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Randomly generate starting point" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "problem.x0 = np.random.randn(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compute root using Newton method" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t0 = tic()\n", "x1 = problem.newton()\n", "t1 = 100 * toc(t0)\n", "n1 = problem.fnorm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compute root using Broyden method" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t0 = tic()\n", "x2 = problem.broyden()\n", "t2 = 100 * toc(t0)\n", "n2 = problem.fnorm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print results" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hundreds of seconds required to compute root of Rosencrantz function\n", "f(x1,x2)= [200*x1*(x2-x1^2)+1-x1;100*(x1^2-x2)] via Newton and Broyden\n", "methods, starting at x1 = -1.20 x2 = 0.26\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeNorm of fx1x2
Newton7.3972942.043809-0.7242360.522311
Broyden6.5981632.320916-0.6173090.375372
\n", "
" ], "text/plain": [ " Time Norm of f x1 x2\n", "Newton 7.397294 2.043809 -0.724236 0.522311\n", "Broyden 6.598163 2.320916 -0.617309 0.375372" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print('Hundreds of seconds required to compute root of Rosencrantz function')\n", "print('f(x1,x2)= [200*x1*(x2-x1^2)+1-x1;100*(x1^2-x2)] via Newton and Broyden')\n", "print('methods, starting at x1 = {:4.2f} x2 = {:4.2f}'.format(*problem.x0))\n", "\n", "pd.DataFrame({\n", " 'Time': [t1, t2],\n", " 'Norm of f': [n1, n2],\n", " 'x1': [x1[0], x2[0]],\n", " 'x2': [x1[1], x2[1]]},\n", " index=['Newton', 'Broyden']\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "vscode": { "interpreter": { "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186" } } }, "nbformat": 4, "nbformat_minor": 0 }