User:Jwood/matlab-control-tutorial

The purpose of this page is to provide a useful primer to teachers (and students) of a control theory course that requires numerical analysis using MATLAB to model complex linear systems. These systems consist of several interconnected sub-systems and are referred to as block diagrams. This primer will start with forming the most basic MATLAB system representation objects and then show how to interconnect several of them. Then a couple simulation methods will be discussed.

In linear control theory, the dynamics of the system of interest (plant) are often expressed in state-space as

$$\frac{dx}{dt} = Ax + Bu$$

$$y = Cx + Du \frac{}{}$$

where $$x \in R^n$$ is the vector of system states, $$y \in R^m$$ is the vector of system outputs, and $$u \in R^i$$ is the vector of inputs. In MATLAB a state-space object can be constructed as

$ ss(A,B,C,D);

Alternatively, a system can be represented as a transfer function. The transfer function is the system's output response to an input impulse. The output response is then

$$Y(s) = H(s)U(s) \frac{}{}$$

For one input one output (SISO), a transfer function is of the form

$$H(s) = \frac{b_{n_z-1} s^{n_z-1} + b_{n_z-2} s^{n_z-2} + ... + b_0}{a_{n_p-1} s^{n_p-1} + a_{n_p-2} s^{n_p-2} + ... + a_0}$$

In MATLAB a transfer function object can be constructed as

$ tf(num,den);

where num is the numerator polynomial and den is the denominator polynomial of the transfer function. For example, if the transfer function is

$$H(s) = \frac{1}{s+.5}$$

then you would construct the transfer function object as

$ tf([1],[1 .5]);

You can always get a state-space object from a transfer function object as

$ ss(tf(num,den));

Often a complex system can be modeled by interconnecting several sub-systems. After connecting the sub-systems, the resulting system consists of some subset of the inputs and outputs of the sub-systems. Constructing a state-space object in this manner can be done in MATLAB. To do this, you first need your sub-system state-space objects (ss1, ss2, ..., ssN). You then need to construct a system that has them independent of each other. You do this as

$ append(ss1,ss2,...,ssN);

You now need to connect inputs to outputs. To do this, define a matrix of connectivity. This matrix is of the form

$$ M = \begin{bmatrix} 1 & sysIn_{1,1} & sysIn_{1,2} & .. & sysIn_{1,N} \\ 2 & sysIn_{2,1} & sysIn_{2,2} & .. & sysIn_{2,N} \\

N & sysIn_{N,1} & sysIn_{N,2} & .. & sysIn_{N,N} \end{bmatrix} $$

In this matrix, the first column specifies the sub-system number. The elements $$sysIn_{i,j}$$ specify the outputs of which sub-systems sum together to feed into the input of the sub-system specified by the number in the first column. As a first example, consider a simple negative feedback system. You construct the feedback system by using the matrix

$$ M = \begin{bmatrix} 1 & -1 \end{bmatrix} $$

As a more complex example, if you have two sub-systems for which the output of the first sub-system sums with the negative output of the second sub-system and then feeds into the input of the second sub-system, then the matrix is

$$ M = \begin{bmatrix} 1 & 0 & 0 \\ 2 & 1 & -2 \end{bmatrix} $$

Notice that elements that are $$0$$ specify an empty input.

Now that the connectivity of the sub-systems is specified, you need to specify which of the sub-system inputs and outputs are the inputs and outputs of the total connected system. To do this you form vectors specifying the numbers of the inputs and outputs to be used. For example, if the inputs of sub-systems 2 and 5 are the connected system's inputs, then the input vector is

$$ in = \begin{bmatrix} 2 \\ 5 \end{bmatrix} $$

If the outputs of the sub-systems 1 and 3 are the outputs of the connected system, then the output vector is

$$ out = \begin{bmatrix} 1 \\ 3 \end{bmatrix} $$

So, after constructing the sub-system state-space objects ($$ss1$$, $$ss2$$, ..., $$ssN$$), the connectivity matrix ($$M$$), the input indices vector ($$in$$), and the output indices vector ($$out$$), and the appended system ($$sysApp$$), the final connected system is constructed in MATLAB as

$ connect(sysApp,M,in,out);

To check the stability of your connected system, one option is to plot the root loop and observe where the closed loop are. Do this in MATLAB as

$ rlocus(sys);

Now to simulate the the response of the connected system ($$sys$$), the input signals have to be specified. After specifying an arbitrary input signal-time pair ($$u$$ and $$t$$), you can simulate the response of the system as

$ lsim(sys,u,t);

The size of the input signal $$u$$ must match the length of the time vector $$t$$ and the number of system inputs. For example, if the number of inputs is $$N_i$$ and the size of $$t$$ is $$1 \times N_t$$, then the input signal must be of size $$N_i \times N_t$$. Note also that the order of the inputs is the same as the order specified when creating the connected system (i.e., check the $$in$$ vector).

An example input signal is a noisy step input. The nominal step is all ones. At each step in time add a random disturbance. The loop below illustrates the construction of this input signal.

$ dt = .01; tt = 0; $ t = zeros(1,N); u = zeros(1,N); $ for i=1:length(t) $    tt += dt; $    t(i) = tt; $    u(i) = 1+rand(-.1,.1); $ end

Hopefully this short system dynamics primer has helped jump start you in constructing block diagrams in MATLAB. The methods introduced here are very general so that arbitrary block diagrams can be constructed and arbitrary input signals can be applied to the system.

Jwood 29 November 2009