{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 13: Linear Differential Equations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.1, page no. 398"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differential equation is given by: \n",
      "y =  c1*exp(-1.28077640640442*x) + c2*exp(0.780776406404415*x)\n"
     ]
    }
   ],
   "source": [
    "import sympy, numpy\n",
    "\n",
    "print \"Solution to the given linear differential equation is given by: \"\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**2+m-2\n",
    "r = numpy.roots([2, 1, -2])\n",
    "y = c1*sympy.exp(r[0]*x)+c2*sympy.exp(r[1]*x)\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.2, page no. 399"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "y =  (c1 + c2*x)*exp(x*(-1.5 + 1.5*I))\n"
     ]
    }
   ],
   "source": [
    "import sympy, numpy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**2+6*m+9;\n",
    "r = numpy.roots([2, 6, 9])\n",
    "y =(c1+x*c2)*sympy.exp(r[0]*x)\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.3, page no. 401"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "y =  c1*exp(0.105616806777468*x) + c2*exp(0.105616806777468*x) + c3*exp(-0.877900280221601*x)\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**3+m*2+4*m+4;\n",
    "r = numpy.roots([3, 2, 4, 4])\n",
    "y = c1*sympy.exp(r[0].real*x)+c2*sympy.exp(r[1].real*x)+c3*sympy.exp(r[2].real*x)\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.4, page no. 402"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "y =  c1*exp(-0.707106781186547*x) + c2*exp(-0.707106781186547*x) + c3*exp(0.707106781186547*x) + c4*exp(0.707106781186547*x)\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "c4 = sympy.Symbol('c4')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**4+4;\n",
    "r = numpy.roots([4, 0, 0, 0, 4])\n",
    "y = c1*sympy.exp(r[0].real*x)+c2*sympy.exp(r[1].real*x)+c3*sympy.exp(r[2].real*x)+c4*sympy.exp(r[3].real*x)\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.5, page no. 402"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "y =  [ 9041.93285661    22.41271075]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "m = numpy.poly([0])\n",
    "f = m**2+5*m+6\n",
    "y = numpy.exp(f)/numpy.polyval(f,1)\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.6, page no. 403"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution of the given linear equation is given by: \n",
      "[ 0.25+0.96824584j  0.25-0.96824584j]\n",
      "y = 1/f(D)∗[exp(-2x)+exp(x)-exp(-x)\n",
      "using 1/f(D)exp(ax) = x/f1(D)∗exp(ax) if f(m)=0\n",
      "y =  x**2*exp(x)/6 + x*exp(-2*x)/9 + exp(-x)/4\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution of the given linear equation is given by: '\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f =(m+2)*(m-1)**2;\n",
    "r = numpy.roots([2, -1, 2])\n",
    "print r\n",
    "print 'y = 1/f(D)∗[exp(-2x)+exp(x)-exp(-x)'\n",
    "print 'using 1/f(D)exp(ax) = x/f1(D)∗exp(ax) if f(m)=0'\n",
    "y1 = x*sympy.exp(-2*x)/9\n",
    "y2 = sympy.exp(-x)/4\n",
    "y3 = x**2*sympy.exp(x)/6\n",
    "y = y1+y2+y3\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.7, page no. 404"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution of the given linear equation is given by: \n",
      "Using the identity 1/f(D**2)∗sin(ax+b)[or cos(ax+b)]=1/f(-a**2)∗sin(ax+b)[or cos(ax+b)] this equation \n",
      "can be redused to y = (4D+1)/65∗cos(2x-1)\n",
      "y =  -8*sin(2*x - 1)/65 + cos(2*x - 1)/65\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution of the given linear equation is given by: '\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**3+1;\n",
    "print '''Using the identity 1/f(D**2)∗sin(ax+b)[or cos(ax+b)]=1/f(-a**2)∗sin(ax+b)[or cos(ax+b)] this equation \n",
    "can be redused to''',\n",
    "print 'y = (4D+1)/65∗cos(2x-1)'\n",
    "y = (sympy.cos(2*x-1)+4*sympy.diff(sympy.cos(2*x-1),x))/65\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.8, page no. 405"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution of the given linear equation is given by: \n",
      "Using 1/f(D)exp(ax) = x/f1(D)∗exp(ax) if f(m)=0\n",
      "y = x∗1/(3D**2+4)∗sin2x\n",
      "Using this identity 1/f(D**2)∗sin(ax+b)[or cos(ax+b)]= 1/f(-a**2)∗sin(ax+b)[or cos (ax+b)] this equation \n",
      "can be redused to y = -x/8∗sin2x\n",
      "y =  -0.113662178353\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution of the given linear equation is given by: '\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**3+4*m\n",
    "print 'Using 1/f(D)exp(ax) = x/f1(D)∗exp(ax) if f(m)=0'\n",
    "print 'y = x∗1/(3D**2+4)∗sin2x'\n",
    "print '''Using this identity 1/f(D**2)∗sin(ax+b)[or cos(ax+b)]= 1/f(-a**2)∗sin(ax+b)[or cos (ax+b)] this equation \n",
    "can be redused to''',\n",
    "print 'y = -x/8∗sin2x'\n",
    "x=1\n",
    "y = -x*numpy.sin(2*x)/8\n",
    "print \"y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example  13.9, page no. 406"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution of the given linear equation is given by: \n",
      "y = 1/(D(D+1))[x**2+2x+4] can be written as (1−D+D**2)/D[x**2+2x+4] which is combination of\n",
      "differentialtion and integration\n",
      "y =  x**3/3 + 4*x\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution of the given linear equation is given by: '\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "print '''y = 1/(D(D+1))[x**2+2x+4] can be written as (1−D+D**2)/D[x**2+2x+4] which is combination of\n",
    "differentialtion and integration'''\n",
    "g = x**2+2*x+4\n",
    "f = g-sympy.diff(g,x)+sympy.diff(g,x,2)\n",
    "y = sympy.integrate(f,x)\n",
    "print 'y = ', y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.11, page no. 406"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "CF + PI\n",
      "[-1.]\n",
      "CF is given by: \n",
      "(c1 + c2*x)*exp(-1.0*x)\n",
      "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n",
      "PI = 8∗{1/(D−2)**2[exp(2x)]+{1/(D−2)**2[sin(2x)]+{1/(D−2)**2[x**2]}\n",
      "Using identitties it reduces to:  4*x**2*exp(2*x) + 4*x + cos(2*x) + 3\n",
      "The solution is y =  4*x**2*exp(2*x) + 4*x + (c1 + c2*x)*exp(-1.0*x) + cos(2*x) + 3\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "print 'CF + PI'\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = (m-2)**2;\n",
    "r = numpy.roots([2, 2])\n",
    "print r\n",
    "print 'CF is given by: '\n",
    "cf = (c1+c2*x)*sympy.exp(r[0]*x)\n",
    "print cf\n",
    "print '−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−'\n",
    "print 'PI = 8∗{1/(D−2)**2[exp(2x)]+{1/(D−2)**2[sin(2x)]+{1/(D−2)**2[x**2]}'\n",
    "print 'Using identitties it reduces to: ',\n",
    "pi = 4*x**2*sympy.exp(2*x)+sympy.cos(2*x)+4*x+3\n",
    "print pi\n",
    "y = cf + pi ;\n",
    "print 'The solution is y = ', y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exampe 13.12, page no.  407"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "CF + PI\n",
      "[-0.+1.41421356j  0.-1.41421356j]\n",
      "CF is given by\n",
      "c1*exp(1.4142135623731*I*x) + c2*exp(-1.4142135623731*I*x)\n",
      "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n",
      "PI = 8∗{1/(D**2-4)[x∗sinh(x)]\n",
      "Using identities it reduces to:  -x*(exp(x) - exp(-x))/6\n",
      "The solution is y =  c1*exp(1.4142135623731*I*x) + c2*exp(-1.4142135623731*I*x) - x*(exp(x) - exp(-x))/6\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "print 'CF + PI'\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**2-4\n",
    "r = numpy.roots([2, 0, 4])\n",
    "print r\n",
    "print 'CF is given by'\n",
    "cf = c1*sympy.exp(r[0]*x)+c2*sympy.exp(r[1]*x)\n",
    "print cf\n",
    "print '−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−'\n",
    "print 'PI = 8∗{1/(D**2-4)[x∗sinh(x)]'\n",
    "print 'Using identities it reduces to: ',\n",
    "pi = -x/6*(sympy.exp(x)-sympy.exp(-x))-2/18*(sympy.exp(x)+sympy.exp(-x))\n",
    "print pi\n",
    "y = cf + pi\n",
    "print \"The solution is y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.13, page no. 408"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "CF + PI\n",
      "[-0.+0.70710678j  0.-0.70710678j]\n",
      "CF is given by\n",
      "c1*exp(0.707106781186548*I*x) + c2*exp(-0.707106781186548*I*x)\n",
      "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n",
      "PI = -1/10∗{1/(D**2-1)[x∗sin(3x)+cos(x)]\n",
      "Using identities it reduces to:  -x*sin(3*x) - cos(x)/2\n",
      "The solution is y =  c1*exp(0.707106781186548*I*x) + c2*exp(-0.707106781186548*I*x) - x*sin(3*x) - cos(x)/2\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "print 'CF + PI'\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f = m**2-1\n",
    "r = numpy.roots([2, 0, 1])\n",
    "print r\n",
    "print 'CF is given by'\n",
    "cf = c1*sympy.exp(r[0]*x)+c2*sympy.exp(r[1]*x)\n",
    "print cf\n",
    "print '−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−'\n",
    "print 'PI = -1/10∗{1/(D**2-1)[x∗sin(3x)+cos(x)]'\n",
    "print 'Using identities it reduces to: ',\n",
    "pi = -1/10*(x*sympy.sin(3*x)+3/5*sympy.cos(3*x))-sympy.cos(x)/2\n",
    "print pi\n",
    "y = cf + pi\n",
    "print \"The solution is y = \", y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13.14, page no. 408"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution to the given linear differntial equation is given by: \n",
      "CF + PI\n",
      "(5.55111512313e-17+0.707106781187j)\n",
      "CF is given by\n",
      "(c1 + c2*x)*exp(5.55111512312578e-17*x) + (c3 + c4*x)*exp(-0.5*x)\n",
      "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n",
      "PI = ∗{1/(D**4+2∗D+1)[x**2∗cos(x)]\n",
      "Using identities it reduces to:  4*x**3*sin(x) - (x**4 - 9*x**2)*cos(x)\n",
      "The solution is y =  4*x**3*sin(x) + (c1 + c2*x)*exp(5.55111512312578e-17*x) + (c3 + c4*x)*exp(-0.5*x) - (x**4 - 9*x**2)*cos(x)\n"
     ]
    }
   ],
   "source": [
    "import numpy, sympy\n",
    "\n",
    "print 'Solution to the given linear differntial equation is given by: '\n",
    "print 'CF + PI'\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "c4 = sympy.Symbol('c4')\n",
    "x = sympy.Symbol('x')\n",
    "m = numpy.poly([0])\n",
    "f =m**4+2*m**2+1\n",
    "r = numpy.roots([4, 2, 2, 1])\n",
    "print r[0]\n",
    "print 'CF is given by'\n",
    "cf = ((c1+c2*x)*sympy.exp(r[0].real*x)+(c3+c4*x)*sympy.exp(r[2].real*x))\n",
    "print cf\n",
    "print '−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−'\n",
    "print 'PI = ∗{1/(D**4+2∗D+1)[x**2∗cos(x)]'\n",
    "print 'Using identities it reduces to: ',\n",
    "pi = -1/48*((x**4-9*x**2)*sympy.cos(x)-4*x**3*sympy.sin(x))\n",
    "print pi\n",
    "y = cf + pi\n",
    "print \"The solution is y = \", y"
   ]
  }
 ],
 "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.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}