{
 "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": [
    "## Taylor raðir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nálgun á $\\sin$ á bilinu $[-\\frac{\\pi}{4},\\frac{\\pi}{4}]$ með Taylor margliðum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.arange(-math.pi/4,math.pi/4,1e-2)\n",
    "#x = np.arange(-math.pi,math.pi,1e-2)\n",
    "#plt.plot(x,np.sin(x)-(x- x**3/6))\n",
    "#plt.plot(x,np.sin(x))\n",
    "#plt.plot(x,(x-x**3/6))\n",
    "#plt.plot(x,(x-x**3/6 + x**5/120))\n",
    "#plt.plot(x,np.sin(x) - (x-x**3/6+x**5/120-x**7/(7*6*120)))\n",
    "plt.plot(x,(np.sin(x) - (x-x**3/6+x**5/120-x**7/(7*6*120)))/np.sin(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hversu marga liði þarf til að nálga $\\sin$ með 15 aukastöfum?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def fact(n):\n",
    "    r = 1.0\n",
    "    for x in range(1,n+1):\n",
    "        r *= x\n",
    "    return r\n",
    "\n",
    "for n in range(14):\n",
    "    R = (math.pi/4)**(n+1)/fact(n+1)\n",
    "    print(\"n = %d, R_(n+1) = %.ef\"%(n,R))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nálgun á $ln(1+x)$ á bilinu $[\\frac{\\sqrt{2}}{2},\\sqrt{2}]$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.arange(math.sqrt(2)/2,math.sqrt(2),1e-2)-1\n",
    "#plt.plot(x,np.log(1+x))\n",
    "#plt.plot(x,x)\n",
    "#plt.plot(x,(x-x**2/2))\n",
    "#plt.plot(x,(x-x**2/2 + x**3/3))\n",
    "#plt.plot(x,np.log(1+x)-(x-x**2/2 + x**3/3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hversu marga liði þarf til að nálga $\\ln$ með 15 aukastöfum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for n in range(40):\n",
    "    R = (math.sqrt(2)-1)**(n+1)/(n+1)\n",
    "    print(\"n = %d, R_(n+1) = %.ef\"%(n,R))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "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
}
