454MI: Introduction to MATLAB - Part 6 - Symbolic Math Computations
This is the sixth MATLAB live script of the collection ***Using MATLAB in the 454MI "***Data Driven Digital Systems" course, devoted to introduce the MATLAB/Simulink environment and tools for solving practical problems related to the topics of the 454MI course, i.e. performance analysis of dynamic systems, parametric estimation, identification of models from data, and prediction of the evolution of dynamic systems.
Use this link to go back to the main live script of the collection.
Introduction
Symbolic Math Toolbox provides functions for solving, representing and manipulating symbolic math equations. The toolbox offers functions for calculus, linear algebra, algebraic and differential equations, simplification and manipulation of equations. Symbolic Math Toolbox allows analytical derivations, Integrations, simplifications, solving of equations, applying Laplace and Z-transform, and much more.
Before You Start
In this live script you will learn, by running some examples
- How to define and manage symbolic variables;
- How to perform linear algebraic operations, such as computing the inverse, or the determinant, or the eigenvalues of a square matrix;
- How to apply the Laplace and the Z-transform.
clc % cleaning the workspace
sympref('HeavisideAtOrigin',1) % forcing the default preferences for the Symbolic Math Toolbox
ans =

Symbolic Scalar Variables, Matrix Variables & Functions
Symbolic Scalar Variables
The command
allows the definition of a scalar symbolic variable, named var1, assigning to the new variable a symbolic value corresponding to the variable's name
A simple example:
Then, you may create a symbolic expression using the new variables, and assign the result to one of them
t = 2*y^2-sqrt(abs(x-pi))/sin(2*x-pi/6);
Assumptions on Symbolic Variables
It is possible to declare assumptions on symbolic variables, regarding particular features or data type of the variables. The assumption can be 'real', 'rational', 'integer', or 'positive'.
assumptions
ans = 
Alternatively, you may check assumptions on a single symbolic variable
assumptions(a)
ans = 
Symbolic Vectors & Matrices: The Simplest Way to Create a Symbolic Array
You have two possibilities:
- Assemble a symbolic array, using at least a symbolic scalar element into it. Example:
A = [a, b; c, d] % <<- this is a symbolic variable (indeed a symbolic array)
A =

whos A
Name Size Bytes Class Attributes
A 2x2 8 sym
det(A)
ans = 
- Exploit the "casting" capabilities of the MATLAB programming language. Example:
% -->> magic() returns a NUMERIC array,
% BUT then we FORCE to change the data type into sym
M = sym([magic(3)])
M =

whos M
Name Size Bytes Class Attributes
M 3x3 8 sym
% now we can simply compute linear algebra computations
b = sym([1; 2/3; 0])
b =

v = M * b
v =

whos v b
Name Size Bytes Class Attributes
b 3x1 8 sym
v 3x1 8 sym
Symbolic Vectors & Matrices: More Sophisticate Approaches
A symbolic row or a column vector may be defined by using the command
for a row vector with
elements, and in the case of a column vector, with
components. In the general case of a matrix, the syntax of the command syms is obviously
Examples
b4 = sym(1/2)
b4 =

Technically they are arrays of symbolic scalar variables.
To create a symbolic matrix object, the command syntax is different
For example
Note the formal difference: the "matrix" symbolic object T is an all-in-one object, but the array Mis instead an array of symbolic scalar objects
whos T*
Name Size Bytes Class Attributes
T 4x3 8 symmatrix
whos M*
Name Size Bytes Class Attributes
M 4x3 8 sym
M1_1 1x1 8 sym
M1_2 1x1 8 sym
M1_3 1x1 8 sym
M2_1 1x1 8 sym
M2_2 1x1 8 sym
M2_3 1x1 8 sym
M3_1 1x1 8 sym
M3_2 1x1 8 sym
M3_3 1x1 8 sym
M4_1 1x1 8 sym
M4_2 1x1 8 sym
M4_3 1x1 8 sym
Symbolic Functions
You may create symbolic scalar functions with one or two arguments, simply by typing, for example
Both s and fare abstract symbolic functions. You have to assign an expression to them before using them.
Example
f(x, y) = 2*x+sqrt(3*y^2+4)
f(x, y) = 
Now, it is possible to evaluate the function at a specific input point:
Function of Vector and Scalar
Let's create the function
syms g(v,alpha) [1 1] matrix keepargs
The keyword keepargs allows to keep existing definitions of the vector v and the scalar α already in the MATLAB workspace.
Now, let's assign the formula of 
g(v,alpha) = norm(v)/alpha
g(v, alpha) = 
Now we can evaluate the function
gResult = g([ 1 2 3], 4)
gResult =

