{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fyrirlestur 3\n",
    "### 26. janúar\n",
    "\n",
    "Python uppsetning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Afleiður\n",
    "\n",
    "Python fall sem býr til tölulegar afleiður"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def simple_derivative(f,h):\n",
    "    def fp(x):\n",
    "        return (f(x+h)-f(x))/h\n",
    "    return fp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def f1(x):\n",
    "    return x*np.exp(x)\n",
    "\n",
    "def df1(x):\n",
    "    return (x+1)*np.exp(x)\n",
    "\n",
    "ndf1 = simple_derivative(f1,1e-13)\n",
    "\n",
    "\n",
    "\n",
    "x = np.arange(-0.5,0.5,1/128)\n",
    "ndf1 = simple_derivative(f1,1e-5)\n",
    "plt.plot(x,(df1(x) - ndf1(x))/df1(x))\n",
    "print(np.max(np.abs((df1(x) - ndf1(x))/df1(x))))\n",
    "#plt.plot(x,ndf1_4(x))\n",
    "#df1(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nákvæmni sem fall af $h$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "klist = []\n",
    "ylist = []\n",
    "for k in range(2,16):\n",
    "    klist.append(k)\n",
    "    h = 10**(-k)\n",
    "    ndf1 = simple_derivative(f1,h)\n",
    "    ylist.append(np.max(np.abs((df1(x) - ndf1(x))/df1(x))))\n",
    "plt.plot(klist,np.log10(ylist))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f2 = np.cos\n",
    "df2 = lambda x: -np.sin(x)\n",
    "ndf2 = simple_derivative(f2,1e-5)\n",
    "plt.plot(x,df2(x) - ndf2(x))\n",
    "print(np.max(np.abs(df2(x) - ndf2(x))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "klist = []\n",
    "ylist = []\n",
    "for k in range(2,16):\n",
    "    klist.append(k)\n",
    "    h = 10**(-k)\n",
    "    ndf2 = simple_derivative(f2,h)\n",
    "    ylist.append(np.max(np.abs(df2(x) - ndf2(x))))\n",
    "plt.plot(klist,np.log10(ylist))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miðpunktsaðferð\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def mid_derivative(f,h):\n",
    "    return lambda x: (f(x+h)-f(x-h))/(2*h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "x = np.arange(-0.5,0.5,1/128)\n",
    "ndf1 = mid_derivative(f1,1e-5)\n",
    "plt.plot(x,df1(x) - ndf1(x))\n",
    "print(np.max(np.abs(df1(x) - ndf1(x))))\n",
    "#plt.plot(x,ndf1_4(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "klist = []\n",
    "ylist = []\n",
    "for k in range(2,16):\n",
    "    klist.append(k)\n",
    "    h = 10**(-k)\n",
    "    ndf1 = mid_derivative(f1,h)\n",
    "    ylist.append(np.max(np.abs(df1(x) - ndf1(x))))\n",
    "plt.plot(klist,np.log10(ylist))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f2 = np.cos\n",
    "df2 = lambda x: -np.sin(x)\n",
    "ndf2 = mid_derivative(f2,1e-5)\n",
    "plt.plot(x,df2(x) - ndf2(x))\n",
    "print(np.max(np.abs(df2(x) - ndf2(x))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "klist = []\n",
    "ylist = []\n",
    "for k in range(2,16):\n",
    "    klist.append(k)\n",
    "    h = 10**(-k)\n",
    "    ndf2 = mid_derivative(f2,h)\n",
    "    ylist.append(np.max(np.abs(df2(x) - ndf2(x))))\n",
    "plt.plot(klist,np.log10(ylist))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ólínulegar jöfnur\n",
    "\n",
    "Skoðum jöfnuna $$x^3 - x - 1 = 0$$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return x**3 - x -1\n",
    "x = np.arange(-2,2,0.01)\n",
    "plt.plot(x,f(x))\n",
    "plt.axhline(color='red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helmingunarleit\n",
    "\n",
    "Skilgreinum fall sem tekur inn tvær tölur og eitt fall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def bisection(f,a,b):\n",
    "    fa = f(a)\n",
    "    if fa==0:\n",
    "        return a;\n",
    "    fb = f(b)\n",
    "    if fb==0:\n",
    "        return b\n",
    "    assert fa*fb < 0, \"Values must have opposite signs\"\n",
    "    c = (a+b)/2\n",
    "    while a!=c and b!=c:\n",
    "        fc = f(c)\n",
    "    \n",
    "        if fc==0:\n",
    "            return c\n",
    "        elif fa*fc < 0:\n",
    "            b,fb = c,fc \n",
    "        else:\n",
    "            a,fa = c,fc\n",
    "        c = (a+b)/2\n",
    "\n",
    "    return c\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(lambda x: x**2-2)\n",
    "print(bisection(lambda x: x**2-2,0,2))\n",
    "print(bisection(lambda x: x**2-2,0,2) - math.sqrt(2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lausn á 3. stigs jöfnu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bisection(f,0,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Samanburður við innbyggð föll\n",
    "Berum saman helmingunarleit og innbyggða fallið `math.asin`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def my_asin(x):\n",
    "    return bisection(lambda y: np.sin(y) - x, -np.pi/2,np.pi/2)\n",
    "print(my_asin(0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import math\n",
    "x = np.arange(-0.99,0.99,0.01)\n",
    "np_my_asin = np.vectorize(my_asin) \n",
    "np_asin = np.vectorize(math.asin)\n",
    "plt.plot(x,np_asin(x) - np_my_asin(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
