{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 26 : Difference Equations And Z Transform"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.2, page no. 667"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yn=  (-2)**n*b + 2**n*a\n",
      "y(n+1)=yn1= (-2.0)**n*b + 2.0**n*a\n",
      "y(n+2)=yn2= (-2.0)**n*b + 2.0**n*a\n",
      "Eliminating a b from these equations we get :\n",
      "The required difference equation : \n",
      "16*yn0 - 4*yn2\n",
      "=0\n"
     ]
    }
   ],
   "source": [
    "import sympy,numpy\n",
    "\n",
    "n = sympy.Symbol('n')\n",
    "a = sympy.Symbol('a')\n",
    "b = sympy.Symbol('b')\n",
    "yn0 = sympy.Symbol('yn0')\n",
    "yn1 = sympy.Symbol('yn1')\n",
    "yn2 = sympy.Symbol('yn2')\n",
    "yn = a*2**n+b*(-2)**n\n",
    "print \"yn= \",yn\n",
    "n = n+1\n",
    "yn = yn.evalf()\n",
    "print \"y(n+1)=yn1=\",yn\n",
    "n = n+1\n",
    "yn = yn.evalf()\n",
    "print \"y(n+2)=yn2=\",yn\n",
    "print \"Eliminating a b from these equations we get :\"\n",
    "A = sympy.Matrix([[yn0,1,1],[yn1,2,-2],[yn2,4,4]])\n",
    "y = A.det()\n",
    "print \"The required difference equation : \"\n",
    "print y\n",
    "print \"=0\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.3, page no. 669"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function is given by Eˆ3−2∗Eˆ2−5∗E+6=0 \n",
      "[-2.  3.  1.]\n",
      "Therefor the complete solution is : \n",
      "un= (-2.0)**(n + 2)*c1 + 1.0**(n + 2)*c3 + 3.0**(n + 2)*c2\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "print \"Cumulative function is given by Eˆ3−2∗Eˆ2−5∗E+6=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**3-2*E**2-5*E+6\n",
    "r = numpy.roots([1,-2,-5,6])\n",
    "print r\n",
    "print \"Therefor the complete solution is : \"\n",
    "un = c1*(r[0])**n+c2*(r[1])**n+c3*(r[2])**n\n",
    "print \"un=\",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 26.4, page no. 670"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function is given by Eˆ2−2∗E+1=0 \n",
      "[ 1.  1.]\n",
      "Therefor the complete solution is : \n",
      "un =  1.0**n*(c1 + c2*n)\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"Cumulative function is given by Eˆ2−2∗E+1=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-2*E+1\n",
    "r = numpy.roots([1,-2,1])\n",
    "print r\n",
    "print \"Therefor the complete solution is : \"\n",
    "un = (c1+c2*n)*(r[0])**n\n",
    "print \"un = \",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.6, page no. 671"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For Fibonacci Series yn2=yn1+yn0 \n",
      "So Cumulative function is given by Eˆ2−E−1=0 \n",
      "[ 1.61803399 -0.61803399]\n",
      "Therefor the complete solution is : \n",
      "un =  (-0.618033988749895)**n*c2 + 1.61803398874989**n*c1\n",
      "Now puttting n=1, y=0 and n=2, y=1 we get \n",
      "c1=(5−sqrt(5))/10 c2=(5+sqrt(5))/10 \n",
      "(-0.618033988749895)**n*c2 + 1.61803398874989**n*c1\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy,math\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"For Fibonacci Series yn2=yn1+yn0 \"\n",
    "print \"So Cumulative function is given by Eˆ2−E−1=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-E-1\n",
    "r = numpy.roots([1,-1,-1])\n",
    "print r\n",
    "print \"Therefor the complete solution is : \"\n",
    "un = (c1)*(r[0])**n+c2*(r[1])**n \n",
    "print \"un = \",un\n",
    "print \"Now puttting n=1, y=0 and n=2, y=1 we get \"\n",
    "print \"c1=(5−sqrt(5))/10 c2=(5+sqrt(5))/10 \"\n",
    "c1 =(5-math.sqrt(5))/10\n",
    "c2 =(5+math.sqrt(5))/10\n",
    "un = un.evalf()\n",
    "print un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.7, page no. 672"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function  is given by Eˆ2−4∗E+3=0 \n",
      "[ 3.  1.]\n",
      "Therefor the complete solution is = cf+pi \n",
      "cf =  1.0**n*c2 + 3.0**n*c1\n",
      "PI=1/(Eˆ2−4E+3)[5ˆn]\n",
      "put E=5\n",
      "We get PI=5ˆn/8 \n",
      "un =  1.0**n*c2 + 3.0**n*c1 + 5**n/8\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy,math\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"Cumulative function  is given by Eˆ2−4∗E+3=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-4*E+3\n",
    "r = numpy.roots([1,-4,3])\n",
    "print r\n",
    "print \"Therefor the complete solution is = cf+pi \"\n",
    "cf = c1*(r[0])**n+c2*r[1]**n \n",
    "print \"cf = \",cf\n",
    "print \"PI=1/(Eˆ2−4E+3)[5ˆn]\"\n",
    "print \"put E=5\"\n",
    "print \"We get PI=5ˆn/8 \"\n",
    "pi = 5**n/8\n",
    "un = cf+pi\n",
    "print \"un = \",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.8, page no. 672"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function is given by Eˆ2−4∗E+4=0 \n",
      "[ 2.  2.]\n",
      "Therefor the complete solution is = cf+pi\n",
      "cf =  2.0**n*(c1 + c2*n)\n",
      "PI=1/(Eˆ2−4E+4)[2ˆn]\n",
      "We get PI=n∗(n−1)/2∗2ˆ(n−2)\n",
      "un =  2**(n - 2)*n*(n - 1)/2 + 2.0**n*(c1 + c2*n)\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy,math\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"Cumulative function is given by Eˆ2−4∗E+4=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-4*E+4\n",
    "r = numpy.roots([1,-4,4])\n",
    "print r\n",
    "print \"Therefor the complete solution is = cf+pi\" \n",
    "cf = (c1+c2*n)*r[0]**n\n",
    "print \"cf = \",cf\n",
    "print \"PI=1/(Eˆ2−4E+4)[2ˆn]\"\n",
    "print \"We get PI=n∗(n−1)/2∗2ˆ(n−2)\"\n",
    "pi = n*(n-1)/math.factorial(2)*2**(n-2)\n",
    "un = cf+pi\n",
    "print \"un = \",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.10, page no. 674"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function is given by Eˆ2−4=0 \n",
      "[-2.  2.]\n",
      "Therefor the complete solution is = cf+pi \n",
      "CF =  (-2.0)**n*(c1 + c2*n)\n",
      " PI=1/(Eˆ2−4)[nˆ2+n−1]\n",
      "We get PI=−nˆ2/3−7/9∗n−17/27 \n",
      "un =  (-2.0)**n*(c1 + c2*n) - n**2/3\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy,math\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"Cumulative function is given by Eˆ2−4=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-4\n",
    "r = numpy.roots([1,0,-4]) \n",
    "print r\n",
    "print \"Therefor the complete solution is = cf+pi \"\n",
    "cf = (c1+c2*n)*r[0]**n\n",
    "print \"CF = \",cf\n",
    "print \" PI=1/(Eˆ2−4)[nˆ2+n−1]\"\n",
    "print \"We get PI=−nˆ2/3−7/9∗n−17/27 \"\n",
    "pi = -n**2/3-7/9*n-17/27\n",
    "un = cf+pi \n",
    "print \"un = \",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.11,page no. 674"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cumulative function is given by Eˆ2−2∗E+1=0 \n",
      "[-2.41421356  0.41421356]\n",
      "Therefor the complete solution is = cf+pi\n",
      "CF =  (-2.41421356237309)**n*(c1 + c2*n)\n",
      "PI=1/(E−1)ˆ2[nˆ2∗2ˆn]\n",
      "We get PI=2ˆn∗(nˆ2−8∗n+20)\n",
      "un =  (-2.41421356237309)**n*(c1 + c2*n) + 2**n*(n**2 - 8*n + 20)\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy,math\n",
    "\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "n = sympy.Symbol('n')\n",
    "print \"Cumulative function is given by Eˆ2−2∗E+1=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2+2*E-1\n",
    "r = numpy.roots([1,2,-1])\n",
    "print r\n",
    "print \"Therefor the complete solution is = cf+pi\"\n",
    "cf = (c1+c2*n)*r[0]**n\n",
    "print \"CF = \",cf\n",
    "print \"PI=1/(E−1)ˆ2[nˆ2∗2ˆn]\"\n",
    "print \"We get PI=2ˆn∗(nˆ2−8∗n+20)\"\n",
    "pi = 2**n*(n**2-8*n+20)\n",
    "un = cf+pi\n",
    "print \"un = \",un"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.12, page no. 676"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Simplified equations are : \n",
      "(E−3) ux+vx=x..... (i) 3ux+(E−5)∗vx=4ˆx......(ii)\n",
      "Simplifying we get (Eˆ2−8E+12) ux=1−4x−4ˆx \n",
      "Cumulative function is given by Eˆ2−8∗E+12=0 \n",
      "[ 6.  2.]\n",
      "Therefor the complete solution is = cf+pi\n",
      "CF =  2.0**x*c2 + 6.0**x*c1\n",
      "Solving for PI \n",
      "We get PI= \n",
      "ux =  2.0**x*c2 + 4**x/4 + 6.0**x*c1 - x\n",
      "Putting in (i) we get vx= \n",
      "2**x*c1 - 4**x/4 - 3*6**x*c2 - 1\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy\n",
    "\n",
    "print \"Simplified equations are : \"\n",
    "print \"(E−3) ux+vx=x..... (i) 3ux+(E−5)∗vx=4ˆx......(ii)\"\n",
    "print \"Simplifying we get (Eˆ2−8E+12) ux=1−4x−4ˆx \"\n",
    "c1 = sympy.Symbol('c1')\n",
    "c2 = sympy.Symbol('c2')\n",
    "c3 = sympy.Symbol('c3')\n",
    "x = sympy.Symbol('x')\n",
    "print \"Cumulative function is given by Eˆ2−8∗E+12=0 \"\n",
    "E = numpy.poly([0])\n",
    "f = E**2-8*E+12\n",
    "r = numpy.roots([1,-8,12])\n",
    "print r\n",
    "print \"Therefor the complete solution is = cf+pi\"\n",
    "cf = c1*r[0]**x+c2*r[1]**x\n",
    "print \"CF = \",cf\n",
    "print \"Solving for PI \"\n",
    "print \"We get PI= \"\n",
    "pi = -4/5*x-19/25+4**x/4\n",
    "ux = cf+pi \n",
    "print \"ux = \",ux\n",
    "print \"Putting in (i) we get vx= \"\n",
    "vx = c1*2**x-3*c2*6**x-3/5*x-34/25-4**x/4\n",
    "print vx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 26.16, page no. 682"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "u2 =  2\n",
      "u3 =  13\n"
     ]
    }
   ],
   "source": [
    "import numpy,sympy\n",
    "\n",
    "z = sympy.Symbol('z')\n",
    "f = (2/z**2+5/z+14)/(1/z-1)**4\n",
    "u0 = sympy.limit(f,z,0)\n",
    "u1 = sympy.limit(1/z*(f- u0),z,0)\n",
    "u2 = sympy.limit(1/z**2*(f-u0-u1*z),z,0)\n",
    "print \"u2 = \",u2\n",
    "u3 = sympy.limit(1/z**3*(f-u0-u1*z-u2*z**2),z,0)\n",
    "print \"u3 = \",u3"
   ]
  }
 ],
 "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
}