Finally, let's convert the result from the symmatrix datatype to the sym datatype
vSym = symmatrix2sym(gResult)
vSym =

Linear Algebraic Operations
An Example of an Ill-Conditioned Matrix
In linear algebra, a Hilbert matrix is a square matrix with entries defined as
The Hilbert matrices are classical examples of ill-conditioned matrices, i.e. difficult to use in numerical computations (computation of the eigenvalues and eigenvectors, the determinant, the inverse matrix are all difficult numerical problems for Hilbert matrices).
Generate the 20-by-20 Hilbert matrix. With format short, MATLAB prints the output
H = hilb(20)
1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500
0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476
0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455
0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435
0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417
0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400
0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385
0.1250 0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370
0.1111 0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357
0.1000 0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345
0.0909 0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345 0.0333
0.0833 0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345 0.0333 0.0323
0.0769 0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345 0.0333 0.0323 0.0312
0.0714 0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345 0.0333 0.0323 0.0312 0.0303
0.0667 0.0625 0.0588 0.0556 0.0526 0.0500 0.0476 0.0455 0.0435 0.0417 0.0400 0.0385 0.0370 0.0357 0.0345 0.0333 0.0323 0.0312 0.0303 0.0294
The computed elements of the matrix H are floating-point numbers that are the ratios of small integers. H is a MATLAB array of class double.
Let's convert H to a symbolic matrix:
Symbolic Linear Algebra Operations
Symbolic operations on
produce results that correspond to the infinite-precision Hilbert matrix, sym(hilb(20)), not its floating-point approximation, hilb(20). The Inverse Matrix
Compare with the numerical, ill conditioned, result
inv(hilb(20))
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.542396e-20.
1017 ×
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0001 -0.0002 0.0001 0.0000 0.0000 -0.0004 0.0005 -0.0003 0.0002 -0.0002 -0.0001 0.0003 -0.0002 0.0001
-0.0000 0.0000 -0.0000 0.0000 -0.0001 0.0006 -0.0017 0.0029 -0.0022 -0.0001 -0.0007 0.0059 -0.0078 0.0042 -0.0030 0.0034 0.0010 -0.0055 0.0041 -0.0010
0.0000 -0.0000 0.0000 -0.0001 0.0010 -0.0051 0.0151 -0.0255 0.0191 0.0016 0.0056 -0.0507 0.0664 -0.0352 0.0258 -0.0308 -0.0087 0.0487 -0.0361 0.0087
-0.0000 0.0000 -0.0000 0.0006 -0.0051 0.0254 -0.0757 0.1275 -0.0939 -0.0131 -0.0225 0.2488 -0.3248 0.1672 -0.1290 0.1616 0.0413 -0.2496 0.1860 -0.0447
0.0000 -0.0000 0.0001 -0.0017 0.0151 -0.0757 0.2251 -0.3763 0.2671 0.0629 0.0379 -0.7070 0.9188 -0.4525 0.3838 -0.5152 -0.1116 0.7650 -0.5737 0.1380
-0.0000 0.0000 -0.0002 0.0029 -0.0255 0.1275 -0.3763 0.6167 -0.3995 -0.1945 0.0462 1.0517 -1.3587 0.6061 -0.6543 0.9828 0.1503 -1.3517 1.0227 -0.2462
0.0000 -0.0000 0.0001 -0.0022 0.0191 -0.0939 0.2671 -0.3995 0.1428 0.4013 -0.3566 -0.3707 0.4935 -0.0735 0.5414 -1.0221 -0.0250 1.1509 -0.8857 0.2130
-0.0000 0.0000 0.0000 -0.0001 0.0016 -0.0131 0.0629 -0.1945 0.4013 -0.5902 0.7360 -0.9490 1.0754 -0.6539 -0.1587 0.4874 -0.1311 -0.1968 0.1583 -0.0355
0.0000 -0.0000 0.0000 -0.0007 0.0056 -0.0225 0.0379 0.0462 -0.3566 0.7360 -0.7230 0.2899 0.0218 -0.1884 0.5345 -0.5419 -0.1363 0.6254 -0.4228 0.0951
-0.0000 0.0000 -0.0004 0.0059 -0.0507 0.2488 -0.7070 1.0517 -0.3707 -0.9490 0.2899 2.7052 -3.8697 2.2079 -1.7393 1.8608 0.7222 -3.1220 2.2486 -0.5322
0.0000 -0.0000 0.0005 -0.0078 0.0664 -0.3248 0.9188 -1.3587 0.4935 1.0754 0.0218 -3.8697 4.8501 -2.3265 2.1118 -2.7439 -0.7989 4.2809 -3.1338 0.7448
-0.0000 0.0000 -0.0003 0.0042 -0.0352 0.1672 -0.4525 0.6061 -0.0735 -0.6539 -0.1884 2.2079 -2.3265 0.9793 -1.4937 2.0388 0.4591 -2.7700 2.0004 -0.4690
0.0000 -0.0000 0.0002 -0.0030 0.0258 -0.1290 0.3838 -0.6543 0.5414 -0.1587 0.5345 -1.7393 2.1118 -1.4937 1.4224 -1.0959 -0.7072 1.9691 -1.3035 0.2955
The Determinant of a Symbolic Matrix
Find the determinant of H.
One more time, compare with the numerical result:
The Adjoint Matrix
The following command returns the adjoint matrix X for a given numeric or symbolic matrix H
help adjoint
adjoint - Classical adjoint (adjugate) of square matrix
This MATLAB function returns the Classical Adjoint (Adjugate) Matrix X
of A, such that A*X = det(A)*eye(n) = X*A, where n is the number of rows
in A.
Syntax
X = adjoint(A)
Input Arguments
A - Square matrix
numeric matrix | matrix of symbolic scalar variables |
symbolic function | symbolic matrix variable |
symbolic matrix function | symbolic expression
Examples
Classical Adjoint (Adjugate) of Matrix
Compute Inverse Using Classical Adjoint and Determinant
See also ctranspose, det, inv, rank
Introduced in Symbolic Math Toolbox in R2013a
Documentation for adjoint
Other uses of adjoint
A = magic(4)
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
XA = adjoint(A)
103 ×
-0.1360 -0.4080 0.4080 0.1360
-0.4080 -1.2240 1.2240 0.4080
0.4080 1.2240 -1.2240 -0.4080
0.1360 0.4080 -0.4080 -0.1360
The Column Space of a Matrix
Given a symbolic matrix A, the command colspace(A) returns a symbolic matrix whose columns form a basis for the column space of the matrix A
Rh = colspace(H1)
Rh =

