ECEN 3300 Linear Systems Spring 2010 1-18-10 P. Mathys Lab 1: First Order CT Systems, Blockdiagrams, Introduction to Simulink 1 Introduction Many continuous time (CT) systems of practical interest can be described in the form of differential equations. One way to represent such systems is in the form of block diagrams. Originally, differential equations were often solved using analog computers that implemented these block diagrams. Nowadays tools, such as Simulink, are used to obtain numerical solutions to differential equations on digital computers, but still based on block diagram descriptions. Thus, it is important to learn how to represent analog circuits in the abstract form of block diagrams and how to obtain results for different CT input signals. 1.1 First Order RC Circuit Consider the following first order RC circuit with input voltage x(t): i(t) R y R (t) x(t) C y C (t) The voltage across the capacitor is y C (t) and the voltage across the resistor is y R (t). From x(t) = y C (t) y R (t) it follows that y R (t) = x(t) y C (t). The current i(t) can be computed as i(t) = x(t) y C(t) = C y (1) C (t), where y(1) C R (t) = dy C(t). dt Thus, the circuit is characterized by the linear differential equation RC y (1) C (t) y C(t) = x(t), with time constant T c = RC, and initial condition y C (0). A convenient way to represent differential equations is in the form of block diagrams that use addition, subtraction, gains (i.e., multiplication by a constant), and integrators. The symbol for an integrator with input x(t), output y(t), and initial condition y(0) at t = 0 is shown in the following figure. 1
x(t) y(t) t y(t) = x(τ) dτ y(0) 0 y(0) To use this as a building block for the representation of differential equations, note that if the output of the integrator is y C (t), then its input (for t > 0) must be y (1) C (t) as shown below. y (1) C (t) y C(t) y(0) But the differential equation RC y (1) C (t) y C(t) = x(t) can be rewritten as y (1) C (t) = 1 RC [x(t) y C(t)]. Using this together with the integral block yields the block diagram x(t) 1 RC y (1) C (t) y C (t) y C (0) This is the block diagram of an analog computer with input x(t) and output y C (t) that can be used to simulate the RC circuit for different values of RC and different initial conditions y C (0). Another way to express this is to say that the analog computer defined by the block diagram solves the differential equation RC y (1) C (t) y C(t) = x(t) for the output variable y C (t). A second variable of interest in the RC circuit is the resistor voltage y R (t). Note that from i(t) = y R(t) R = C y(1) C (t) = y R(t) = RC y (1) C (t). Thus, the same analog computer can be used to simultaneously compute y R (t) and y C (t) as shown in the following block diagram. 2
RC y (1) C (t) y R (t) x(t) 1 RC y (1) C (t) y C (t) y C (0) 1.2 Simulink Simulink is a software package that simulates the bevaior of a dynamic system at successive time instants within a given range. The dynamic system is typically described in the form of a blockdiagram that graphically depicts the relationships between system inputs, outputs, and states. Simulink is closely integrated with Matlab and system parameters, as well as input, output and state sequences, can be easily imported from and exported to Matlab, e.g., to use Matlab s powerful graphing capabilities. Simulink can be used to simulate both continuous time (CT) and discrete time (DT) systems and combinations thereof. The process of solving a model involves computing successive system states depending on the initial state and the input signals, and then to use this data to compute the output signals, all in accordance with the blockdiagram description of the system. For CT systems approximations, such as y((n 1)T s ) = (n1)ts 0 x(τ) dτ y(nt s ) T s x(nt s ), which is based on Euler s forward approximation, need to be used to update the outputs or states y(t) of all integration blocks in a model a time instants spaced T s seconds apart. More sophisticated methods use better approximations for the area under x(t) than the rectangular stripes that Euler s method uses. In this way more precise results can be obtained with the same step size T s, or the simulation time can be reduced by increasing T s and/or choosing it adaptively. To start Simulink click on this icon or type simulink at the Matlab command prompt. This brings up the Simulink Library Browser shown below from which you select and then drag and drop individual blocks to your model window. 3
Most of the blocks needed for ECEN 3300 are in the Commonly Used Blocks library shown in the next figure. Note that the label for the integrator block is 1 s rather than, because of the Laplace transform property t 0 x(τ)dτ 1 s X(s). 4
1.3 Simulink Simulation of a First Order System Start Simulink (type simulink in the Matlab workspace or click on ) to open the Simulink Library Browser. Click on File, then select New and Model and drag the following blocks from the Library Browser into the model window. The Step block is from the Sources library and all other blocks are from the Commonly Used Blocks library. The Sum block is set up for addition of two input signals when it comes from the library. To make it into a subtractor, select the block by clicking on it. Then right-click on it and select Sum Parameters... as shown next. Change the List of signs entry from to - as shown below. 5
To interconnect two blocks, click on the first block to select it, then hold down the Ctrl key and click on the second block. Alternatively, drag a wire from one block to another by holding down the left mouse key while moving the cursor from one connection to the other. The feedforward connections for the first order model are shown in the following figure. To make the feedback connection from the output of the integrator to the subtractor, start a wire from the - input of the subtractor as shown next. 6
Then connect the end of this wire to the wire that runs from the integrator to the scope block. The finished first order block diagram looks like this: Save the model by clicking on File and selecting Save. To run a simulation click on Simulation and select Start as shown in the next figure. 7
Double click on the Scope block to see the result of the simulation as displayed in the following screen snapshot. As expected, the step response at the output of the integrator of a first order system with zero intial conditions is of the form g(t) = A (1 e t/tc, where A is the amplitude and T c is the time constant of the system. To obtain a better Scope picture, right-click on the Scope graph and select Axes Properties.... Change the Y-min and the Y-max values and enter a graph title as shown below. Now the Scope graph looks like this: 8
One thing that is missing in this graph is the input signal from the Step source. To show more than one signal on the Scope, use a Mux (multiplexer) block as shown in the new model below. If necessary, the number of inputs for the Mux block can be increased under Mux Parameters... (select the block, then right click on it). If the model above is simulated with the default parameters, then the result displays as follows on the Scope. 9
To look at the (default) parameters that were actually used in the simulation, click on Simulation and then choose Configuration Parameters. This brings up the window shown below. The default start and stop times are 0.0 and 10.0. The default solver is a variable-step solver that attempts to optimize the tradeoff between speed and accuracy of the solution. The alternative choice is a fixed-step solver which needs to be used for instance to produce results with a specified fixed sampling rate. In either case a numerical integration technique needs to be specified if the system to be modeled contains CT components. The default variable-step solver is ode45 as shown in the above solver window. For fixed-step simulations the default solver is ode3. Both of these work well for most of the systems that will be simulated in ecen3300. 1.4 Displaying Simulation Results in MATLAB One of the problems when working directly in Simulink is that the graphing capabilities of the Scope block are fairly limited. There is a good reason for this, namely the fact that data can be passed very easily from Simulink to MATLAB and therefore any postprocessing and graphing of data from a simuation can make use of the power of MATLAB. To export data from a Simulink model output ports are used like the Out1 block shown in the following augmented version of the first order CT system model. 10
Before running the simulation of this model (with default parameters), check the Data Import/Export settings under Simulation, Configuration Parameters... and make sure the boxes next to Time: tout and Output: yout are checked and the box next to Limit data points to last: 1000 is unchecked (red circle), as shown in the screen snapshot below. Then, after runnung the simulation in Simulink, use the MATLAB commands 11
plot(tout,yout(:,1),.-b,tout,yout(:,2), o-r ) %tout and yout are column vectors %yout has two columns here grid title( Step Response of 1 st order circuit, Gain=1 ) ylim([-0.2,1.2]) xlabel( Time t [sec] ) ylabel( x(t), y(t) ) legend( x(t), y(t),4) to produce the following more informative graph. Step Response of 1 st order circuit, Gain=1 1 0.8 x(t), y(t) 0.6 0.4 0.2 0 x(t) y(t) 0.2 0 1 2 3 4 5 6 7 8 9 10 Time t [sec] Type help plot at the MATLAB command prompt to learn more about making plots in MATLAB and labeling them. As can be seen from the graph, the points (red circles) for which the output was computed by Simulink are quite evenly spaced. If you look at the variables tout and yout in the MATLAB workspace, then you can see that at t = 1 where the step in the input signal occurs there are three samples very close together, one just before and two just after the step. 2 Prelab Questions P1. First Order Step Response. For the differential equation RC y (1) C (t) y C(t) = x(t), with initial condition y C (0), determine y C (t) and y R (t) = RC y (1) C (t) in response to x(t) = A x u(t). Sketch and label y C (t) and y R (t). 12
P2. Block Diagram Using Differentiator. Show how to implement the differential equation RC y (1) C (t) y C(t) = x(t), y R (t) = RC y (1) C (t), in the form of a block diagram that uses only adders, subtractors, gains, and differentiators (d/dt blocks). Show the input x(t) and both outputs, y C (t) and y R (t), in your block diagram. P3. Approximate Differentiator Using Integrator. Show how the following block diagram which uses an integrator can be used as an approximate differentiator (for bandlimited signals). x(t) 1 RC y (1) C (t) y C (t) y C (0) = 0 How does RC have to be chosen and which signal would be used as output? How would you test that the system indeed acts as a differentiator? 3 Lab Experiments E1. Step/Impulse Response in Simulink. The goal of this experiment is to simulate the RC circuit i(t) R y R (t) x(t) C y C (t) for different values of RC and to make plots of the unit step responses of y C (t) and y R (t), and the unit impulse response of y C (t). (a) Build the following model in Simulink: 13
Run this model for RC = 0.1, 1, 10 and make labeled plots of x(t), y C (t), and y R (t) using the MATLAB plot command. The default variable-step solver (ode45) should work fine for this. You may have to change the stop time of the simulation and the time at which the unit step occurs to obtain characteristic graphs. Also make sure to uncheck the Limit data points to last: 1000 option under Simulation, Configuration Parameters... in the Data Import/Export window. From the plots of y C (t) determine the system rise time t r, defined as the time it takes for the unit step response to go from 10% to 90% of its final value. From your observations, can you tell how t r and RC are related? (b) To obtain the unit impulse response h C (t) of the voltage y C (t) across the capacitor, the derivative of the step response can be taken since δ(t) = du(t)/dt and the RC circuit is linear and time-invariant (LTI). The corresponding Simulink model looks as follows. To run a simulation for this model use a fixed-step solver with fixed-step size of 0.01 as shown in the figure below. 14
Note that if RC = 1 then the unit impulse response of y C (t) is the same as the unit step response of y R (t), so the result obtained from this Simulink model can be checked easily. Before you run the simulation, make sure to uncheck the Limit data points to last: 1000 option under Simulation, Configuration Parameters... in the Data Import/Export window. Then plot and label the result of the simulation in Matlab. You may have to adjust the scaling of the graph to obtain meaningful results or use two separate graphs for yout(:,1) and yout(:,2). Try changing the fixed-step size to 0.1 and observe if and how that changes the result. (c) It is tempting to think that it is better to obtain the unit impulse response h C (t) of y C (t) by actually using a unit impulse as input for the simulation. Try the following Simulink model and run it with the same parameters that you used in part(b). Don t be surprised if the result is not quite right. To obtain the correct result, try other fixed-step solvers (e.g., ode5, ode4, etc). Which ones do work, which ones don t? Does 15
the model that you used in (b) show the same sensitivity when you use different fixed-step solvers? What conclusions can you draw? E2. Sinusoidal Response in Simulink. The intent of this experiment is to characterize the RC circuit shown in E1 in terms of its frequency selective behavior. Use the following Simulink model: This is the same model as the one used in E1a, except that the source is now a sinusoidal signal ( Sine Wave from the Sources library). If you run this model with the default settings (variable-step ode45 solver) and the Stop time set to 20.0, then you should obtain a graph in MATLAB (use plot(tout,yout), grid) similar to the one shown below. 16
Note that, for the settings and frequencies used here, it takes about 3-4 seconds before the sinusoidal outputs (especially the one from y R (t)) reach their steady-state values. Thus, when you need to measure the (steady-state) amplitude and phase of y R (t) and y C (t) in response to a sinusoidal input, you need to look at the waveforms after the initial transients have died out. Another thing to observe from the graph above is that even in steady-state the sinusoids don t look very smooth. This is a consequence of the fact that Simulink tries to obtain accurate solution points as quickly as possible and the variable-step solver uses larger time increments between solution points when nothing big happens. To obtain smoother looking plots you can increase the Refine factor (under Simulation, Configuration Parameters... in the Data Import/Export window), e.g., from 1 to 10 as shown in the screen snapshot below. Now, after adding labels and a legend, the graph looks like this: 17
(a) The input signal for the Simulink model shown above is x(t) = sin(ωt). Determine the amplitude and the phase of the sinusoidal steady-state response of y C (t) and y R (t) for RC = 1 and ω = 0.1, 1, 10 rad/sec. To change the frequency of the Sine Wave source, doubleclick on it and then enter the value (in rad/sec) in the dialog box under Parameters and Frequency. To measure the phase shift of y C (t) and y R (t) with respect to x(t), determine the fraction of a whole period by which the output signal is delayed or advanced. You may have to adjust the Stop time of the simulation to obtain characteristic plots of the steady-state response. Based on your measurements, which of the outputs acts as highpass filter (HPF) and which acts as lowpass filter (LPF)? How can you tell from your sinusoidal steady-state response measurements that this is a first order system? (b) The Simulink model shown below was used to obtain the responses y C (t) and y R (t) of the RC circuit to a composite input signal x(t). 18
The results of the simulation are shown in the following plot. 1.5 Simulation of RC Circuit with Composite Input Signal 1 0.5 y R (t) 0 0.5 1 1.5 0 1 2 3 4 5 6 7 8 9 10 t [sec] 1.5 1 0.5 y C (t) 0 0.5 1 1.5 0 1 2 3 4 5 6 7 8 9 10 t [sec] Determine the source and the gain K that was used for the simulation. Recreate the simulation and make a labeled plot of the signal x(t). Is the solution unique? Explain your approach! E3. Approximate Differentiator from Integrator. Use the following blockdiagram and your solution from prelab problem 3 to set up the Simulink model of an approximate differentiator made from an integrator block. 19
x(t) 1 RC y (1) C (t) y C (t) y C (0) = 0 Your differentiator has to be essentially perfect for signals with frequencies of 10 rad/sec or less and the gain at 10 rad/sec must be equal to 1. Show your Simulink model and explain your design methodology. Explain how you tested your model and how you made sure that it satisfies the design requirements. What is the largest frequency for which your differentiator makes an error of no more than about 10%? Specify the criteria that you used. c 2002 2010, P. Mathys. Last revised: 01-20-10, PM. 20