Anda di halaman 1dari 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 our hand at Fourier Transforms.

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 nite power series in a variable: f = 3 + 4x2 + 6x6 We already know how to approach this function by creating a vector of values for x and obtaining 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 rst 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 dont have a set of values for x. This is now a symbolic representation of f in terms of x. Scilab answers as follows: 2 6 3 + 4x + 6x Rational functions are ratios of polynomials. They are dened the same way: x=poly(0,x); g=(3+4*x^3)/(1-x^4); disp(g); Scilab now answers: 1

3 3 + 4x -----4 1 - 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 1 3 + 4x -----4 1 - x 3 6 9 + 24x + 16x ------------4 8 1 - 2x + x

1 1

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 coefcients have appeared. These are really zero. What this means is that the polynomials are symbolic objects. But the operations between polynomials involves operations on coefcients. These are done in normal, nite precision, arithmetic. Cleaning up, we get: 4 - 1 + x --------3 4 2 + 4x + x 4 8 - 1 + 2x - x ---------------------3 4 6 7 - 6 - 20x - 3x - 16x - 4x We can verify by multiplying the matrices: A*y 2

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

Scilab answers: 0 1 1 1 Above I said cleaning up. Unfortunately, it is not easy to clean up since a rational function 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 arrays 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 functions grow 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 rst. 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.

Analysis of Circuits using Laplace Transforms

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

C1

R1

R2

p + C2 m G o (G-1)R

Our equations are Vo G 1 Vp = V1 1 + j R2C2 Vo = G(Vp Vm ) Vi V1 Vp V1 + + j C1 (Vo V1 ) = 0 R1 R2 Solving for Vo in 3, we get GV1 1 Vo = 2 1 + j R2C2 Thus, Eq. 4 becomes an equation for V1 as follows: Vm = Vi 1 1 1 1 G 1 + V1 + + j C1 j C1 R1 R1 R2 1 + j R2C2 R2 2 1 + j R2C2 The term involving G will dominate, and so we obtain V1 2Vi 1 + j R2C2 G j R1C1 Vi j R1C1 =0 (1) (2) (3) (4)

Substituting back into the expression for Vo we nally get Vo (5)

We can solve directly for the exact result from Scilab. Let us dene s = j , and rewrite the equations in a matrix equation 1 0 0 1 G V1 0 1 1+sR 1 0 0 0 Vp = 2C2 Vm 0 0 G G 1 1 1 1 Vo Vi (s)/R1 R R sC1 R 0 sC1
1 2 2

This is the equation that we create and solve for now. The following function both denes the matrices and solves for the solution. 4

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 denes the coefcient 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\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 dene 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

-->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 -->x(2) ans = 1 + s -->x(3) ans = 2 1 + s + s

dt

The rst line denes the variable that is used to build the polynomial. The second command denes 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 dene Vi (s) and call the function. Let us look at the impulse response of the circuit, with R1 = 1K , R2 = 1K , C1 = 1 F , C2 = 1 F , gain G = 1.07 and Vi = 1. Vo=lowpass(1000,1000,1e-6,1e-6,1.07,1) Vo = 0.00107 ------------------------------- 0.002 - 0.0000049s - 2.000E-09s
Magnitude 50 0 50 100 150 200 250 300 350 400 450

db

10

10 Hz Phase

10

10

200 150 degrees 100 50 0 50 100


0 3 6 9

10

10 Hz

10

10

Since G 1, the theoretical (blue) curve does not follow the exact solution. The exact solution corresponds to a low-pass Butterworth lter, with a cutoff = 103 rad/sec. Let us try the same lter 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
Magnitude 50 0 50 100 db 150 200 250 300 350
0 3 6 9

10

10 Hz Phase

10

10

270 260 250 240 230 220 210 200 190 180 170

degrees

10

10 Hz

10

10

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

0.6379262 ------------179.63573 + s ans(3) - 0.1029262 ------------1113.3643 + s The answer tells us that there is a DC gain to the conguration, together with two exponentially decaying transients. The transients have inverse time constants of 179.6 and 1113 radians per second, respectively. Note: pfss is a Scilab function that analyses a rational function and breaks it into simple components. If you do not nd it simplifying, give a large second argument. Note that it does not always work! It has trouble with the second case above. High gain appears to 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.

The Assignment
1. Consider the following RLC lter.

1.0 uF 1.0 k 5 Vp-p 0 Voffset 1 kHz

1.0 uH

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 response). The pulse width can be taken to be 25 msec.

4.0 vin Vpp=1 pulses

80.96 mH v1

80.96 mH v2

vout 4.0

2.485 mF

Note: You will have to do the nal step back to time domain manually, i.e., pfss only gives you the partial fraction decomposition. From that you will have to write down the time function corresponding to each term. 3. Consider the following circuit (from page 274 of Horowitz and Hill)

R1

Vi

C1

C2

+ G (G-1)R R Vo

R2

(a) Analyse the circuit using Scilab, i.e., create a function similar to the one dened above. For the same choice of components and gain, this circuit is a highpass lter. (Use a gain of 1000). (b) Obtain the time response of the circuit to a unit step function. Do you understand the response? (c) Obtain the time response of the circuit to a damped sinusoid vi (t ) = e50t sin (2000t ) u(t ) To do these inversions, use the pfss function and then invert each term manually.

Anda mungkin juga menyukai