{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Optimization in 1-D and N-D. Debugging Functions\n", "====\n", "\n", "## Unit 11, Lecture 1\n", "\n", "*Numerical Methods and Statistics*\n", "\n", "----\n", "\n", "#### Prof. Andrew White, April 19, 2020" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Goals:\n", "---\n", "\n", "1. Learn the meaning of root-finding and minimization, the two types of optimization\n", "2. Understand the iterative nature of these methods\n", "3. Debug common problems when defining functions, which is essential for optimization\n", "4. Be able to identify convex problems and understand their complexties\n", "5. Learn the two standard methods for minimize and root-finding and how to call them in Python" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide_input": false, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Numerical Optimization - Root Finding\n", "====\n", "\n", "What is $x$ in\n", "\n", "$$\\cos (x) = x$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "To use a root-finding method, we must make our equation have one side be 0.\n", "\n", "$$\\cos (x) - x = 0$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Newton's Method - For finding Roots\n", "====" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7390851332151606 1.1102230246251565e-16\n" ] } ], "source": [ "from scipy.optimize import newton\n", "\n", "root = newton(lambda x: np.cos(x) - x, x0=0)\n", "print(root, np.cos(root) - root)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$$x_{i+1} = x_i - \\frac{f(x_i)}{f'(x_i)}$$" ] }, { "cell_type": "raw", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "
Source: Wikipedia, Newton's Method
\n", "