MATLAB stands for MATrixLABoratory. It is a technical computing environment for high-performance numeric computation and visualization. It integrates numerical analysis, matrix computation, signal processing and graphics in an easy-to-use environment, where problems and solutions are mathematically expressed instead of traditional programming. MATLAB allows us to express the entire algorithm in a few dozen lines, to compute the solution with great accuracy in a few minutes on a computer and to readily manipulate a three-dimensional display of the result in colour.
MATLAB is an interactive system whose basic data element is a matrix that does not require dimensioning. It enables us to solve many numerical problems in a fraction of the time than it would take to write a program and execute in a language such as FORTRAN, BASIC or C. It also features a family of application-specific solutions called toolboxes. Areas in which toolboxes are available include control system design, image processing, signal processing, dynamic system simulation, system identification, neural networks, wavelength communication and others. It can handle linear, non-linear, continuous-time, discrete-time, multivariable and multirate systems. This chapter gives simple programmes to solve specific problems that are included in the previous chapters.
The system models in a control system which are developed by applying laws such as Kirchhoff's current law, Kirchhoff's voltage law, Newton's laws and so on lead to linear, non-linear and ordinary differential equations. The standard input signals are applied to the system model to obtain the response of a control system. The compensation system are used along with the system to improve and optimize the system if the response of the system is unsatisfactory.
There exists two approach for the analysis and design of control systems. They are classical or frequency domain approach and state−variable approach. In classical approach, differential or difference equations are converted into transfer functions by relating the input and output using Laplace or z-transforms and then the transfer functions are used for analysing the system.
Laplace transform converts the differential equations of a dynamic system into algebraic equations of a complex variable s. The syntax for using the Laplace transform in MATLAB is laplace(function_in_time_domain).
The commands which are to be used in getting the Laplace transform of a system are:
syms t
f = function_in_t
laplace(f)
The inverse Laplace transform helps in determining the system equations in time domain t from the equations in s domain. The syntax for using the inverse Laplace transform in MATLAB is
ilaplace(function_in_s_domain).
The commands which are to be used in getting the system equation in time domain are:
syms s,t
f = function_in_s
ilaplace(f)
Example for the usage of laplace and ilaplace command is given in Table 14.1
Table 14.1 ∣ Laplace and inverse Laplace transform in MATLAB
Let us consider a polynomial where and are coefficients of in the numerator and denominator respectively. The above polynomial can be simplified using partial fraction expansion that is a tedious process. MATLAB provides a simplified way for converting a polynomial into a partial fractions. The command in MATLAB for partial fraction conversion is residue (numerator, denominator).
The commands which are to used in getting the partial fraction of a polynomial is given below:
n = [bm bm−1 … b1 b0] % coefficients of numerator polynomial
d = [an an−1 … a1 a0] % coefficients of denominator polynomial
[r, p, k] = residue (n, d)
where r is the column vector that contains the residues, p is the column vector that contains the poles of the system and k is the row vector which contains the direct terms.
Therefore, the partial fraction of the is
If all the values in the column vector are same, then
Example for the usage of residue command is given in Table 14.2.
Consider a polynomial
Table 14.2 ∣ Partial fraction in MATLAB
Therefore,
Similarly, to obtain polynomials of the numerator and the denominator, we can use the same residue command as follows:
[n, d] = residue(r, p, k)
Example for the usage of residue command for getting the coefficients of polynomial is given in Table 14.3.
Table 14.3 ∣ Usage of residue command in MATLAB
The time-domain analysis, frequency domain analysis and stability of the system can be analysed easily when the model of the system is represented in the form of transfer function. Let the transfer function of a system be
The above transfer function can be represented in MATLAB, if the coefficients of numerator polynomial and denominator polynomial are known. These coefficients are known by taking the Laplace transform of the equation in time domain which has been explained in the previous section.
Consider the transfer function of a system be
In MATLAB, this transfer function can be represented using the following syntax:
n = [bm bm−1 … b1 b0]
d = [an an−1 … a1 a0]
sys = tf (n, d)
Similarly, if the transfer function of the system is obtained in MATLAB, the coefficients of the numerator and denominator polynomials can be obtained using the command tfdata.
The syntax for using the command tfdata is
[n, d] = tfdata(sytem_transfer_function, 'v')
where the variable v ensures that the output variables n and d are polynomials of transfer function and return the values as a row vector. The usage of commands tf and tfdata for getting the transfer function of a system and getting the coefficients of the polynomial is given in Table 14.4.
Table 14.4 ∣ Usage of tf and tfdata commands in MATLAB
The gain, zeros and poles of a transfer function can be obtained in MATLAB using the command tf2zp. The syntax for using the command is
[z, p, k] = tf2zp (numerator_poly, denominator_poly)
where z is the zeros of the transfer function, p is the poles of the transfer function and k is the gain of the system.
Example for the usage of tf2zp for getting the gain, zeros and poles of a transfer function is given in Table 14.5.
Table 14.5 ∣ Zeros and poles from a transfer function in MATLAB
Also, from the poles, zeros and gain of a system, the coefficients of the polynomial of a transfer function can be obtained using the command zp2tf. The syntax for using the command is
[n, d] = zp2tf (z, p, k)
Example for the usage of zp2tf for getting the coefficients of polynomial of a transfer function is given in Table 14.6.
Table 14.6 ∣ Coefficients of polynomial from zeros and poles of a system
The command printsys used in the above code will be help in printing the transfer function of the system as a ratio of two polynomials in the variable mentioned within the quotes.
It is to be noted that if the commands tf2zp and zp2tf are used alone (i.e., without any output variable), the output will show only the zeros and coefficient of the numerator polynomial respectively. An example to explain the above concept is given in Table 14.7.
Table 14.7 ∣ Usage of tf2zp and zp2tf in MATLAB
The poles and zeros of a transfer function can be plotted in a graph in MATLAB using the command pzmap. The syntax for using the command is
pzmap(n, d)
Example for the usage of pzmap for plotting the poles and zeros of a transfer function is given in Table 14.8.
Table 14.8 ∣ Pole-zero map in MATLAB
The state-space representation of a continuous dynamic system is described by
where is the velocity vector or the derivative of the state vector of order (n × 1)
is the state vector of order (n × 1)
is the input or control signal vector of order (m × 1)
is the output vector of order (p × 1)
is the system or state matrix of order (n × n)
is the input matrix of order (n × m)
is the output matrix of order (p × n)
is the feed-through or feed-forward matrix of order (p × m)
If the matrices A, B, C and D are known, these matrices can be converted into a state-space model in MATLAB with the help of the command ss. The syntax for using the command is
variable_name = ss (A, B, C, D)
Example for the usage of ss for creating the state-space model for a system is given in Table 14.9.
Table 14.9 ∣ Creation of state-space model for a system
The phase variable canonical form of a system can be obtained from the state-space model of a system using the similarity transformation matrix P such that
where and are the system matrices in phase variable canonical form.
Consider a state-space matrices of a system as , , and . Obtain the phase variable canonical form of the system using MATLAB with the help of the similarity transformation matrix .
The code which is used for determining the phase variable canonical form of a system is given below:
The transfer function of a system can be converted into a state-space model using the command tf2ss. The syntax for using the command is
[A, B, C, D] = tf2ss (n, d)
where n is the row vector that contains the coefficient of numerator polynomial and d is the row vector which contains the coefficient of denominator polynomial
The state-space model of a system can be converted into a transfer function model using the command ss2tf. The syntax for using the command is
[n, d] = ss2tf (A, B, C, D, k)
where the parameter k is required if the system has more than one input and the value of k specifies the particular input.
An example for the usage of ss2tf to obtain transfer function of a system whose state-space model is given by , , and and the usage of tf2ss to obtain state-space model of a system whose transfer function is given by is given in Table 14.10.
Table 14.10 ∣ Usage of tf2ss and ss2tf in MATLAB for a system
If there exists two transfer functions and which are connected either in series (cascade), parallel or in feedback connection as given in Table 14.11, the resultant transfer function of the system in MATLAB is obtained using the functions mentioned below.
Consider two systems with the transfer functions and
where are the row matrices that contain the coefficients of numerator polynomials of transfer function and respectively and are the row matrices that contain the coefficients of denominator polynomials of transfer function and respectively.
The syntax for determining the resultant of the system for different type of connection is given in Table 14.11.
Table 14.11 ∣ Syntax for Series, Parallel and Cascade connections in MATLAB
where and are the row matrices which contains the coefficients of numerator polynomials of series, parallel and feedback connection respectively. and are the row matrices which contains the coefficients of numerator polynomials of series, parallel and feedback connection respectively.
An example to explain the concept of series, parallel and feedback connection is given in Table 14.12.
Table 14.12 ∣ Series, parallel and feedback connections using MATLAB
The detailed study about the time-domain analysis of a system is described in Chapter 5. In this section, we will be discussing how the time-domain analysis can be analysed using MATLAB.
14.2.12.1 Step response of a system:
The step response of a system can be obtained if the system is represented either by state-space model or transfer function model. The different syntax for determining the step response of a systems are given below:
Case 1: If the system is described using the transfer function:
Examples for using the command is given in Table 14.13.
Case 2: If the system is described using state-space model:
Table 14.13 ∣ Step response from transfer function
If i = 0 for the MIMO system, the number of time response graphs which will be displayed is (n * n) where n is the number of input to the system or number of output from the system.
Example for using the command is given in Table 14.14.
Table 14.14 ∣ Step response from state-space model
In addition, the step response of the system can be obtained using the plot command for a system when it is represented either by means of transfer function or by state-space model. The syntax for the usage of plot command given below:
Case 1: If the system is described by the transfer function
plot (t, y)
plot (t, y)
Case 2: If the system is described by the state-space model
plot (t, y)
plot (t, y)
plot (t, y)
plot (t, y)
(c) [y, x, t] = step (A, B, C, D, i, t); (when i > 0)
plot (t, y)
An example to show the usage of plot command in plotting the time response of the system is given in Table 14.15.
Table 14.15 ∣ Step response of a system using plot command
14.2.12.2 Impulse Response of a System: The different MATLAB commands used for determining the time response of a system when a system is subjected to impulse input is given in Table 14.16.
Table 14.16 ∣ Impulse response of a system in MATLAB
14.2.12.3 Ramp Response of a System: Similar to the step and impulse response of a system, it is not so easy to determine the ramp response of a system as ramp command does not exists in MATLAB. But by using the default command such as step and impulse it is possible to determine the ramp response of a system.
An example to determine the ramp response of a system using the step command is given in Table 14.17.
If the transfer function of the system is divided by s2, then the impulse command can be used to determine the time response of the system when it is subjected to ramp input.
Table 14.17 ∣ Ramp response of a system using step command in MATLAB
An example to determine the ramp response of a system using the impulse command is given in Table 14.18.
Table 14.18 ∣ Ramp response of a system using impulse command in MATLAB
14.2.12.4 Response to an Arbitrary Input: The MATLAB command lsim is used to determine the time response of a system when the system is subjected to any random input other than the standard inputs. The main difference between the other commands (step, impulse) is that the time values should be mentioned in lsim command. The different syntax used for determining the time response of a system when it is subjected to an input u is given below:
plot (t, y)
plot (t, y)
An example for the usage of lsim command in MATLAB is given in Table 14.19.
Table 14.19 ∣ Response of a system for an arbitrary input
The different performance indices which can be obtained from the time response of a system are
As the formulae for determining the performance indices varies for different inputs, it is necessary to write individual programmes for different inputs. But if the formulae are not used to determine the performance indices, a single programme will be enough to determine the performance indices for all the inputs.
A programme to determine the performance indices of a system for Step input is given in Table 14.20.
Table 14.20 ∣ Code to determine the performance indices of a system
The output of the code given in Table 14.20 is given in Table 14.21.
Table 14.21 ∣ Output of the code
The steady-state error of a system is determined with the help of the error constants (position, velocity and acceleration error constants). In addition, the steady-state error and the error constants depends on both TYPE of the system and type of the input applied to the system.
The relationship among the TYPE of the system, static error constant and steady-state error of the system is given in Table 5.5.
A program to determine the steady-state error of a system for any input and any TYPE of a system is given in Table 14.22
Table 14.22 ∣ Code to determine the steady-state error of a system
The output for the code given in Table 14.22 is given below:
The given system is Type 2 system
The error constants of the system are
Kp = Inf
Kv = Inf
Ka = 1.666667
The steady state error of the system for
Unit step input = 0.000000
Unit ramp input = 0.000000
Unit parabolic input = 0.600000
The Routh−Hurwitz criterion is one of the method for determining the stability of the system. The characteristic equation of a system is to be known for determining the stability of a system based on Routh−Hurwitz criterion. The detailed description about the Routh−Hurwitz criterion is discussed in Chapter 6. A programme to determine the stability of a system based on Routh−Hurwitz criterion can be coded.
The root locus technique helps in determining the stability of the system based on the location of closed-loop poles in the s-plane. The step-by-step procedure for plotting the root locus of a system is explained in Chapter 7. In MATLAB, two default command exist in plotting the root locus of a system.
Case 1: Using the command rlocus
The default command rlocus is used to plot the root locus of a system. The syntax for the command rlocus is given below:
rlocus (n, d)
where n is the row vector which contains the coefficient of numerator polynomial and d is the row vector which contains the coefficient of denominator polynomial
An example to show the usage of rlocus command in MATLAB is given in Table 14.23.
Table 14.23 ∣ Root locus of a system
In the above command, the value of gain K is automatically taken from 0 to . But if the user defines certain value of gain K, the command to be used in MATLAB for plotting the root locus of a system is
rlocus (n, d, K)
where K is the user-defined values of gain K.
Case 2: Using the command plot
The plot command can also be used to plot the root locus of a system. The syntax for using the plot command are given below:
[r, K] = rlocus (n, d);
plot (r)
[r, K] = rlocus (n, d, K);
plot (r)
r = rlocus (n, d, K);
plot (r)
An example to show the usage of plot command to plot the root locus of a system is given in Table 14.24.
Table 14.24 ∣ Root locus of a system
It is to be noted that if a point is clicked in the root locus plot of a system, it will display the information such as frequency (rad/s), overshoot (%), damping and gain at that point.
The gain value K at any point on the root locus of a system in MATLAB is determined using the command rlocfind. The different syntax for using the command rlocfind are given below:
where K is the gain value at a particular point and r is the column vector that gives the location of roots at gain K.
In the commands shown above, the first will return both the gain and location of roots while the second and third command when used in MATLAB, will return only the gain value K.
The step-by-step procedure to determine the gain value K at a given point are given below:
An example to determine the value of gain K from the root locus using MATLAB is given in Table 14.25.
Table 14.25 ∣ Gain of a system from root locus in MATLAB
The root locus plot for a system which is represented using the state-space model can be plotted with the help of the command rlocus. The syntax for using the command are given below:
Usage of plot command
plot (r)
plot (r)
An example to show the usage of rlocus plot using state-space model is given in Table 14.26.
Table 14.26 ∣ Root locus of a state − space model of a system
The Bode plot is one of the frequency-response technique which is used to analyse the stability of the system. The stability of the system is analysed based on the value of the frequency domain specifications (gain margin, phase margin, gain crossover frequency and phase crossover frequency). The manual determination of frequency domain specification from the Bode plot is discussed in Chapter 8. In this section, plotting of Bode plot in MATLAB and determination of frequency domain specification from Bode plot in MATLAB are discussed.
The default command in MATLAB which is used for plotting the Bode plot of a system is bode. The syntax for using the command bode for plotting the Bode plot for a system are given below:
Case A: If the system is represented in the transfer function model
bode (n, d) or
bode (sys_tf)
where n is the row vector containing the coefficients of numerator polynomial, d is the row vector containing the coefficients of denominator polynomial and sys_tf is the system transfer function obtained using the command tf.
An example for plotting the Bode plot is given in Table 14.27.
Table 14.27 ∣ Bode plot of a system
bode (n, d, w) or
bode (sys_tf, w)
where w is the range of frequency for plotting the Bode plot.
As the Bode plot is plotted in the logarithmic scale of the frequency w the user-defined values should also be in logarithmic scale. The MATLAB command which is used to obtain the frequency value in logarithmic scale is
w = logspace (a, b).
where 10a is the starting point of frequency range and 10b is the ending point of frequency range
An example for plotting the Bode plot is given in Table 14.28.
Table 14.28 ∣ Bode plot of a system
[mag, phase, w] = bode (n, d) or
[mag, phase, w] = bode (sys_tf)
[mag, phase, w] = bode (n, d, w) or
[mag, phase, w] = bode (sys_tf, w)
where mag and phase are the column vectors that contains the magnitude and phase values of the system corresponding to the different frequencies respectively.
Here, mag value can be converted into decibels with the help of 20 *log10(mag).
An example for obtaining the magnitude and phase values corresponding to different frequencies is given in Table 14.29.
Table 14.29 ∣ Bode plot of a system
It is to be noted that at a particular frequency, the magnitude and phase angles can be obtained by clicking at the plot which is shown in Fig. 14.1.
Fig. 14.1 ∣ Bode plot with the magnitude and phase angle at a frequency
The frequency domain specifications which can be obtained from Bode plot are gain margin, phase margin, gain crossover frequency and phase crossover frequency. The definition of each frequency domain specifications and its manual calculation is discussed in Chapter 8. In this section, determination of frequency domain specification values from Bode plot using MATLAB is discussed.
The default command used in MATLAB for determining the frequency domain specification is margin. The syntax for using the command in MATLAB are given below:
or [Gm, Pm, pcf, gcf] = margin (n, d) displays all the frequency domain specifications.
or [Gm, Pm, pcf, gcf] = margin (sys_tf) displays all the frequency domain specifications.
where Gm is the gain margin of the system, Pm is the phase margin of the system, pcf is the phase crossover frequency of the system and gcf is the gain crossover frequency of the system.
An example to show the usage of the command is given in Table 14.30.
Table 14.30 ∣ Frequency domain specification of a system using bode plot
Case B: If the system is represented using state-space model
bode (A, B, C, D)
where A, B, C, D are the state-space matrix of a system.
bode (A, B, C, D, 1, w)
where w is the user-defined frequency range obtained using the default command logspace.
An example for the usage of the command bode is given in Table 14.31.
Table 14.31 ∣ Bode plot of a system
bode (A, B, C, D, i)
where i is the input for which the Bode plot has to be plotted.
bode (A, B, C, D)
If we use this command, the number of bode plots generated will be
where m is the number of inputs and n is the number of outputs.
If the system has two inputs and two outputs, then the number of Bode plot generated by using the second command will be 4 (2 * 2).
An example for the usage of this command is given in Table 14.32.
bode (A, B, C, D, i, w)
Table 14.32 ∣ Bode plot of a system
An example for the usage of this command is given in Table 14.33.
Table 14.33 ∣ Bode plot of a system
It is to be noted that the determination of frequency domain specification using the default command margin in MATLAB is not possible. To use the default command margin, the system must be a SISO system.
The frequency response technique that analyze the stability of the closed-loop system based on the open-loop frequency response is known as Nyquist plot. The detailed analysis of the Nyquist plot is discussed in Chapter 9. The default command which is used to plot the Nyquist plot of a system in MATLAB is nyquist. The syntax for using the command in MATLAB are discussed below for different cases.
Case A: If the system is represented in the transfer function model
nyquist (n, d) or
nyquist (sys_tf)
where n is the row vector containing the coefficients of numerator polynomial, d is the row vector containing the coefficients of denominator polynomial and sys_tf is the system transfer function obtained using the command tf.
nyquist (n, d, w) or
nyquist (sys_tf, w)
where w is the range of frequency for plotting the Nyquist plot.
The range of frequency is generated with the help of the command linspace. The syntax for usage of linspace is
w = linspace (a, b, c);
where a is the starting point of the frequency range, b is the ending point of the frequency range and c is the number of points to be taken between a and b.
An example for plotting the Nyquist plot is given in Table 14.34.
[re, im, w] = nyquist (n, d) or
[re, im, w] = nyquist (sys_tf)
[re, im, w] = nyquist (n, d, w) or
[re, im, w] = nyquist (sys_tf, w)
where re and im are the column vectors that contain the real and imaginary values of the system corresponding to the different frequencies respectively.
An example for obtaining the real and imaginary values corresponding to different frequencies is given in Table 14.35.
Table 14.34 ∣ Nyquist plot of a system
Table 14.35 ∣ Nyquist plot data of a system
It is to be noted that at a particular frequency, the real value, imaginary value and its corresponding frequency can be obtained by clicking at the plot which is shown in Fig. 14.2.
Case B: If the system is represented using state-space model
nyquist (A, B, C, D)
where A, B, C, D are the state-space matrix of a system.
nyquist (A, B, C, D, 1, w)
where w is the user-defined frequency range obtained using the default command logspace.
Fig. 14.2 ∣ Nyquist plot with the real and imaginary value at a frequency
An example for the usage of the command nyquist is given in Table 14.36
Table 14.36 ∣ Nyquist plot
nyquist (A, B, C, D, i)
where i is the input for which the Nyquist plot has to be plotted.
nyquist (A, B, C, D)
If we use this command, the number of Nyquist plots generated will be .
where m is the number of inputs and n is the number of outputs.
If the system has two inputs and two outputs, then the number of Nyquist plot generated by using the second command will be 4 (2 * 2).
An example for the usage of this command is given in Table 14.37.
Table 14.37 ∣ Nyquist plot for a state-space model
nyquist (A, B, C, D, i, w)
An example for the usage of this command is given in Table 14.38.
Table 14.38 ∣ Nyquist plot for state-space model with user-defined frequency
The detailed analysis of the design of compensators is discussed in Chapter 11. There is no default command which can be used for designing the compensators in MATLAB. In this section, we will be analyzing the design of compensators in MATLAB with the help of an example.
14.2.19.1 Lag compensator: Consider an uncompensated system with the open-loop transfer function as . Design a lag compensator for the system such that the compensated system has static velocity error constant , phase margin and the gain margin .
Solution
As discussed in Chapter 10, step-by-step procedure are followed to determine the transfer function of the compensator.
Table 14.39 ∣ Bode plot for uncompensated system
Fig. 14.3 ∣ Magnitude value corresponding to desired phase margin
Table 14.40 ∣ Bode plot of compensated system
14.2.19.2 Lead compensator: Consider an uncompensated system with the open-loop transfer function as . Design a lead compensator for the system such that the compensated system has static velocity error constant , phase margin and the gain margin gm = 10 dB.
Solution
As discussed in Chapter 10, step-by-step procedure are followed to determine the transfer function of the compensator.
Table 14.41 ∣ Bode plot for uncompensated system
Fig. 14.4 ∣ Determination of frequency corresponding to AdB
14.2.19.3 Lag–Lead compensator: Consider an uncompensated system with the open-loop transfer function as . Design a lag−lead compensator for the system such that the compensated system has static velocity error constant , phase margin and the gain margin .
Solution
As discussed in Chapter 10, step-by-step procedure are followed to determine the transfer function of the compensator.
Table 14.42 ∣ Bode plot of compensated system
Table 14.43 ∣ Bode plot for uncompensated system
rad/s.
.
Fig. 14.5 ∣ Determination of magnitude A at
Table 14.44 ∣ Bode plot of compensated system
52.15.38.176