MATLAB Symbolic Math Toolbox Guide
MATLAB Symbolic Math Toolbox Guide
Appendix F
F.1 Introduction
Readers who are studying MATLAB may want to explore the additional functionality of
MATLAB’s Symbolic Math Toolbox. Before proceeding, the reader should have studied
Appendix B, the MATLAB tutorial, including Section B.1, which is applicable to this
appendix.
MATLAB’s Symbolic Math Toolbox Version 8.0 in addition to MATLAB Version 9.3
(R2017b) and the Control System Toolbox Version 10.3 is required in order to add symbolic
mathematics capability to your M-files.
The M-files in this appendix are available elsewhere on this Web site.
Symbolic math commands are used in your MATLAB M-files right along with your
standard MATLAB statements. The only additional requirement is to declare symbolic
variables before they are used with the statement syms x1 x2..., where xi are symbolic
variables.
Some of the added capabilities that the Symbolic Math Toolbox yields for control
systems analysis and design include the following:
1. Functions and equations can be entered symbolically. That is, alpha characters as
well as numerical characters can be used in your M-files. For example, you can enter
B=x 2+3∗x+7, instead of B=[1 3 7]. You could even enter B=a∗x 2+b∗x+c and
obtain its factors as
[ 2 1/2]
[ -b + (b - 4 a c) ]
[1/2 — — — — — — — — — — — — — — — — — — — ]
[ a ]
[ ]
[ 2 1/2]
[ -b - (b - 4 a c) ]
[1/2 — — — — — — — — — — — — — — — — — — — ]
[ a ]
A-126
bappF_epdf 12/18/2018 20:14:24 Page 2
4. Laplace and z-transforms as well as their inverses can be entered and found in symbolic
form.
5. Functions can be “pretty printed” for clarity in the MATLAB Command Window and
printed output.
These are only a few advantages of using the Symbolic Math Toolbox. This appendix
will explore more. The reader is encouraged not to stop exploration at the end of
Appendix F, since there is so much more than can be covered here. The Bibliography
at the end of this appendix gives references for further pursuit.
The M-files for Appendix F can be found in the Control Systems Engineering
Toolbox. Symbolic Math Toolbox examples are included for Chapters 2, 3, 4, 6, and 13. The
reader is encouraged, however, to apply what is learned to other chapters.
ch2apF2 In this example, we find Laplace transforms of time functions using the
command, laplace(f), where f is a time function, f(t). As an example, we use
the time functions that resulted from the calculations in Cases 2 and 3 in Section 2.2 in
the text and work in reverse to obtain their Laplace transforms. We will see that the
command, laplace(f), yields F(s) in partial fractions. In addition to pretty printing
discussed in the previous example, the Symbolic Math Toolbox contains other commands
that can change the look of the displayed result for readability and form. Some of these
commands are: collect(F)—collect common coefficient terms of F; expand(F)—
expands product of factors of F; factor(F)—factors F; simple(F)—finds
simplest form of F with the least number of terms; simplify(F)—simplifies
F; vpa(expression, places)—standing for variable precision arithmetic, this
command converts fractional symbolic terms into decimal terms with a specified number
of decimal places. For example, the symbolic fraction, 3/16, would be converted to 0.1875 if
the argument, places, were 4. In the example below, we find the Laplace transform of a
time function. The result is displayed as partial fractions. To combine the partial fractions,
we use the command, simplify(F), where F is the Laplace transform of f(t) found using
laplace(f). Finally, we use F=vpa(F,3) to convert the symbolic fractions to
decimals in the displayed result.
ch2apF3 MATLAB’s Symbolic Math Toolbox may be used to simplify the input of
complicated transfer functions as follows: Initially, input the transfer function G(s) =
numg/deng via symbolic math statements. Then convert G(s) to an LTI transfer function
object. This conversion is done in two steps. The first step uses the command [numg,
deng]=numden(G) to extract the symbolic numerator and denominator of G. The
second step converts, separately, the numerator and denominator to vectors using the command
sym2poly(S), where S is a symbolic polynomial. The last step consists of forming the LTI
transfer function object by using the vector representation of the transfer function’s numerator
and denominator. As an example, we form the LTI object G s 54 s 27 s3 52s2
4 3 2 2
37s 73 s s 872s 437s 89s 65 s 79s 36 , making use of MATLAB’s
Symbolic Math Toolbox for simplicity and readability.
ch2apF4 (Example 2.10) MATLAB’s Symbolic Math Toolbox may be used to simplify
the solution of simultaneous equations by using Cramer’s rule. A system of simultaneous
equations can be represented in matrix form by Ax B, where A is the matrix formed from
the coefficients of the unknowns in the simultaneous equations, x is a vector containing the
unknowns, and B is a vector containing the inputs. Cramer’s rule states that xk the kth
element of the solution vector, x, is found using xk det Ak det A , where Ak is the
matrix formed by replacing the kth column of matrix A with the input vector, B. In the text,
we refer to det(A) as “delta.” In MATLAB, matrices are written with a space or comma
separating the elements of each row. The next row is indicated with a semicolon or carriage
return. The entire matrix is then enclosed in a pair of square brackets. Applying the above to
the solution of Example 2.10: A=[(R1+L∗s) L∗s; L∗s (L∗s+R2+(1/(c∗s)))]
and Ak=[(R1+L∗s) V; L∗s 0]. The function det(matrix) evaluates the determinant
of the square matrix argument. Let us now find the transfer function G s I2 s V s ,
asked for in Example 2.10. The command simplify(S), where S is a symbolic function,
is introduced in the solution. Simplify(S) simplifies the solution by shortening the
bappF_epdf 12/18/2018 20:14:24 Page 5
length of S. The use of simplify(I2) shortens the solution by combining like powers of
the Laplace variable, s.
'(ch2apF4) Example 2.10' % Display label.
syms s R1 R2 L c V % Construct symbolic objects for
% frequency variable 's', and
% 'R1', 'R2', 'L', 'c', and 'V'.
% Note: Use lower-case 'c'
% in declaration for
% capacitor.
A2=[(R1+L*s)V;-L*s 0] % Form Ak = A2.
A=[(R1+L*s)-L*s;-L*s (L*s+R2+(1/(c*s)))]
% Form A.
I2=det(A2)/det(A); % Use Cramer's rule to solve for
% I2(s).
I2=simplify(I2); % Reduce complexity of I2(s)
G=I2/V; % Form transfer function,
% G(s) = I2(s)/V(s).
'G(s)' % Display label.
pretty (G) % Pretty print G(s).
pause
Chapter 6: Stability
ch6apF1 (Example 6.2) MATLAB’s Symbolic Math Toolbox may be used conve-
niently to calculate the values in a Routh table. The toolbox is particularly useful for more
complicated tables, where symbolic objects, such as epsilon, are used. In this example, we
represent each row of the Routh table by a vector. Expressions are written for subsequent
row elements by using the equations given in Table 6.2 of the text. The MATLAB command
det(M) is used to find the determinant of the square matrix, M, as shown for each row
element in Table 6.2. Further, we test the previous row’s first element to see if it is zero.
If it is zero, it is replaced by epsilon, e, in the next row’s calculation. The preceding logic is
performed using MATLAB’s IF/ELSE/END as shown in the code below.
We now demonstrate the making of a Routh table using the Symbolic Math Toolbox
for a problem that requires the epsilon method to complete the table. The following program
produces the Routh table for Example 6.2 in the text. Also, for clarity, we convert all rows to
symbolic objects, simplify, and pretty print after forming the table. CAUTION: In general,
the results of this program are not valid if an entire row is zero as e approaches zero, such
as[e 0 0 0]. This case must be handled differently, as discussed in text Section 6.3 in the
subsection, “Entire Row Is Zero.”
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if -det([s3(1) s3(2);s2(1) s2(2)])/s2(1)==0
s1=[e...
-det([s3(1) s3(3);s2(1) s2(3)])/s2(1) 0 0];
% Create s^1 row of Routh table
% if 1st element is 0.
else
s1=[-det([s3(1) s3(2);s2(1) s2(2)])/s2(1)...
-det([s3(1) s3(3);s2(1) s2(3)])/s2(1) 0 0];
% Create s^1 row of Routh table
% if 1st element is not zero.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s0=[-det([s2(1) s2(2);s1(1) s1(2)])/s1(1)...
-det([s2(1) s2(3);s1(1) s1(3)])/s1(1) 0 0];
% Create s^0 row of Routh table.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
's5' % Display label.
s5=sym(s5); % Convert s5 to a symbolic object.
s5=simplify(s5); % Simplify terms in s^5 row.
pretty(s5) % Pretty print s^5 row.
's4' % Display label.
s4=sym(s4); % Convert s4 to a symbolic object.
s4=simplify(s4); % Simplify terms in s^4 row.
pretty(s4) % Pretty print s^4 row.
's3' % Display label.
s3=sym(s3); % Convert s3 to a symbolic object.
s3=simplify(s3); % Simplify terms in s^3 row.
pretty(s3) % Pretty print s^3 row.
's2' % Display label.
s2=sym(s2); % Convert s2 to a symbolic object.
s2=simplify(s2); % Simplify terms in s^2 row.
pretty(s2) % Pretty print s^2 row.
's1' % Display label.
s1=sym(s1); % Convert s1 to a symbolic object.
s1=simplify(s1); % Simplify terms in s^1 row.
pretty(s1) % Pretty print s^1 row.
's0' % Display label.
s0=sym(s0); % Convert s0 to a symbolic object.
s0=simplify(s0); % Simplify terms in s^0 row.
pretty(s0) % Pretty print s^0 row.
pause
ch13apF2 (Example 13.2) MATLAB’s Symbolic Math Toolbox and the command
iztrans(F) can be used to find the time-sampled function represented as f(nT), given its
z-transform, F(z). If you want the sampled time function returned as f(kT), then
change MATLAB’s default independent sampled-time variable by using the command
iztrans(F,k). Let us solve Example 13.2 using MATLAB’s Symbolic Math Toolbox.
ch13apF3 (Example 13.4) MATLAB’s Symbolic Math Toolbox can be used to find the
z-tansform of a transfer function, G(s), in cascade with a z.o.h. Two new commands
are introduced. The first, compose(f,g), allows a variable g to replace the variable t in
f(t). We use this command to replace t in g2(t) with nT before taking the z-transform. The other
new command is subs(S,old,new). Subs stands for symbolic substitution. Old is a
variable contained in S. New is a numerical or symbolic quantity to replace old. We use subs
to replace T in G(z) with a numerical value. To find the z-transform of a transfer function, G(s),
in cascade with a z.o.h. by using MATLAB’s Symbolic Math Toolbox, we perform the
following steps: (1) Construct G2 s G s s; (2) find the inverse Laplace transform of
G2(s); (3) replace t with nT in g2(t); (4) find G z 1 z 1 G2 z ; (5) substitute a numerical
value for T. Let us solve Example 13.4 using MATLAB’s Symbolic Math Toolbox.
Bibliography
MathWorks. Control System ToolboxTM Getting Started Guide R2018b. MathWorks, Natick, MA,
2000–2018.
MathWorks. MATLAB Primer R2018. MathWorks, Natick, MA, 1984–2018.
MathWorks. Symbolic Math ToolboxTM User’s Guide R2018. MathWorks, Natick, MA, 1993–2018.