State-Space Simulation Using Matlab

advertisement
ME 344
Fall 2012
State-variable Simulation in MATLAB
(Linear Time-Invariant System Simulation – Part II)
This handout briefly describes commands related to finding the response of a system represented
in standard state space form to various inputs or initial conditions. Additional information,
including examples, can be found in Section 5.2 of the textbook (System Dynamics, 2nd Edition,
W. Palm). A previous handout, Transfer Function Simulation in MATLAB (Linear TimeInvariant System Simulation – Part I) covered finding the response of a system represented by a
transfer function (or transfer matrix).
Creating a model
The ss command creates a state-space model:
sys=ss(A,B,C,D)
where the matrices A,B,C, and D are the various matrices in the standard state-variable format,
and sys is the name of the variable you’re creating to hold your model.
Converting a model
If you know the matrices A, B, C, and D corresponding to state-space form, but wish to create a
transfer function object, you can find the necessary numerator and denominator coefficients
using the ss2tf command:
[num,den]=ss2tf(A,B,C,D);
Likewise, if you knew the transfer function representation and wanted to find the equivalent
state-space form, you can use the tf2ss command:
[A,B,C,D]=tf2ss(num,den);
If you have a model already created in memory, you can obtain the transfer function or statespace information using the tfdata and ssdata commands, regardless of the form in which
the model was originally created. For example, if you have a variable sys holding a model
created using either the ss or the tf command, you can find a representation of the system in
standard state-space form using
[A,B,C,D]=ssdata(sys)
which returns four numerical matrices, and the transfer function representation using
[num,den]=tfdata(sys,’v’)
as described in the previous handout.
Note that the state-variable model corresponding to a given transfer function is not necessarily
unique. If you create a state-variable model, your choice of A,B,C, and D reflects your own
choice of state variables. However, if you ask MATLAB to create a state-variable model from a
transfer function model, MATLAB will create as many state variables as it needs depending on
the order of the polynomials in the transfer function, and those state variables may or may not
correspond to actual physical degrees of freedom (although the input and output variables will
always correspond to the input and output of the transfer function).
Converting a Model with Multiple Inputs and/or Outputs
Since a transfer function is the relationship between a single input and a single output, when a
model consisting of a single transfer function is converted to state-space form, the matrix B will
have a single column and C will have a single row. If there are m inputs and p outputs, a transfer
function can be found relating each input to each output, so there are p  m transfer functions,
and they are usually written together in a p  m transfer matrix.
For a system with multiple outputs (p > 1), the ss2tf command described above will return a
single denominator vector den but a matrix num, where each row of the matrix contains the
coefficients for the numerator of a different transfer function. For example, if the system has
input u and outputs y1 and y2, the first row of the num matrix will have coefficients for the
polynomial in the numerator of Y1(s)/U(s) and the second row will contain the numerator
coefficients for Y2(s)/U(s).
If there are multiple inputs (m > 1), you must run the ss2tf command multiple times, using the
following format:
[num1,den]=ss2tf(A,B,C,D,1)
[num2,den]=ss2tf(A,B,C,D,2)
etc… (if there are more than two inputs)
If there is only one output, num1, num2, etc., will be vectors, containing the numerator
coefficients for Y(s)/U1(s), Y(s)/U2(s), etc. If there a multiple outputs as well as multiple inputs,
the various “nums” will all be matrices, with each row holding the numerator coefficients
corresponding to a different output (as described in the previous paragraph). Thus, to find the
transfer functions for a system with m=3 inputs and p=5 outputs, you’d need to run the
ss2tf command 3 times (once per input) and each time would give you a numerator matrix
with 5 rows (one per output), allowing you to determine a total of 15 transfer functions.
If a state-space model with multiple inputs and/or outputs has already been created, and you wish
to use the tfdata command to extract the various transfer functions, the results will be cell
arrays that have p rows and m columns, so, for example,
num{2,3}
gives the numerator coefficients for the transfer function Y2(s)/U3(s).
Finding the response to initial conditions
The initial command can be used to find the response to initial conditions for state space
models. Remember, transfer function relate output to input and assume zero initial conditions, so
this command can only be used on state space models, not transfer function models:
initial(sys,x0)
or
initial(sys,x0,t)
will plot the response of the system defined by the variable sys to the initial conditions
contained in the vector x0 (there must be one element in x0 for each state variable). The
optional last entry, t, can be either a single number, in which case MATLAB evaluates the
response at some default number of time steps between 0 and t, or it can be a vector containing
all the time steps at which you want the response evaluated.
If you would rather just obtain the response data itself (rather than a plot) you can use
[y,t_out]=initial(sys,x0,t);
which will return a vector t_out containing all the times and a variable y containing the output.
The output variable will be a matrix with a row corresponding to each time step in t_out and a
column for each output variable. As above, the last argument t is optional.
If you want to obtain the time histories of all the state variables (in addition to the outputs) you
can use
[y,t_out,x]=initial(sys,x0,t);
which, in addition to y and t_out, will create a matrix x with a row for each time step and a
column for each state variable. This is necessary if you’d like to create plots of the state
variables themselves and not just the output variable(s).
Finding the response to input(s)
The step and impulse commands calculate the response to a unit step function input and a
unit impulse, input respectively, exactly as they do for a transfer function. Use any of these
forms:
step(sys,t)
impulse(sys,t)
As in the case of the initial command, t can be either a scalar “final time” or a vector of times.
Also, placing “[y,t_out]=” before the command will place the results into the y and t_out
variables rather than a creating a plot.
The lsim command is also used to simulate the response of state-space systems to arbitrary
user-defined input.
lsim(sys,u,t)
As described in the previous handout on transfer function simulation, 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
step, impulse, or initial). 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). Each row must be the value of the input(s) at the corresponding time.
A state-space model can also be accompanied by initial conditions using the form
lsim(sys,u,t,x0)
where x0 has the same form as for the initial command described above.
Finally, for each of the above commands, not only can the “[y,t_out]=” addition be used (as
it could when applying them to transfer function models), but
[y,t_out,x]=step(sys,t,x0)
[y,t_out,x]=impulse(sys,t,x0)
[y,t_out,x]=lsim(sys,u,t,x0)
can also be used, which create an additional matrix x containing columns for all state variables
with rows corresponding to all time steps (similar to the initial command).
The following example of the use of lsim is similar to that provided in the transfer function
handout, but considers a start-variable model that has two input functions. Assume we have the
system already created in memory, stored in the variable sys, and it has two input functions
u1(t) and u2(t), and you wanted to find the first two seconds of the response of the system when
the first input is a unit step function at t = 0.5 and the second is the first half-period of sin(p t) ,
that is,
ìï sin(p t) for 0 < t < 1
ìï 0 for t < 0.5
and u2 (t) = í
u1 (t) = í
0
for t ³ 1
ïî 1 for t ³ 0.5
ïî
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,2); (set up u as a matrix with 21 rows, one for each time step, and 2
columns, one for each input functions)
u(6:21,1)=1;
(create the step function by making the first column 1 for all rows
corresponding to times starting at 0.5)
u(1:11,2)=sin(pi*t(1:11));
(create the half-period of the sine by entering the desired numbers
in the 2nd column for rows corresponding to the first 1 second)
If, at this point, you plotted the two columns of u versus t, you would see:
u1(t)
u2(t)
which is a (somewhat crude) representation of the desired input functions. 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 either a column vector (with the same number of entries as t_out) or a
matrix with multiple columns (if you linear model sys has multiple output variables).
Download