Python Assignment No. 8 Symbolic Algebra….

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

In this assignment, the focus will be on two powerful capabilities of Python:
• Symbolic Algebra
• Analysis of Circuits using Laplace Transforms
1 Analysis of Circuits using Laplace Transforms
Consider the following figure (from page 274 of Horowitz and Hill):

+
R1
C1
G
(G-1)R
R
i 1
o
m
p
R2
C2
where G = 1.586 and R1 = R2 = 10kΩ and C1 = C2 = 10pF. This gives a 3dB Butterworth filter with cutoff frequency of 1/2πMHz.
The circuit equations are
Vm =
Vo
G
(1)
Vp = V1
1
1+ jωR2C2
(2)
Vo = G(Vp −Vm) (3)
Vi −V1
R1
+
Vp −V1
R2
+ jωC1(Vo −V1) = 0 (4)
1
Solving for Vo in 3, we get
Vo =
GV1
2
1
1+ jωR2C2
Thus, Eq. 4 becomes an equation for V1 as follows:
Vi
R1
+V1


1
R1
+
1
R2
1
1+ jωR2C2

1
R2
+ jωC1
G
2
1
1+ jωR2C2
− jωC1

= 0
The term involving G will dominate, and so we obtain
V1 ≈
2Vi
G
1+ jωR2C2
jωR1C1
Substituting back into the expression for Vo we finally get
Vo ≈
Vi
jωR1C1
(5)
We would like to solve this in Python and also get (and plot) the exact result. For this
we need the sympy module.
2 Introduction to Sympy
Start ipython and import the sympy module
$ ipython
>>> from sympy import *
>>> init_session
>>> x,y,z = symbols(’x y speed’)
Normally we invoke ipython and import * from pylab. Instead we invoke sympy. We are
now ready to do symoblic work. The last line allows us to define variables that can be used
for symbolic manipulations. The string inside the symbols command tells sympy how to
print out expressions involving these symbols. For instance
>>> x=y+1/z
>>> x
y+
1
speed
This is confusing, so just use the variable name for the symbol. One place where this is
useful is for greek symbols:
>>> w=symbols(’omega’)
>>> x=w*y+z
omega*y+z
Note that symbols are not python variables:
>>> expr=x+1
>>> print expr
x + 1 # a symbolic expression
>>> x=2 # reassignment of x
>>> print expr
x + 1 # changing x did not change expr
>>> print x
2 # did change x though
>>> expr=x+1
>>> print expr
3 # now expr used current value of x – no longer a symbol
2
This can lead to confusion, so be careful.
Rational functions of s are straightforward in sympy
>>> s=symbols(’s R_2 C_2’)
>>> h=-1/(1+s*R2*C2)
>>> print h
-1/(C_2*R_2*s+1)
We will require to create some matrices. For example
>>> M=Matrix([[1 2],[3 4]])
>>> print M
creates a 2×2 matrix containing

1 2
3 4
If you define a matrix and a column vector you can multiply them as in Matlab using
“*”
>>> N=Matrix([5,6])
>>> M*N
Matrix([
[17],
[39]])
Suppose you want to solve Mx = N. Then we need to use the inverse of M
>>> M.inv()*N
Matrix([
[-4],
[9/2]])
Finally, to link to the Python we already know, we need to be able to generate numbers out
of symbols. For that we use evalf.
>>> expr=sqrt(8)
>>> print expr
2*sqrt(2)
>>> expr.evalf()
2.82842712474619
This works for single numbers. But what if we want to plot the magnitude response of a
laplace transform expression? We use something called lambdify
>>> h=1/(s**3+2*s**2+2*s+1)
>>> import pylab as p
>>> w=p.logspace(-1,1,21)
>>> ss=1j*w
>>> f=lambdify(x,h,”numpy”)
>>> p.loglog(w,abs(f(ss)))
>>> p.grid(True)
>>> p.show()
What lambdify does is it takes the sympy function “h” and converts it into a python function
which is then applied to the array ’ss’. This is much faster than iterating over the elements
of ss and using evalf on the elements.
3
3 Using Sympy to solve our circuit problem
We can solve directly for the exact result from Python. Let us define s = jω, and rewrite
the equations in a matrix equation


0 0 1 −
1
G

1
1+sR2C2
1 0 0
0 −G G 1

1
R1

1
R2
−sC1
1
R2
0 sC1




V1
Vp
Vm
Vo

 =


0
0
0
Vi(s)/R1


This is the equation that we create and solve for now. The following function both defines
the matrices and solves for the solution.
from sympy import *
import pylab as p
def lowpass(R1,R2,C1,C2,G,Vi):
s=symbols(’s’)
A=Matrix([[0,0,1,-1/G],[-1/(1+s*R2*C2),1,0,0], \
[0,-G,G,1],[-1/R1-1/R2-s*C1,1/R2,0,s*C1]])
b=Matrix([0,0,0,Vi/R1])
That defines the coefficient matrices. Note that I did not include Vi(s) in the vector b. I
multiply it in later. However, I could have included it here as well. Now we solve for the
solution.
V=A.inv()*b
This solution is in s space.
Now extract the output voltage. Note that it is the fourth element of the vector. The
logic of this line is explained below the code.
return (A,b,V)
We now use this function to generate the plot for the values mentioned above. Note that
the input voltage is a unit step function.
A,b,V=lowpass(10000,10000,1e-9,1e-9,1.586,1)
print ’G=1000’
Vo=V[3]
print Vo
w=p.logspace(0,8,801)
ss=1j*w
hf=lambdify(s,Vo,’numpy’)
v=hf(ss)
p.loglog(w,abs(v),lw=2)
p.grid(True)
p.show()
4
10
0 10
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
-3
10
-2
10
-1
10
0
4 The Assignment
1. Obtain the step response of the circuit above. Use the signal toolbox of last week’s
assignment.
2. The input is
vi(t) =
sin(2000πt) +cos
2×106
πt
u0(t) Volts
Determine the output voltage v0(t)
Consider the following circuit (from page 274 of Horowitz and Hill)
5

+
C2
R3
R1
G
(G-1)R
R
Vi
Vo
C1
The values you can use are R1 = R3 = 10kΩ, C1 = C2 = 1nF, and G = 1.586.
3. Analyse the circuit using Sympy as explained above, i.e., create a function similar to
the one defined above. For the same choice of components and gain, this circuit is a
highpass filter.
4. Obtain the response of the circuit to a damped sinusoid (i.e., use suitable Vi). Use
signal.lsim to simulate the output.
5. Obtain the response of the circuit to a unit step function. Do you understand the
response? (Just define Vi to be 1/s)
6