Skip to content

Instantly share code, notes, and snippets.

@byronyi
Created March 2, 2015 11:30
Show Gist options
  • Save byronyi/ff4f28eb6e40d8ab370d to your computer and use it in GitHub Desktop.
Save byronyi/ff4f28eb6e40d8ab370d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:9e4db3e20c173d9ce302cadac98ae530021e48573c025d7706d5efc2d304070c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Assignment 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yi Bairen, 20026373"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import Latex, display\n",
"\n",
"from sympy.interactive import printing\n",
"from sympy.printing.latex import latex\n",
"\n",
"printing.init_printing(use_latex=True)\n",
"\n",
"def print_func(func_str, func):\n",
" if hasattr(func, 'expr'):\n",
" latex_str = '$$' + func_str + '=' + latex(func.expr) + '$$'\n",
" else:\n",
" latex_str = '$$' + func_str + '=' + latex(func) + '$$'\n",
" display(Latex(latex_str))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sympy import log, exp, diff, symbols, vectorize, Symbol, Float, Lambda, Matrix\n",
"from sympy.abc import x\n",
"\n",
"@vectorize(0, 1)\n",
"def vdiff(f, x):\n",
" return diff(f, x).simplify()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Problem 1"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = Lambda((x), 2*log(exp(x)+1)-x)\n",
"\n",
"print_func(\"f(x)\", f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x)=- x + 2 \\log{\\left (e^{x} + 1 \\right )}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0df150>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def Newton1D(f, x0, n_iter):\n",
" df = Lambda((x), f.expr.diff(x).simplify())\n",
" d2f = Lambda((x), df.expr.diff(x).simplify())\n",
" \n",
" print_func(\"f(x)\", f)\n",
" print_func(\"f'(x)\", df)\n",
" print_func(\"f''(x)\", d2f)\n",
" \n",
" xk = Float(x0)\n",
" for k in range(n_iter+1):\n",
" \n",
" print \"x({}) = {:.3f}\".format(k, float(xk)) + '\\t',\n",
" print \"f(x) = {:.3f}\".format(float(f(xk))) + '\\t',\n",
" print \"f'(x) = {:.3f}\".format(float(df(xk))) + '\\t',\n",
" print \"f''(x) = {:.3f}\".format(float(d2f(xk)))\n",
" x_temp = xk - df(xk) / d2f(xk)\n",
" xk = x_temp\n",
"\n",
"Newton1D(f, 2., 4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x)=- x + 2 \\log{\\left (e^{x} + 1 \\right )}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072c10>"
]
},
{
"latex": [
"$$f'(x)=\\tanh{\\left (\\frac{x}{2} \\right )}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072a90>"
]
},
{
"latex": [
"$$f''(x)=\\frac{1}{2 \\cosh^{2}{\\left (\\frac{x}{2} \\right )}}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072b10>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"x(0) = 2.000\tf(x) = 2.254\tf'(x) = 0.762\tf''(x) = 0.210\n",
"x(1) = -1.627\tf(x) = 1.986\tf'(x) = -0.671\tf''(x) = 0.275\n",
"x(2) = 0.819\tf(x) = 1.549\tf'(x) = 0.388\tf''(x) = 0.425\n",
"x(3) = -0.095\tf(x) = 1.389\tf'(x) = -0.047\tf''(x) = 0.499\n",
"x(4) = 0.000\tf(x) = 1.386\tf'(x) = 0.000\tf''(x) = 0.500\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def Secant(f, x_1, x0, n_iter):\n",
" df = Lambda((x), f.expr.diff(x).simplify())\n",
" print_func(\"f(x)\", f)\n",
" print_func(\"f'(x)\", df)\n",
" xk_1 = Float(x_1)\n",
" xk = Float(x0)\n",
" for k in range(n_iter+1):\n",
" secant = (df(xk) - df(xk_1)) / (xk - xk_1)\n",
" print \"x({}) = {:.3f}\".format(k-1, float(xk_1)) + '\\t',\n",
" print \"x({}) = {:.3f}\".format(k, float(xk)) + '\\t',\n",
" print \"f(x({})) = {:.3f}\".format(k, float(f(xk))) + '\\t',\n",
" print \"f'(x({})) = {:.3f}\".format(k-1, float(df(xk_1))) + '\\t',\n",
" print \"f'(x({})) = {:.3f}\".format(k, float(df(xk))) + '\\t',\n",
" print 'secant = {:.3f}'.format(float(secant))\n",
" x_temp = xk - df(xk) / secant\n",
" xk_1 = xk\n",
" xk = x_temp\n",
" \n",
"Secant(f, 2.01, 2., 4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x)=- x + 2 \\log{\\left (e^{x} + 1 \\right )}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072d50>"
]
},
{
"latex": [
"$$f'(x)=\\tanh{\\left (\\frac{x}{2} \\right )}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072a50>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"x(-1) = 2.010\tx(0) = 2.000\tf(x(0)) = 2.254\tf'(x(-1)) = 0.764\tf'(x(0)) = 0.762\tsecant = 0.209\n",
"x(0) = 2.000\tx(1) = -1.641\tf(x(1)) = 1.995\tf'(x(0)) = 0.762\tf'(x(1)) = -0.675\tsecant = 0.395\n",
"x(1) = -1.641\tx(2) = 0.070\tf(x(2)) = 1.388\tf'(x(1)) = -0.675\tf'(x(2)) = 0.035\tsecant = 0.415\n",
"x(2) = 0.070\tx(3) = -0.014\tf(x(3)) = 1.386\tf'(x(2)) = 0.035\tf'(x(3)) = -0.007\tsecant = 0.500\n",
"x(3) = -0.014\tx(4) = 0.000\tf(x(4)) = 1.386\tf'(x(3)) = -0.007\tf'(x(4)) = 0.000\tsecant = 0.500\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Problem 2"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x1, x2 = symbols('x_1, x_2')\n",
"x = (x1, x2)\n",
"f = Lambda(x, 2*x1**2 + x2**2 - 2*x1*x2 + 2*x1**3 + x1**4)\n",
"\n",
"print_func(\"f{}\".format(x), f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=x_{1}^{4} + 2 x_{1}^{3} + 2 x_{1}^{2} - 2 x_{1} x_{2} + x_{2}^{2}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b0d0>"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def Newton2D(f, x0, n_iter):\n",
" g = Lambda(x, vdiff(f.expr, x))\n",
" F = Lambda(x, Matrix(vdiff(g.expr, x)))\n",
" \n",
" g = Lambda(x, Matrix(g.expr))\n",
" \n",
" print_func(\"f{}\".format(x), f)\n",
" print_func(\"g{}\".format(x), g)\n",
" print_func(\"F{}\".format(x), F)\n",
" \n",
" xk = Matrix(x0)\n",
" for k in range(n_iter+1):\n",
" print\n",
" print 'Iter {}'.format(k)\n",
" print_func('x^{{({})}}'.format(k), Matrix(np.round(np.array(xk).astype(np.float), 3).squeeze()))\n",
" print_func('f{}'.format(x), np.round(np.array(f(*xk)).astype(np.float), 3))\n",
" print_func('g{}'.format(x), Matrix(np.round(np.array(g(*xk)).astype(np.float), 3).squeeze()))\n",
" print_func('F{}'.format(x), Matrix(np.round(np.array(F(*xk)).astype(np.float), 2).squeeze()))\n",
" x_temp = xk - F(*xk)**-1 * g(*xk)\n",
" xk = x_temp\n",
" \n",
"Newton2D(f, (0.5, 0.6), 4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=x_{1}^{4} + 2 x_{1}^{3} + 2 x_{1}^{2} - 2 x_{1} x_{2} + x_{2}^{2}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5bf410>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}4 x_{1}^{3} + 6 x_{1}^{2} + 4 x_{1} - 2 x_{2}\\\\- 2 x_{1} + 2 x_{2}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5bf090>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}12 x_{1}^{2} + 12 x_{1} + 4 & -2\\\\-2 & 2\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad04e510>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 0\n"
]
},
{
"latex": [
"$$x^{(0)}=\\left[\\begin{matrix}0.5 & 0.6\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad072a50>"
]
},
{
"latex": [
"$$f(x_1, x_2)=0.572$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049890>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}2.8 & 0.2\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049ed0>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}13.0 & -2.0\\\\-2.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad000250>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 1\n"
]
},
{
"latex": [
"$$x^{(1)}=\\left[\\begin{matrix}0.227 & 0.227\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049d50>"
]
},
{
"latex": [
"$$f(x_1, x_2)=0.078$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049210>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.811 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad000750>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}7.35 & -2.0\\\\-2.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0003d0>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 2\n"
]
},
{
"latex": [
"$$x^{(2)}=\\left[\\begin{matrix}0.076 & 0.076\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad000e90>"
]
},
{
"latex": [
"$$f(x_1, x_2)=0.007$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad00d310>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.187 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad03ded0>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}4.97 & -2.0\\\\-2.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049290>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 3\n"
]
},
{
"latex": [
"$$x^{(3)}=\\left[\\begin{matrix}0.013 & 0.013\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad00b090>"
]
},
{
"latex": [
"$$f(x_1, x_2)=0.0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad00b490>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.026 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad00bb10>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}4.15 & -2.0\\\\-2.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad00be50>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 4\n"
]
},
{
"latex": [
"$$x^{(4)}=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf907d0>"
]
},
{
"latex": [
"$$f(x_1, x_2)=0.0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049a90>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.001 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049490>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}4.01 & -2.0\\\\-2.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad049190>"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Problem 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Let $f(x) = (x \u2212 x_0)^4$ for $x \\in \\mathbb{R}$, where $x_0 \\in \\mathbb{R}$ is a constant. Suppose that we apply\n",
"Newton\u2019s method to the problem of minimizing $f$.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(1) Write down the update equation for Newton\u2019s method applied to the problem.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose $ x^{(k)} \\neq x_0 $, \n",
"\n",
"$$ f'(x) = 4(x-x_0)^3 $$\n",
"\n",
"$$ f''(x) = 12(x-x_0)^2 $$\n",
"\n",
"Therefore,\n",
"\n",
"$$ x^{(k+1)} = x^{(k)} - \\dfrac{f'(x^{(k)})}{f''(x^{(k)})} = x^{(k)} - \\dfrac{1}{3}(x^{(k)}-x_0) $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(2) Let $y^{(k)} = \\left|x^{(k)} \u2212 x_0\\right|$, where $x^{(k)}$ is the k-th iterate in Newton\u2019s method. Show that the sequence $\\left\\{y^{(k)}\\right\\}$ satisfies $ y^{(k+1)} = \\frac{2}{3} y^{(k)} $.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As in (1),\n",
"\n",
"$$ x^{(k+1)} = x^{(k)} - \\dfrac{1}{3}(x^{(k)}-x_0) $$\n",
"\n",
"$$ y^{(k+1)} = \\left|x^{(k+1)} - x_0\\right| = \\left|x^{(k)} - \\dfrac{1}{3}(x^{(k)}-x_0) - x_0 \\right|= \\frac{2}{3}\\left|x^{(k)}-x_0 \\right| = \\frac{2}{3} y^{(k)} $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(3) Show that $x^{(k)} \\rightarrow x_0$ for any initial guess $x^{(0)}$.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If $x^{(k)} = x_0$, the case is trivial. \n",
"\n",
"Otherwise, as $\\forall x^{(0)} \\neq x_0 $, $\\left\\{y^{(k)}\\right\\} \\rightarrow 0$, then $x^{(k)} \\rightarrow x_0$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(4) Show that the order of convergence of the sequence $\\left\\{x^{(k)}\\right\\}$ in (2) is 1.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As $$ \\lim_{k \\to \\infty} \\dfrac{\\left\\|x^{(k+1)} - x_0\\right\\|}{\\left\\|x^{(k)} - x_0\\right\\|} = \\dfrac{y^{(k+1)}}{y^{(k+1)}} = \\dfrac{2}{3} $$\n",
"By definition, order of convergence of $\\left\\{x^{(k)}\\right\\}$ is 1."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(5) The theorem (Theorem 9.1 in Ref. [1]) states that under certain conditions, the order of convergence of Newton\u2019s method is at least 2. Why does that the theorem not hold in this particular problem?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One precondition for Theorem 9.1 is that the Hessian of $f$ is invertible at $x_0$, which is clearly not satisfied as $f''(x_0) = 0$."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Problem 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Consider the problem of finding minimizer of the function $f(x_1, x_2) = 100(x_2 \u2212 x_1^2)^2 + (1-x_1)^2$.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(1) Show that $(1, 1)^T$ is the unique global minimizer of $f$ over $\\mathbb{R}^2$.**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x1, x2 = symbols('x_1, x_2')\n",
"x = (x1, x2)\n",
"\n",
"f = Lambda(x, 100*(x2-x1**2)**2 + (1-x1)**2)\n",
"g = Lambda(x, vdiff(f.expr, x))\n",
"F = Lambda(x, Matrix(vdiff(g.expr, x)))\n",
"\n",
"g = Lambda(x, Matrix(g.expr))\n",
"\n",
"print_func(\"f{}\".format(x), f)\n",
"print_func(\"g{}\".format(x), g)\n",
"print_func(\"F{}\".format(x), F)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=\\left(- x_{1} + 1\\right)^{2} + 100 \\left(- x_{1}^{2} + x_{2}\\right)^{2}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5a5a50>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}400 x_{1} \\left(x_{1}^{2} - x_{2}\\right) + 2 x_{1} - 2\\\\- 200 x_{1}^{2} + 200 x_{2}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0264d0>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1200 x_{1}^{2} - 400 x_{2} + 2 & - 400 x_{1}\\\\- 400 x_{1} & 200\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad026350>"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"var = (1, 1)\n",
"\n",
"print_func('f{}'.format(var), f(*var))\n",
"print_func('g{}'.format(var), g(*var))\n",
"print_func('F{}'.format(var), F(*var))\n",
"print_func('\\det F{}'.format(var), F(*var).det())\n",
"print_func('trace\\ F{}'.format(var), F(*var).trace())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(1, 1)=0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad03dbd0>"
]
},
{
"latex": [
"$$g(1, 1)=\\left[\\begin{matrix}0\\\\0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad03d450>"
]
},
{
"latex": [
"$$F(1, 1)=\\left[\\begin{matrix}802 & -400\\\\-400 & 200\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0e7b10>"
]
},
{
"latex": [
"$$\\det F(1, 1)=400$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0e7dd0>"
]
},
{
"latex": [
"$$trace\\ F(1, 1)=1002$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5bf850>"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sympy import solve\n",
"\n",
"display(solve(g(*x)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\begin{bmatrix}\\begin{Bmatrix}x_{1} : 1, & x_{2} : 1\\end{Bmatrix}\\end{bmatrix}$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"[{x\u2081: 1, x\u2082: 1}]"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, as $(1, 1)^T$ is the only solution to $g(x_1,x_2) = 0$, and there is no on-interior points in $\\mathbb{R}$, by SONC, there is no other local minimizer. \n",
"\n",
"Therefore, $(1, 1)^T$ is the unique global minimizer of $f$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(2) With the starting point $(0, 0)^T$, apply two iterations of Newton\u2019s method (use the inverse formula for a $2\\times 2$ matrix).**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Newton2D(f, (0., 0.), 2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=\\frac{x_{1}^{2}}{2} + x_{1} + x_{2}^{2} + \\frac{x_{2}}{2} + 3$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5bf410>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}x_{1} + 1\\\\2 x_{2} + \\frac{1}{2}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf85c10>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1 & 0\\\\0 & 2\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf85d90>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 0\n"
]
},
{
"latex": [
"$$x^{(0)}=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76410>"
]
},
{
"latex": [
"$$f(x_1, x_2)=3.0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad0e7f10>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.5\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76c10>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.0\\\\0.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76e90>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 1\n"
]
},
{
"latex": [
"$$x^{(1)}=\\left[\\begin{matrix}-1.0 & -0.25\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76750>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.438$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76690>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b550>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.0\\\\0.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b3d0>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 2\n"
]
},
{
"latex": [
"$$x^{(2)}=\\left[\\begin{matrix}-1.0 & -0.25\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8df50>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.438$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d1d0>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d4d0>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.0\\\\0.0 & 2.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d550>"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(3) Repeat part (2) using a gradient algorithm with a fixed step size of $\\alpha_k = 0.05$ at each iteration.**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def Gradient(f, x0, alpha, n_iter):\n",
" g = Lambda(x, vdiff(f.expr, x))\n",
" F = Lambda(x, Matrix(vdiff(g.expr, x)))\n",
" \n",
" g = Lambda(x, Matrix(g.expr))\n",
" \n",
" print_func(\"f{}\".format(x), f)\n",
" print_func(\"g{}\".format(x), g)\n",
" print_func(\"F{}\".format(x), F)\n",
" \n",
" xk = Matrix(x0)\n",
" for k in range(n_iter+1):\n",
" print\n",
" print 'Iter {}'.format(k)\n",
" print_func('x^{{({})}}'.format(k), Matrix(np.round(np.array(xk).astype(np.float), 3).squeeze()))\n",
" print_func('f{}'.format(x), np.round(np.array(f(*xk)).astype(np.float), 3))\n",
" print_func('g{}'.format(x), Matrix(np.round(np.array(g(*xk)).astype(np.float), 3).squeeze()))\n",
" x_temp = xk - alpha * g(*xk)\n",
" xk = x_temp\n",
" \n",
"Gradient(f, (0., 0.), 0.05, 2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=\\frac{x_{1}^{2}}{2} + x_{1} + x_{2}^{2} + \\frac{x_{2}}{2} + 3$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d650>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}x_{1} + 1\\\\2 x_{2} + \\frac{1}{2}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad03dc10>"
]
},
{
"latex": [
"$$F(x_1, x_2)=\\left[\\begin{matrix}1 & 0\\\\0 & 2\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d610>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 0\n"
]
},
{
"latex": [
"$$x^{(0)}=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d510>"
]
},
{
"latex": [
"$$f(x_1, x_2)=3.0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d390>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.5\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8df90>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 1\n"
]
},
{
"latex": [
"$$x^{(1)}=\\left[\\begin{matrix}-0.05 & -0.025\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b450>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.939$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b390>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.95 & 0.45\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76490>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 2\n"
]
},
{
"latex": [
"$$x^{(2)}=\\left[\\begin{matrix}-0.098 & -0.048\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76ad0>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.886$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf763d0>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.902 & 0.405\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace7c750>"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Problem 5"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x1, x2 = symbols('x_1, x_2')\n",
"x = (x1, x2)\n",
"f = Lambda(x, x1 + x2/2 + x1**2/2 + x2**2 + 3)\n",
"\n",
"print_func(\"f{}\".format(x), f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=\\frac{x_{1}^{2}}{2} + x_{1} + x_{2}^{2} + \\frac{x_{2}}{2} + 3$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d710>"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def SteepestDescent(f, x0, n_iter):\n",
" g = Lambda(x, Matrix(vdiff(f.expr, x)))\n",
" alpha = Symbol('\u03b1')\n",
" \n",
" print_func(\"f{}\".format(x), f)\n",
" print_func(\"g{}\".format(x), g)\n",
" \n",
" xk = Matrix(x0)\n",
" for k in range(n_iter+1):\n",
" print\n",
" print 'Iter {}'.format(k)\n",
" print_func('x^{{({})}}'.format(k), Matrix(np.round(np.array(xk).astype(np.float), 3).squeeze()))\n",
" print_func('f{}'.format(x), np.round(np.array(f(*xk)).astype(np.float), 3))\n",
" print_func('g{}'.format(x), Matrix(np.round(np.array(g(*xk)).astype(np.float), 3).squeeze()))\n",
" x_temp = Lambda((alpha), xk - alpha * g(*xk))\n",
" a, = solve(vdiff(f(*x_temp(alpha)), alpha))\n",
" print_func('f_{\u03b1}', f(*x_temp(alpha)).simplify())\n",
" print_func('\u03b1_{optimal}', np.round(float(a), 3))\n",
" xk = x_temp(a)\n",
" \n",
"SteepestDescent(f, (0., 0.), 2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$f(x_1, x_2)=\\frac{x_{1}^{2}}{2} + x_{1} + x_{2}^{2} + \\frac{x_{2}}{2} + 3$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace84dd0>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}x_{1} + 1\\\\2 x_{2} + \\frac{1}{2}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5a5d50>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 0\n"
]
},
{
"latex": [
"$$x^{(0)}=\\left[\\begin{matrix}0.0 & 0.0\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace84f10>"
]
},
{
"latex": [
"$$f(x_1, x_2)=3.0$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace7c890>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}1.0 & 0.5\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76550>"
]
},
{
"latex": [
"$$f_{\u03b1}=0.75 \u03b1^{2} - 1.25 \u03b1 + 3$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8df90>"
]
},
{
"latex": [
"$$\u03b1_{optimal}=0.833$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d350>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 1\n"
]
},
{
"latex": [
"$$x^{(1)}=\\left[\\begin{matrix}-0.833 & -0.417\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf8d390>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.479$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace880d0>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.167 & -0.333\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace883d0>"
]
},
{
"latex": [
"$$f_{\u03b1}=0.125 \u03b1^{2} - 0.138888888888889 \u03b1 + 2.47916666666667$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ad09b550>"
]
},
{
"latex": [
"$$\u03b1_{optimal}=0.556$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76ed0>"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Iter 2\n"
]
},
{
"latex": [
"$$x^{(2)}=\\left[\\begin{matrix}-0.926 & -0.231\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76610>"
]
},
{
"latex": [
"$$f(x_1, x_2)=2.441$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf76b90>"
]
},
{
"latex": [
"$$g(x_1, x_2)=\\left[\\begin{matrix}0.074 & 0.037\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60acf85c10>"
]
},
{
"latex": [
"$$f_{\u03b1}=0.00411522633744856 \u03b1^{2} - 0.00685871056241427 \u03b1 + 2.44058641975309$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ae5a5d50>"
]
},
{
"latex": [
"$$\u03b1_{optimal}=0.833$$"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Latex at 0x7f60ace84f10>"
]
}
],
"prompt_number": 20
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment