scilab6

download scilab6

of 9

Transcript of scilab6

  • 8/13/2019 scilab6

    1/9

    Scilab Week 6

    September 22, 2009

    In this assignment, the focus will be on two powerful capabilities of Scilab:

    •  Polynomials and their manipulations.

    •  Symbolic Laplace Transforms and their inverses

    Using these, we will analyse the time response of an AC circuit. Next week we will try ourhand at Fourier Transforms.

    1 Polynomials

    Polynomials and rational functions are fundamental data objects in Scilab, just like complex

    numbers or matrices. You can treat them as numbers for most purposes. That means you

    can add, subtract, multiply, divide etc, polynomials.

    A polynomial is a finite power series in a variable:

     f  = 3 + 4 x2 + 6 x6

    We already know how to approach this function by creating a vector of values for  x  andobtaining the value of   f  at each of those values:

    x=linspace(-1,1,101);

    f=3+4*x.^2+6*x.^6;

    plot2d(x,f)

    But many powerful possibilities exist if we can deal with these entities as symbolic objects.

    For that, we must first declare our variable and then build the polynomial in terms of it:

    x=poly(0,”x”);

    f=3+4*x^2+6*x^6;

    disp(f)

    Note that I am not plotting   f  since I don’t have a set of values for x. This is now a symbolicrepresentation of   f  in terms of  x. Scilab answers as follows:

    2 6

    3 + 4 x + 6 x

    Rational functions are ratios of polynomials. They are defined the same way:

    x=poly(0,”x”);

    g=(3+4*x^3)/(1-x^4);

    disp(g);

    Scilab now answers:

    1

  • 8/13/2019 scilab6

    2/9

    3

    3 + 4 x

    ------

    41 - x

    These polynomials and rational functions are elementary objects in scilab. So I can actually

    create a matrix of these objects:

    A=[1 g;1 g^2];

    disp(A);

    which yields

    3

    1 3 + 4x

    - ------

    41 1 - x

    3 6

    1 9 + 24x + 16x

    - -------------

    4 8

    1 1 - 2x + x

    I can now solve a system of equations using this matrix:

    b=[0;1];

    y=A\b;

    disp(y);

    to get

    2 3 4

    - 1 + 3.646E-16x - 3.326E-16x + 7.115E-17x + x ----------------

    Some common sense is required here. The multiplications and divisions done by Scilab

    have resulted in round off errors and some very small coefficients have appeared. These

    are really zero.  What this means is that the polynomials are symbolic objects. But the

    operations between polynomials involves operations on coefficients. These are done in

    normal, finite precision, arithmetic.  Cleaning up, we get:

    4

    - 1 + x---------

    3 4

    2 + 4 x + x

    4 8

    - 1 + 2x - x

    ----------------------

    3 4 6 7

    - 6 - 2 0 x - 3 x - 1 6 x - 4 x

    We can verify by multiplying the matrices:

    A*y

    2

  • 8/13/2019 scilab6

    3/9

    Scilab answers:

    0

    -1

    1

    -

    1

    Above I said “cleaning up”. Unfortunately, it is not easy to clean up since a rational func-

    tion array is not stored as expected. A rational function is actually a “list” consisting four

    entities. The second and third are the numerator and denominator. An array of rational

    functions also results in a list of four objects. But the second and third elements are now ar-

    rays of numerators and denominators respectively. So we must actually store the corrected

    values in y(2)(1) and  y(3)(1). Here is the code for that:

    y(2)(1)=-1+x^4;y(3)(1)=2+4*x^3+x^4;

    disp(y);

    This is all well and good. But at some point we want to go back. We might want to evaluate

    the rational function or the polynomial at a set of x values. The function to do this is called

    horner. Suppose we want to plot the solutions between  x = 0.1 and x = 3. The functionsgrow strongly, so we will use a logarithmically spaced set of points and plot in a log-log

    plot.

    X=logspace(-1,log10(3),30)’;

    Y=horner(y’,X);

    plot2d(X,abs(Y),logflag=”ll”);

    Notice the single quotes above. A single quote next to a vector transposes it. So I have

    created a 30 by 1 column vector for  X  and evaluate  y’  on it. Why  y’? Because if  y  were

    used, I would get two column vectors, one on top of the other to get a 60 by 1 column

    vector. What I want is a 30 element column vector for each element of  y. So I convert y to

    a row vector first. As a result  Y  is a 30 by 2 matrix, and  plot2d gives me 2 curves.

    We use all this in the next section when we do Laplace transform analysis of a circuit

    to obtain its impulse response.

    2 Analysis of Circuits using Laplace Transforms

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

    3

  • 8/13/2019 scilab6

    4/9

    +R1

    C1

    G

    (G-1)R

    R

    1io

    m

    pR2

    C2

    Our equations are

    V m = V o

    G(1)

    V  p = V 11

    1 + jω  R2C 2(2)

    V o = G(V  p −V m)   (3)

    V i −V 1

     R1 +

     V  p −V 1

     R2 + jω C 1(V o−V 1) = 0 (4)

    Solving for V o  in 3, we get

    V o = GV 1

    2

    1

    1 + jω  R2C 2Thus, Eq. 4 becomes an equation for  V 1 as follows:

    V i

     R1+V 1

    1

     R1+

      1

     R2

    1

    1 +  jω  R2C 2−

    1

     R2+ jω C 1

    G

    2

    1

    1 + jω  R2C 2−  jω C 1

    = 0

    The term involving G  will dominate, and so we obtain

    V 1 ≈2V i

    G

    1 + jω  R2C 2 jω  R1C 1

    Substituting back into the expression for V o

     we finally get

    V o ≈V i

     jω  R1C 1(5)

    We can solve directly for the exact result from Scilab. Let us define  s =   jω , and rewrite theequations in a matrix equation

    0 0 1   − 1

    G

    −  1

    1+sR2C 21 0 0

    0   −G G   1

    −  1 R1

    −  1 R2

    − sC 11

     R20   sC 1

    V 1V  pV mV o

    =

    0

    0

    0

    V i(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.

    4

  • 8/13/2019 scilab6

    5/9

    function [Vo]=lowpass(R1,R2,C1,C2,G,Vi)

    s=poly(0,"s"); // This is our $j\omega$.

    A=[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=[0;0;0;1/R1];

    That defines the coefficient matrices. Note that I did not include V i(s)  in the vector  b. Imultiply it in later. However, I could have included it here as well. Now we solve for the

    solution.

    V=A\b;

    Note that this is a symbolic solution, i.e., it is the exact  solution in Laplace domain. To get

    the frequency response, we need to treat it as a fourier transform, i.e., evaluate the answer

    along the   jω  axis. First we define the frequency axis, going from 1 to 108 Hz.

    f=logspace(0,8,801);

    w=2*%pi*%i*f;

    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.

    Vo=Vi*V(2)(4)/V(3)(4);

    We now evaluate the  s-domain solution at the desired frequencies. The command for that

    is “horner”.

    v=horner(Vo,w);

    The output response is complex. We convert it into magnitude and phase for easier plotting.

    [phi,db]=phasemag(v);

    We also have a theoretical expression. We evaluate that as well, and get its magnitude and

    phase.

    v1=horner(Vi/(s*R1*C1),w);

    [phi1,db1]=phasemag(v1);

    Now open the graphics window and clear it.

    xset("window",0);

    xselect();

    xset("wpos",0,0);

    xbasc(0);

    Plot both the exact and the theoretical approximation in a standard plot, known as the Bode

    plot (you will learn about this in your control engineering course).

    bode(imag([f; f]),[db; db1],[phi; phi1]);

    Note that the black lines in the plot are the exact solution while the blue lines correspond

    to Eq. 5.

    endfunction

    The solution, V, is a vector of rational functions. A rational function is stored as a “list”.

    5

  • 8/13/2019 scilab6

    6/9

    -->s=poly(0,"s")

    s = s

    -->x=(1+s)/(1+s+s^2)

    x = 1 + s---------

    2

    1 + s + s

    -->x(1)

    ans = !r num den dt !

    -->x(2)

    ans = 1 + s

    -->x(3)

    ans = 2

    1 + s + s

    The first line defines the variable that is used to build the polynomial. The second command

    defines the rational function. The numerator is in  x(2) and the denominator is in x(3).In the code above,   V   is a vector of rational functions. For reasons that are not clear

    to me, Scilab stores the information as follows:   V(2)  contains an array of numerator

    polynomials.   V(3)  contains an array of denominator polynomials. That is why, to get

    the output voltage, we want the fourth element of   V(2)  and   V(3), i.e.,   V(2)(4)  and

    V(3)(4).

    To use this function, we define V i(s)  and call the function. Let us look at the impulseresponse of the circuit, with  R1 =  1K Ω, R2 =  1K Ω, C 1 =  1µ F , C 2 = 1µ F , gain  G = 1.07and V i = 1.

    Vo=lowpass(1000,1000,1e-6,1e-6,1.07,1)

    Vo = 0.00107

    -------------------------------

    - 0.002 - 0.0000049s - 2.000E-09s

    0

    103

    106

    109

    10

    −450

    −400

    −350

    −300

    −250

    −200

    −150

    −100

    −50

    0

    50

    Magnitude

    Hz

            d        b

    0

    103

    106

    109

    10

    −100

    −50

    0

    50

    100

    150

    200

    Phase

    Hz

            d      e      g      r      e      e      s

    6

  • 8/13/2019 scilab6

    7/9

    Since G ≈ 1, the theoretical (blue) curve does not follow the exact solution. The exact

    solution corresponds to a low-pass Butterworth filter, with a cutoff  ω  =  103rad/sec.Let us try the same filter with  G = 104:

    s=poly(0,"s");

    Vo=lowpass(1000,1000,1e-6,1e-6,10000,1/s)

    Vo =

    10

    ------------------------------

    2 3

    - 0.002s + 0.009994s - 2.000E-09s

    0

    103

    106

    109

    10

    −350

    −300

    −250

    −200

    −150

    −100

    −50

    0

    50

    Magnitude

    Hz

            d        b

    0

    103

    106

    109

    10

    170

    180

    190

    200210

    220

    230

    240

    250

    260

    270

    Phase

    Hz

            d      e      g      r      e      e      s

    Here, the theoretical result and the exact solution agree very well.

    Having the s-domain response is all very well and good. But that does not tell us what

    the time-domain function will do. For that, we need to break up the function into partial

    fractions. Let us do that for the Butterworth solution above, for an input that is a unit step

    function:

    s=poly(0,"s");

    Vo=lowpass(1000,5000,1e-6,1e-6,1.07,1/s);

    pfss(Vo)

    ans =

    ans(1)

    - 0.535

    -----

    s

    ans(2)

    7

  • 8/13/2019 scilab6

    8/9

    0.6379262

    -------------

    179.63573 + s

    ans(3)

    - 0.1029262

    -------------

    1113.3643 + s

    The answer tells us that there is a DC gain to the configuration, together with two expo-

    nentially decaying transients. The transients have inverse time constants of 179 .6 and 1113radians per second, respectively.

    Note:   pfss  is a Scilab function that analyses a rational function and breaks it into

    simple components. If you do not find it simplifying, give a large second argument. Note

    that it does not always work! It has trouble with the second case above. High gain appearsto creates too stiff a matrix to analyse for the pfss routine.

    Scilab is limited in its ability to do symbolic calculus. For example, it can only have one

    symbolic variable. Matlab is better though you have to buy the correct symbolic toolbox,

    but Mathematica is far, far better than either.

    3 The Assignment

    1. Consider the following RLC filter.

    1.0uF

    1   . 0  k  Ω 1.0uH

    5Vp-p0Voffset1kHz

    Create the set of equations for the circuit. Solve for and plot the current in the cirucit

    vs. frequency.

    2. Obtain vout(t ) for a pulse input (a pulse is the difference of two unit step functions).Use superposition and add delayed copies of the step response to get the pulse re-

    sponse). The pulse width can be taken to be 25 msec.

    Vpp=1

    pulses

    4.0Ω   80.96 mH 80.96 mH

    2.485 mF 4.0Ω

    vin   voutv1 v2

    8

  • 8/13/2019 scilab6

    9/9