EE2703 Assignment 7-Symbolic Algebra Solved

35.00 $

Category:

Description

Rate this product

 

In this assignment, the focus will be on two powerful capabilities of Python: • Symbolic Algebra
• Analysis of Circuits using Laplace Transforms

Analysis of Circuits using Laplace Transforms

Consider the following figure (from page 274 of Horowitz and Hill):

C1 iR11R2 p

G C2

o

(G-1)R

R

m

whereG=1.586andR1 =R2 =10kΩandC1 =C2 =10pF.Thisgivesa3dBButter- worth filter with cutoff frequency of 1/2πMHz.

The circuit equations are

Vm = Vo (1) G

V =V 1 (2) p 1 1+ jωR2C2

Vo = G(Vp −Vm) (3) Vi−V1 +Vp−V1 +jωC1(Vo−V1)=0 (4)

R1 R2
1

− +

Solving for Vo in 3, we get

Vo = GV1 1
2 1+jωR2C2

Thus, Eq. 4 becomes an equation for V1 as follows: Vi􏰄1111G1 􏰅

R +V1 −R +R 1+jωRC −R +jωC121+jωRC −jωC1 =0 11222222

The term involving G will dominate, and so we obtain V1 ≈ 2Vi 1+ jωR2C2

G jωR1C1 Substituting back into the expression for Vo we finally get

Vo ≈ Vi (5) jωR1C1

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 isympy

    $ isympy
    IPython console for SymPy 1.4 (Python 3.7.3-64-bit) (ground types: gmpy)
    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols(’x y z t’)
    >>> k, m, n = symbols(’k m n’, integer=True)
    >>> f, g, h = symbols(’f g h’, cls=Function)
    >>> init_printing()
    Documentation can be found at https://docs.sympy.org/1.4/

Normally we invoke ipython and import * from pylab. Instead we invoke sympy. It starts up with some pre-defined variables and the Ipython shell and also interactive plotting. Note: when coding these lines have to be included. The string inside the symbols command tells sympy how to print out expressions involving these symbols. For instance

>>> x,y,z = symbols(’x y speed’) >>> x=y+1/z
>>> x
y+ 1

s peed

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
>>> x
ω *y+z

Note that symbols are not python variables:

2

    >>> 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

This can lead to confusion, so be careful.
Rational functions of s are straightforward in sympy

    >>> s,R2,C2=symbols(’s R_2 C_2’)
    >>> h=-1/(1+s*R2*C2)
    >>> print(h)
    -1/(C_2*R_2*s+1)

>>> h -1

C2R2s + 1

We will require to create some matrices. For example

>>> M=Matrix([[1,2],[3,4]]) >>> M
|1 2|
||

|3 4|

creates a 2 × 2 matrix containing

􏰄1 2􏰅 34

If you define a matrix and a column vector you can multiply them as in Matlab using “*”

>>> N=Matrix([5,6]) >>> M*N
|17|
||

|39|

Suppose you want to solve Mx = N. Then we need to use the inverse of M

>>> M.inv()*N |-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.

3

>>> expr=sqrt(8)
>>> expr

22
>>> 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
    >>> ww=p.logspace(-1,1,21)
    >>> ss=1j*w
    >>> f=lambdify(s,h,”numpy”)
    >>> p.loglog(ww,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. Note that the first argument in lambdify, s, is whatever occurs in the definition of h. i.e., it is the symbolic variable inside h. In the loglog line, we actually replace ’s’ by a Numpy array, ’ss’.

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 01−G1V10

−1100V0
 1+sRC  p =   22  

 0 −GG1Vm0 −R1 −R1 −sC1 R1 0 sC1 Vo Vi(s)/R1

122

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. 4

      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=%f’ % G)
Vo=V[3]
print(Vo)
ww=p.logspace(0,8,801)
ss=1j*ww
hf=lambdify(s,Vo,’numpy’)
v=hf(ss)
p.loglog(ww,abs(v),lw=2)
p.grid(True)

p.show()

100

10-1

10-2

10-3
100 101 102 103 104 105 106 107 108

4

1. 2.

The Assignment

Obtain the step response of the circuit above. Use the signal toolbox of last week’s assignment.

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

Vi

C1 C2 R3

R1

G

+

(G-1)R R

Vo

ThevaluesyoucanuseareR1 =R3 =10kΩ,C1 =C2 =1nF,andG=1.586.

  1. 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.
  2. Obtain the response of the circuit to a damped sinusoid (i.e., use suitable Vi). Use signal.lsim to simulate the output.
  3. Obtain the response of the circuit to a unit step function. Do you understand the response? (Just define Vi to be 1/s)

6

  • Assignment-7-jlmxpe.zip