In this post, we provide a brief tutorial on MATLAB Control System Toolbox. The YouTube video accompanying this post is given below.
Consider the following mass-spring-damper system
Using second Newton’s law, we can obtain a differential equation describing the motion of the mass-spring-damper system under the action of the force . The procedure for deriving the differential equation is explained here. The differential equation has the following form
(1)
where is the distance from the equilibrium point, and are the first and second derivatives of , is the damper constant, is the spring constant, and is the applied force.
Let us now derive the transfer function of the system. By applying the Laplace operator and by ignoring the initial conditions, we obtain the following equation
(2)
where is the complex variable, and is the Laplace transform of . From (2), we obtain
(3)
where is the transfer function. Let us now derive the state-space model. First, we introduce the state-space variables
(4)
Using these state-space variables, from (1), we obtain
(5)
and assuming that the position is observed, the output equation takes the following form
(6)
In the sequel, we will explain how to compute step and impulse responses of the system using MATLAB Control System Toolbox. Also, we will explain how to compute the response to arbitrary inputs or initial conditions.
First, we define the model parameters, and define transfer function and state-space system representation. This is done using the following code lines that are self-explanatory.
m=10
kd=1
ks=10
%% transfer function represenation
num=[1];
den=[m kd ks];
W=tf(num,den)
%% state-space system representation
A=[0 1; -ks/m -kd/m];
B=[0; 1/m];
C=[1 0];
D=0;
sysSS=ss(A,B,C,D)
The system step response can be computed in two ways. The following code lines explain this.
%% step response
% to get the plot window just exectute step(W) or step(sysSS) without
% specifying the output parameters
[step1,timeStep1]=step(W)
[step2,timeStep2]=step(sysSS)
The step-response graph is shown below.
The impulse response can be computes as follows.
%% impulse response
[impulse1,timeImpulese1]=impulse(W)
[impulse2,timeImpulese2]=impulse(sysSS)
The impulse response graph is shown below.
Next, we compute system response for zero inputs and for non-zero initial condition. To the best of our knowledge, this can only be done for the state-space system description (the MATLAB function “initial” does not accept transfer-function system description). The following code lines can be used to compute the system initial condition response.
%% initial condition response
% define the initial condition
X0=[5;5];
[initial2,timeInitial2]=initial(sysSS,X0)
The initial condition response is shown in the figure below.
Finally, we explain how to compute the system response for arbitrary initial conditions and inputs. The following code lines can be used to perform this task.
%% response to an arbitrary signal and initial condition
X0_lsim=[-0.1,-0.1];
timeSignal=0:0.01:100;
signal=sin(0.5*(timeSignal))+2*ones(size(timeSignal));
[signalResponse, timeSignal1]=lsim(sysSS,signal,timeSignal,X0_lsim)
The results are shown in figure below.
The gray line in the above figure respresents the control inputs. The blue line represents the system response. By analyzing the shape of the blue line, we can observe that after initial transient reponse, lasting approximately until 40 seconds, the response of the system becomes a sinusoildal signal.