Professional Documents
Culture Documents
StateVariableSim in Matlab r2
StateVariableSim in Matlab r2
and u
2
(t) =
sin( t) for 0 < t <1
0 for t 1
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 2
nd
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:
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).
u2(t)
u1(t)