ME 344 Fall 2012 Transfer Function Simulation in MATLAB (Linear Time-Invariant System Simulation – Part I) The basic installation of MATLAB contains a number of functions for numerically solving ordinary differential equations (ode45, for example). However, the Control Systems Toolbox adds a number of functions designed specifically to handle linear, time-invariant (LTI) systems that are represented in transfer function or state space form, as well as converting between these forms. This handout briefly describes commands related to finding the response of a system represented by a transfer function to various inputs. Additional information, including examples, can be found in Section 3.8 of the textbook (System Dynamics, 2nd Edition, W. Palm). A later handout, State-variable Simulation in MATLAB (Linear Time-Invariant System Simulation – Part II) will cover finding the reponse of a system represented by in state space form. Creating a model The tf command creates transfer function model in MATLAB: sys=tf(num,den) where sys is whatever name you want to give the variable holding your model, num is a vector of coefficients of the numerator polynomial, and den is a vector of coefficients of the denominator polynomial. For both vectors, the coefficients must be listed in descending order. For example, 4s3 + 3s + 7 would be represented by the vector [4 0 3 7] (note the 0 in the place corresponding to the lack of s2 term). Examing a Model If a model already exists, you can find the transfer function numerator and denominator using [num,den]=tfdata(sys,’v’) where sys is the name of your model, and the second argument ’v’ forces the outputs num and den to be ordinary numerical vectors in the case of a model with a single input and single output (the default, if the’v’ is not included, is to return the answers as cell arrays, which are more cumbersome to deal with but necessary for models with multiple inputs and/or outputs). 1 Finding the response to a step or impulse input The step and impulse commands calculate the response to a unit step function input and a unit impulse, input respectively. (Because these are linear systems, the response to a step of magnitude is just the unit step response multiplied by .) If you have already created an LTI object (in this case a transfer function model) you can use one of the following commands: step(sys,t) impulse(sys,t) where sys is the variable containing the model and t can be either a scalar “final time” or a vector of times (from initial to final). You can also just send the numerator and denominator information directly, without creating the model first, using one of the following: step(num,den,t) impulse(num,den,t) The above commands create a plot (or either the step response or the impulse response). If you don’t want the plot created automatically, but would rather have access to the simulation data itself, place something like “[y,t_out]=” before either command: [y,t_out]=step(num,den,t) [y,t_out]=impulse(num,den,t) This will create a variable named t_out (you could name it whatever you want) that is a vector containing all the time steps used in the simulation, and a variable y (or whatever name you provide), which is a vector of corresponding output data (in the case of a single output variable), or a matrix with columns for each of the output (in the case of a system with multiple outputs). Finding the response to arbitrary input The lsim command is used to simulate the response to arbitrary user-defined input: lsim(sys,u,t) where sys is the name of the model and the t must be a vector containing a list of times (it cannot be a single number representing the final time, as it could for the step or impulse commands). u is a matrix of inputs with as many rows as there are elements in t and as many columns as there are inputs (thus u is simply a column vector if there’s only one input, which is the case if you’re dealing with a single transfer function). Each row must be the value of the input(s) at the corresponding time. The “[y,t_out]=” addition is also valid for the lsim command if you’d like the data itself rather than an automatically generated plot. 2 As an simple example of the use of lsim, say you have some system already created in memory, stored in the variable sys, and you want to simulate the response to an input that consists of the first half-period of sin(p t) , that is, ìï sin(p t) for 0 < t < 1 u(t) = í 0 for t ³ 1 ïî If you want to simulate the response for 2 seconds, you could set things up using t=0:0.1:2; (or t=linspace(0,2,21);, both make a vector of 21 times equally spaced from 0 to 2) u=zeros(21,1); (set up u with 21 rows, one for each time step, and 1 column) u(1:11)=sin(pi*t(1:11)); (create the half-period of the sine by entering the desired numbers for the first 1 second, leaving the remaing second of input as zero) If, at this point, you plotted u versus t, you would see: which is a (somewhat crude) representation of the desired input function. You could improve the fidelity by using smaller time steps. For instance, t=0:0.01:2 would have 201 elements instead of 21, since the time step is ten times smaller (the entries in u would have to be adjusted accordingly). Once the input and time vectors are created to your satisfaction, the command [y,t_out]= lsim(sys,u,t) would calculate the corresponding response. The vector t_out should be a duplicate of the vector t, and y will be a vector of the response (output) at each of the times in t_out. 3