Ra = colspace(sym(A))
Ra =

Null Space of a Matrix
How to determine a basis for the null space of a symbolic matrix?
Please remember: a Hilbert matrix is always a full rank matrix, so the null space is empty!
ZA = null(sym(A))
ZA =

Characteristic Polynomial of a Matrix
Given a symbolic square matrix, you can compute the coefficients of the characteristic polynomial of such a matrix
or directly the expression of the characteristic polynomial in terms of a previously defined symbolic variable
A = sym([1 1 0; 0 1 0; 0 0 1]); % a symbolic matrix
syms x % the variable we want use to write
% the expression of the characteristic
% polynomial of the matrix A
polyA = charpoly(A,x) % the expression of the polynomial
polyA = 
Eigenvalues and Eigenvectors of Symbolic Matrix
Given a symbolic square matrix, you can compute eigenvalues and eigenvectors by using the command
A = sym([1 1 0; 0 1 0; 0 0 1]); % a symbolic matrix
lambda = eig(A) % just the eigenvalues of A
lambda =

To obtain both the eigenvalues and the corresponding eigenvectors, use
[eigVect, LambdaDiag] = eig(A) %#ok<ASGLU> % "magic code" to suppress unwanded warning messagges
eigVect =

LambdaDiag =

% (remove the magic code and see what happens)
The columns of the matrix eigVect represent the eigenvectors of the matrix A. The elements on the main diagonal of the matrix LambdaDiag are the eigenvalues.
[eigVH, LambdaH] = eig(sym(hilb(4)))
The eig function cannot find the exact eigenvalues in terms of symbolic numbers. Instead, it returns them in terms of the root function.
Use vpa to numerically approximate the eigenvalues
lambdaH_vpa = vpa(lambdaH)
lambdaH_vpa =

To have some insight about the command vpa, please use
What about the algebraic and geometric multiplicity of the eigenvalues?
Have a look at the size of the command output matrix eigVect:
- If eigVect is the same size as A, then the matrix A has a full set of linearly independent eigenvectors.
- If eigVecthas fewer columns than A, then at least one of the eigenvalues λ of the matrix A has an algebraic multiplicity m > 1 with fewer than m linearly independent eigenvectors associated with λ.
Example:
A = sym([1 1 0; 0 1 0; 0 0 1]); % a symbolic matrix
lambda = eig(A) % just the eigenvalues of A
lambda =

Please, note: there is only one eigenvalue, with algebraic multiplicity 3.
[eigVect, LambdaDiag] = eig(A)
eigVect =

LambdaDiag =

