# Chapter 26 : Difference Equations And Z Transform¶

## Example 26.2, page no. 667¶

In :
import sympy,numpy

n = sympy.Symbol('n')
a = sympy.Symbol('a')
b = sympy.Symbol('b')
yn0 = sympy.Symbol('yn0')
yn1 = sympy.Symbol('yn1')
yn2 = sympy.Symbol('yn2')
yn = a*2**n+b*(-2)**n
print "yn= ",yn
n = n+1
yn = yn.evalf()
print "y(n+1)=yn1=",yn
n = n+1
yn = yn.evalf()
print "y(n+2)=yn2=",yn
print "Eliminating a b from these equations we get :"
A = sympy.Matrix([[yn0,1,1],[yn1,2,-2],[yn2,4,4]])
y = A.det()
print "The required difference equation : "
print y
print "=0"

yn=  (-2)**n*b + 2**n*a
y(n+1)=yn1= (-2.0)**n*b + 2.0**n*a
y(n+2)=yn2= (-2.0)**n*b + 2.0**n*a
Eliminating a b from these equations we get :
The required difference equation :
16*yn0 - 4*yn2
=0


## Example 26.3, page no. 669¶

In :
import numpy,sympy

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
print "Cumulative function is given by Eˆ3−2∗Eˆ2−5∗E+6=0 "
E = numpy.poly()
f = E**3-2*E**2-5*E+6
r = numpy.roots([1,-2,-5,6])
print r
print "Therefor the complete solution is : "
un = c1*(r)**n+c2*(r)**n+c3*(r)**n
print "un=",un

Cumulative function is given by Eˆ3−2∗Eˆ2−5∗E+6=0
[-2.  3.  1.]
Therefor the complete solution is :
un= (-2.0)**(n + 2)*c1 + 1.0**(n + 2)*c3 + 3.0**(n + 2)*c2


## Example 26.4, page no. 670¶

In :
import numpy,sympy

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "Cumulative function is given by Eˆ2−2∗E+1=0 "
E = numpy.poly()
f = E**2-2*E+1
r = numpy.roots([1,-2,1])
print r
print "Therefor the complete solution is : "
un = (c1+c2*n)*(r)**n
print "un = ",un

Cumulative function is given by Eˆ2−2∗E+1=0
[ 1.  1.]
Therefor the complete solution is :
un =  1.0**n*(c1 + c2*n)


## Example 26.6, page no. 671¶

In :
import numpy,sympy,math

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "For Fibonacci Series yn2=yn1+yn0 "
print "So Cumulative function is given by Eˆ2−E−1=0 "
E = numpy.poly()
f = E**2-E-1
r = numpy.roots([1,-1,-1])
print r
print "Therefor the complete solution is : "
un = (c1)*(r)**n+c2*(r)**n
print "un = ",un
print "Now puttting n=1, y=0 and n=2, y=1 we get "
print "c1=(5−sqrt(5))/10 c2=(5+sqrt(5))/10 "
c1 =(5-math.sqrt(5))/10
c2 =(5+math.sqrt(5))/10
un = un.evalf()
print un

For Fibonacci Series yn2=yn1+yn0
So Cumulative function is given by Eˆ2−E−1=0
[ 1.61803399 -0.61803399]
Therefor the complete solution is :
un =  (-0.618033988749895)**n*c2 + 1.61803398874989**n*c1
Now puttting n=1, y=0 and n=2, y=1 we get
c1=(5−sqrt(5))/10 c2=(5+sqrt(5))/10
(-0.618033988749895)**n*c2 + 1.61803398874989**n*c1


## Example 26.7, page no. 672¶

In :
import numpy,sympy,math

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "Cumulative function  is given by Eˆ2−4∗E+3=0 "
E = numpy.poly()
f = E**2-4*E+3
r = numpy.roots([1,-4,3])
print r
print "Therefor the complete solution is = cf+pi "
cf = c1*(r)**n+c2*r**n
print "cf = ",cf
print "PI=1/(Eˆ2−4E+3)[5ˆn]"
print "put E=5"
print "We get PI=5ˆn/8 "
pi = 5**n/8
un = cf+pi
print "un = ",un

Cumulative function  is given by Eˆ2−4∗E+3=0
[ 3.  1.]
Therefor the complete solution is = cf+pi
cf =  1.0**n*c2 + 3.0**n*c1
PI=1/(Eˆ2−4E+3)[5ˆn]
put E=5
We get PI=5ˆn/8
un =  1.0**n*c2 + 3.0**n*c1 + 5**n/8


## Example 26.8, page no. 672¶

In :
import numpy,sympy,math

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "Cumulative function is given by Eˆ2−4∗E+4=0 "
E = numpy.poly()
f = E**2-4*E+4
r = numpy.roots([1,-4,4])
print r
print "Therefor the complete solution is = cf+pi"
cf = (c1+c2*n)*r**n
print "cf = ",cf
print "PI=1/(Eˆ2−4E+4)[2ˆn]"
print "We get PI=n∗(n−1)/2∗2ˆ(n−2)"
pi = n*(n-1)/math.factorial(2)*2**(n-2)
un = cf+pi
print "un = ",un

