{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 24: Numerical Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 24.1, page no. 636"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finding roots of this equation by bisection method\n",
      "f(2) is -ve and f(3) is +ve so root lies between 2 and 3\n",
      "The root is:  2.6875\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = numpy.poly([0])\n",
    "p = x**3-4*x-9\n",
    "print \"Finding roots of this equation by bisection method\"\n",
    "print 'f(2) is -ve and f(3) is +ve so root lies between 2 and 3'\n",
    "l = 2.\n",
    "m = 3.\n",
    "def f(x):\n",
    "    y = x**3-4*x-9\n",
    "    return y\n",
    "for i in range(1,5):\n",
    "    k = 1.0/2.*(l+m)\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root is: \", k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 24.3, page no. 638"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f(x)=xeˆx−cos(x)\n",
      "We are required to find the roots of f(x) by the method of false position \n",
      "f(0)=−ve and f(1)=+ve so s root lie between 0 and 1 \n",
      "finding the roots by false position method \n",
      "The root of the equation is :\n",
      "0.517747878322\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "print \"f(x)=xeˆx−cos(x)\"\n",
    "def f(x):\n",
    "    y = x*math.e**(x)-math.cos(x)\n",
    "    return y\n",
    "print \"We are required to find the roots of f(x) by the method of false position \"\n",
    "print \"f(0)=−ve and f(1)=+ve so s root lie between 0 and 1 \"\n",
    "print \"finding the roots by false position method \"\n",
    "l = 0.0\n",
    "m = 1.0\n",
    "for i in range(1,11):\n",
    "    k = l-(m-l)*f(l)/(f(m)-f(l))\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root of the equation is :\"\n",
    "print k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.4, page no. 638"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f(x) = x∗math.log(x)−1.2\n",
      "We are required to find the roots of f(x) by the method of false position \n",
      "f(2)=−ve and f(3)=+ve so s root lie between 2 and 3 \n",
      "finding the roots by false position method \n",
      "The root of the equation is :  2.74063625664\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "print \"f(x) = x∗math.log(x)−1.2\"\n",
    "def f(x):\n",
    "    y = x*math.log10(x)-1.2\n",
    "    return y\n",
    "print \"We are required to find the roots of f(x) by the method of false position \"\n",
    "print \"f(2)=−ve and f(3)=+ve so s root lie between 2 and 3 \"\n",
    "print \"finding the roots by false position method \"\n",
    "l = 2.\n",
    "m = 3.\n",
    "for i in range(1,4):\n",
    "    k = l-(m-l)*f(l)/(f(m)-f(l))\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root of the equation is : \",k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.5, page no. 639"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find the roots of f(x) = 3x−cos(x)−1 by newtons method \n",
      "f(0)=−ve and f(1) is +ve so a root lies between 0 and 1 \n",
      "Let us take x0 =0.6 as the root is closer to 1 \n",
      "Root is given by r=x0−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are \n",
      "0.607290551153\n",
      "0.607096741973\n",
      "0.607101775605\n"
     ]
    }
   ],
   "source": [
    "import math,numpy\n",
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find the roots of f(x) = 3x−cos(x)−1 by newtons method \"\n",
    "print \"f(0)=−ve and f(1) is +ve so a root lies between 0 and 1 \"\n",
    "l = 0\n",
    "m = 1\n",
    "def f(x):\n",
    "    y = 3*x-math.cos(x)-1\n",
    "    return y\n",
    "x0 = 0.6\n",
    "print \"Let us take x0 =0.6 as the root is closer to 1 \"\n",
    "print \"Root is given by r=x0−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are \"\n",
    "for i in range(1,4):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.6, page no. 640"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \n",
      "To find the roots by newtons method\n",
      "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \n",
      "Let us take x0 = 5.5 \n",
      "Root is given by rn=xn−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are\n",
      "5.29545454545\n",
      "5.29150409676\n",
      "5.29150262213\n",
      "5.29150262213\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \"\n",
    "def f(x):\n",
    "    y = x**2-28\n",
    "    return y\n",
    "print \"To find the roots by newtons method\"\n",
    "print \"f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \"\n",
    "l = 5\n",
    "m = 6\n",
    "print \"Let us take x0 = 5.5 \"\n",
    "print \"Root is given by rn=xn−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are\"\n",
    "x0 = 5.5\n",
    "for i in range(1,5):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.7, page no. 641"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \n",
      "To find the roots by newtons method\n",
      "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \n",
      "Let us take x0 = 5.5 \n",
      "Root is given by rn=xn−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are\n",
      "5.29545454545\n",
      "5.29150409676\n",
      "5.29150262213\n",
      "5.29150262213\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \"\n",
    "def f(x):\n",
    "    y = x**2-28\n",
    "    return y\n",
    "print \"To find the roots by newtons method\"\n",
    "print \"f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \"\n",
    "l = 5\n",
    "m = 6\n",
    "print \"Let us take x0 = 5.5 \"\n",
    "print \"Root is given by rn=xn−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are\"\n",
    "x0 = 5.5\n",
    "for i in range(1,5):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}