There are only two linearly independent eigenvectors: the geometric multiplicity is 2. The matrix Adoes not admit the diagonal form (but Jordan's canonical form).
Matrix Exponential of Symbolic Matrices
What if we have to compute the matrix exponential
?
A = sym([1 1 0; 0 1 0; 0 0 1]); % a symbolic matrix
expm(A)
ans =

The Laplace Transform
- For a detailed analysis of definition, fundamental properties and theorems regarding the Laplace Transform, refer to the Part6-L1 live script.
- You find a few applications of the Laplace Transform in the live script Part6-L2.
You can use the symbolic toolbox to calculate Laplace transforms in MATLAB. For example:
syms t s % t and s are now symbolic variables
f = exp(-t) * cos(2 * t);
F = laplace(f, t, s)
F =

You can also calculate the inverse Laplace transform:
ilaplace(F, s, t)
ans = 
and the Laplace transform of unitary steps:
laplace(heaviside(t), t, s)
ans =

MATLAB also knows many of the properties and tricks you also know:
F = exp(-s)/(s + 1)
F =

ilaplace(F, s, t)
ans = 
Laplace Transform: Rational Functions
Standard MATLAB (not the symbolic toolbox) have a number of functions specialized to polynomial and rational functions. Polynomials are represented by their coefficients. The vectors:
can be used to represent the numerator and denominator of the rational function:
For example:
[r,p,k] = residue(num, den)
0.0000 - 1.0000i
0.0000 + 1.0000i
-1.0000 + 1.0000i
-1.0000 - 1.0000i
calculates its partial-fraction expansion. r contain the residues and p the corresponding poles. In this case this partial-fraction leads to the inverse Laplace transform:
as show in Section 3.5. The output parameter k contains the remaining polynomial when
is not strictly proper. For example, for [r,p,k] = residue(num, den)
has a non-zero polynomial remainder
. The State Movement of a Continuous-time LTI System
Consider
and compute the state movement, starting from the initial state
with the input
The state movement is expressed by
Let's compute the state movement, using the Symbolic Math Toolbox
syms s % the Laplace transform variable
syms a K % the symbolic parameters into the state equations
syms t % the time as symbolic variable
% assigning the state equation matrices
A = [0 , 1; 0 -a]
A =

x0 = [1; 0]; % the initial state
B = [0; K]
B =

Now define the symbolic matrix
, used in the Laplace transform of the exponential matrix % now define the symbolic matrix (sI-A)
sIA_inv = inv(sIA)
sIA_inv =

Now, let's compute the movement using the Laplace transform
Us = laplace(2*sin(4*t+pi/3)) % the Laplace transform of the input
Us =

Xs = sIA_inv*x0 + sIA_inv*B*Us % the Laplace transform of the state movement
Xs =

Finally, by using the inverse Laplace transform
xt = ilaplace(Xs)
xt =

The Z-Transform
For a detailed analysis of definition, fundamental properties and theorems regarding the Z-Transform, refer to the Part6-Z live script.
You can use the symbolic toolbox to calculate Z-transforms in MATLAB. For example:
syms n % the time samples variable
By default, the independent variable is nand the transformation variable is z.
F = ztrans(f)
F =

You can also calculate the inverse Z-transform:
iztrans(F)
ans = 
and the Z-transform of unitary steps:
sympref('HeavisideAtOrigin',1); % Pay attention: the default value of the Heaviside function at the origin is 1/2.
collect(ztrans(heaviside(n)))
ans =

Check what happens if you restore the default preferences, by typing
and execute the previous code.
You can also compute the Z-transform of the unitary impulse function
f = kroneckerDelta(n)
f = 
Z-Transforms Involving Heaviside Function and Binomial Coefficient
Let's compute the Z-transform of the sequence
x = nchoosek(n,2)*heaviside(n-2)
x =

Xz = simplify(ztrans(x,z))
Xz =

Inverse Z-transform
iztrans(F)
ans =

Compute the inverse Z-transforms of delayed sequences. The results involve the Kronecker Delta function:
iztrans(1/z,z,n)
ans = 
iztrans(f,z,n)
ans = 
Summary
Using this live script you have:
- learned how to define and manage symbolic variables;
- learned how to perform linear algebraic operations, such as computing the inverse, or the determinant, or the eigenvalues of a square matrix;
- learned how to apply the Laplace and the Z-transform.
Back to the Index
Use this link to go back to the main live script of the collection.
Back to the Previous Part: LTI Systems
Use this link to go back to the previous live script of this collection.
Go to the Next Part: Visualization & Programming
Use this link to go to the next live script of the collection.