Cumulative function is given by Eˆ2−4∗E+4=0
[ 2.  2.]
Therefor the complete solution is = cf+pi
cf =  2.0**n*(c1 + c2*n)
PI=1/(Eˆ2−4E+4)[2ˆn]
We get PI=n∗(n−1)/2∗2ˆ(n−2)
un =  2**(n - 2)*n*(n - 1)/2 + 2.0**n*(c1 + c2*n)


## Example 26.10, page no. 674¶

In :
import numpy,sympy,math

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "Cumulative function is given by Eˆ2−4=0 "
E = numpy.poly()
f = E**2-4
r = numpy.roots([1,0,-4])
print r
print "Therefor the complete solution is = cf+pi "
cf = (c1+c2*n)*r**n
print "CF = ",cf
print " PI=1/(Eˆ2−4)[nˆ2+n−1]"
print "We get PI=−nˆ2/3−7/9∗n−17/27 "
pi = -n**2/3-7/9*n-17/27
un = cf+pi
print "un = ",un

Cumulative function is given by Eˆ2−4=0
[-2.  2.]
Therefor the complete solution is = cf+pi
CF =  (-2.0)**n*(c1 + c2*n)
PI=1/(Eˆ2−4)[nˆ2+n−1]
We get PI=−nˆ2/3−7/9∗n−17/27
un =  (-2.0)**n*(c1 + c2*n) - n**2/3


## Example 26.11,page no. 674¶

In :
import numpy,sympy,math

c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
n = sympy.Symbol('n')
print "Cumulative function is given by Eˆ2−2∗E+1=0 "
E = numpy.poly()
f = E**2+2*E-1
r = numpy.roots([1,2,-1])
print r
print "Therefor the complete solution is = cf+pi"
cf = (c1+c2*n)*r**n
print "CF = ",cf
print "PI=1/(E−1)ˆ2[nˆ2∗2ˆn]"
print "We get PI=2ˆn∗(nˆ2−8∗n+20)"
pi = 2**n*(n**2-8*n+20)
un = cf+pi
print "un = ",un

Cumulative function is given by Eˆ2−2∗E+1=0
[-2.41421356  0.41421356]
Therefor the complete solution is = cf+pi
CF =  (-2.41421356237309)**n*(c1 + c2*n)
PI=1/(E−1)ˆ2[nˆ2∗2ˆn]
We get PI=2ˆn∗(nˆ2−8∗n+20)
un =  (-2.41421356237309)**n*(c1 + c2*n) + 2**n*(n**2 - 8*n + 20)


## Example 26.12, page no. 676¶

In :
import numpy,sympy

print "Simplified equations are : "
print "(E−3) ux+vx=x..... (i) 3ux+(E−5)∗vx=4ˆx......(ii)"
print "Simplifying we get (Eˆ2−8E+12) ux=1−4x−4ˆx "
c1 = sympy.Symbol('c1')
c2 = sympy.Symbol('c2')
c3 = sympy.Symbol('c3')
x = sympy.Symbol('x')
print "Cumulative function is given by Eˆ2−8∗E+12=0 "
E = numpy.poly()
f = E**2-8*E+12
r = numpy.roots([1,-8,12])
print r
print "Therefor the complete solution is = cf+pi"
cf = c1*r**x+c2*r**x
print "CF = ",cf
print "Solving for PI "
print "We get PI= "
pi = -4/5*x-19/25+4**x/4
ux = cf+pi
print "ux = ",ux
print "Putting in (i) we get vx= "
vx = c1*2**x-3*c2*6**x-3/5*x-34/25-4**x/4
print vx

Simplified equations are :
(E−3) ux+vx=x..... (i) 3ux+(E−5)∗vx=4ˆx......(ii)
Simplifying we get (Eˆ2−8E+12) ux=1−4x−4ˆx
Cumulative function is given by Eˆ2−8∗E+12=0
[ 6.  2.]
Therefor the complete solution is = cf+pi
CF =  2.0**x*c2 + 6.0**x*c1
Solving for PI
We get PI=
ux =  2.0**x*c2 + 4**x/4 + 6.0**x*c1 - x
Putting in (i) we get vx=
2**x*c1 - 4**x/4 - 3*6**x*c2 - 1


## Example 26.16, page no. 682¶

In :
import numpy,sympy

z = sympy.Symbol('z')
f = (2/z**2+5/z+14)/(1/z-1)**4
u0 = sympy.limit(f,z,0)
u1 = sympy.limit(1/z*(f- u0),z,0)
u2 = sympy.limit(1/z**2*(f-u0-u1*z),z,0)
print "u2 = ",u2
u3 = sympy.limit(1/z**3*(f-u0-u1*z-u2*z**2),z,0)
print "u3 = ",u3

u2 =  2
u3 =  13