Linear Algebra and Programming Skills
Dr Jon Shiach, Department of Computing and Mathematics, Manchester Metropolitan University
Symbolic Mathematics
Learning Outcomes
On successful completion of this page readers will be able to:
- use MATLAB's symbolic commands to perform algebraic manipulation of equations;
- perform symbolic differentiation, integration and definite integration using MATLAB;
- solve algebraic and simple differential equations symbolically using MATLAB.
MATLAB was originally designed to be a purely computational tool for mathematicians. Over the course of its development a group of tools have been developed that allow us to work with symbolic mathematics where the output is presented using symbols instead of a purely numerical result.
Algebra
Symbolic variables
The first thing we need when working with symbolic mathematics is to define any variables and functions that we want MATLAB to treat symbolically. This is done using the syms command (syms help page). This defines the variables x and y as symbolic variables and also the functions f and g are symbolic functions. Once symbolic variables and functions have been defined, they can be used to create symbolic expressions.
Example 1
The commands below declare symbolic variables and functions and evaluates the functions for some inputs.
Enter them into the code cell below and run the live script.
f(x) = exp(x) + cos(x)
f(x) = 
g(x) = x ^ 2 + 1
g(x) = 
f(2)
ans = 
f(g(x))
ans = 
g(f(4))
ans = 
Expanding expressions
To expand an algebraic expression we can use the expand command (expand help page). Example 2
The commands below expand the expression
. Enter them into the code cell below and run the live script.
expand((x + y) ^ 4)
ans = 
Simplifying expressions
Example 3
The commands below simplify the expression
. Enter them into the code cell below and run the live script.
simplify(x ^ 3 - 3 * x ^ 2 + 3 * x - 1)
ans = 
Partial fractions
To split up a fraction of the form
where f and g are polynomial functions we can use the partfrac command (partfrac help page). Example 4
The commands below determine the partial fraction decomposition of the following expression
Enter them into the code cell below and run the live script.
partfrac((3 * x + 1)/(x ^ 3 - 3 * x + 2))
ans =

Note that care must be taken to use brackets correctly when defining expressions involving polynomial functions.
Exercise 1 - Algebraic expressions
1. Use the expand command to expand the following expressions:
(a)
; expand((x + 2) ^ 2)
ans = 
(b)
; expand((x + 1)*(a * x ^ 2 + a * b * y + c * x * y))
ans = 
(c)
. expand((x + y + z) ^ 3)
ans = 
2. Use the simplify command to simplify the following expressions:
(a)
; simplify(x ^ 3 + 4 * x ^ 2 + 4 * x)
ans = 
(b)
; simplify(x ^ 4 - 12 * x ^ 3 + 54 * x ^ 2 - 108 * x + 81)
ans = 
(c)
. simplify(2 * x / (x ^ 2 - 4 * x + 4) + 1 / (x ^ 2 - 4 * x + 4) + x ^ 2 / (x ^ 2 - 4 * x + 4))
ans =

3. Consider the functions f, g, h all
defined by Use MATLAB commands to find the formulas, simplified as far as possible, that represent the functions:
(a)
; g(x) = x ^ 2 + 3 * x + 1;
fogoh = f(g(h(x)))
fogoh =

(b)
; gofoh = simplify(g(f(h(x))))
gofoh = 
(c)
; hogof = simplify(h(g(f(x))))
hogof =

(d)
; fof = f(f(x))
fof = 
(e)
; gog = simplify(g(g(x)))
gog = 
(f)
. hoh = h(h(x))
hoh =

4. Obtain the partial fraction expansions of the following rational function expressions.
(a)
; partfrac(-(13 * t - 23) / (2 * (3 * t - 1) * (t - 3)))
ans =

(b)
; partfrac(-(9 * t ^2 + 10 * t + 15) / (3 * (t ^ 2 + 1) * (3 * t - 5)))
ans =

(c)
; partfrac((2 * t ^ 2 - 11 * t + 14) / ((5 * t - 4) * (t + 1) ^ 2))
ans =

(d)
. partfrac(-(25 * t + 14) / ((2 * t ^ 2 + 5 * t + 5) * (t - 4)))
ans =

Calculus
Differentiation
To evaluate the derivative of the function
with respect to x Where function is a symbolic function which is differentiated with respect to the variable and n is an optional argument which is the order of the derivative.
Example 5
The commands below evaluate the first, second and third order derivatives of the function
. Enter them into the code cell below and run the live script. df = diff(f, x)
df(x) = 
d2f = diff(f, x, 2)
d2f(x) = 
Example 6
Enter them into the code cell below and run the live script.
diff(f * g, x)
ans(x) =

simplify(diff(f / g, x))
ans(x) =

diff(f(g), x)
ans =

Integration
To evaluate the integral of the function
with respect to x, Example 7
The commands below evaluate the integral
Enter them into the code cell below and run the live script.
int(f, x)
ans(x) =

Note that MATLAB does not include the constant of integration in the output.
Definite integration
To calculate the value of the definite integral
we specify the limits of integration a and b in the int command
Example 8
The commands below evaluates the definite integral
Enter them into the code cell below and run the live script.
int(f, x, pi / 4, pi / 2)
ans =

Exercise 2 - Differentiation and integration
5. Use the diff command to obtain the derivatives of the following functions.
(a)
; f(x) = x ^ 5 + 3 * x ^ 4 - 5 * x ^ 2 + 10;
diff(f, x)
ans(x) = 
(b)
; g(x) = 3 * x ^ (1/2) - x ^ (3/2) + 2 * x ^ (-1/2);
diff(g, x)
ans(x) =

(c)
; lambda(t) = sqrt(3 * t) + 3 * sqrt(t);
diff(lambda, t)
ans(t) =

(d)
; mu(y) = (2 + 5 * y + 1 / 2 * y ^ 2) ^ (1 / 2);
diff(mu, y)
ans(y) =

(e)
; phi(z) = (z + 1) * sqrt(z ^ 4 - 2 * z + 2);
diff(phi, z)
ans(z) =

(f)
; h(x) = x / sqrt(1 - 4 * x^2);
diff(h, x)
ans(x) =

6. Use the int command to determine the following integrals.
(a)
; f(x) = 4 * x ^ 3 + 2 * x ^ 2 - 3 * x + 4;
int(f, x)
ans(x) =

(b)
; int(f, x)
ans(x) =

(c)
; f(x) = x + exp(x ^ (1 / 3));
int(f, x)
ans(x) =

(d)
; int(f, x, 1, 3)
ans =

(e)
. int(f, x, 0, pi / 2)
ans =

Solving equations
Solving algebraic equations
We can use MATLAB to solve equations or systems of equations symbolically using the solve command (solve help page). Where solutions is a list of variables for which the equation(s) are being solved for and equation1 etc. are equations written using the == logical operator, e.g.,
Example 9
The commands below solve the equation
for x. Enter them into the code cell below and run the live script.
x = solve(y == 2 * x - 5)
x =

Example 10
The commands below solves the following linear system of equations
Enter them into the code cell below and run the live script.
eqn3 = x - 2 * y + 3 * z == -12;
[x, y, z] = solve(eqn1, eqn2, eqn3)
z = 
Example 11
The solve command can also solve non-linear equations. The commands below derive the quadratic formula used to find the roots of the quadratic equation
which is Enter them into the code cell below and run the live script.
solve(a * x ^ 2 + b * x + c)
ans =

Solving differential equations
To solve differential equations (where solutions exist) we can use the dsolve command (dsolve help page). Where equation1 etc. are defined using the diff command for the derivatives.
Example 12
The commands below determine the solution to the simplest differential equation
Enter them into the code cell below and run the live script.
eqn = diff(y, t) == lambda * y;
y = dsolve(eqn)
y = 
Example 13
The commands below use dsolve to solve the second-order ODE
Enter them into the code cell below and run the live script.
eqn = diff(y, 2) + diff(y, 1) + y == 0;
y = dsolve(eqn)
y =

Example 14
The commands below use dsolve to solve the system of ODEs
Enter them into the code cell below and run the live script.
[y, z] = dsolve(eqn1, eqn2)
y = 
z = 
Exercise 3 - Solving equations
7. Use the solve command to find the solution to the following linear system of equations
eqn2 = 2 * x + 3 * y + z == 1;
eqn3 = - x - 2 * y + 3 * z == 2;
[x, y, z] = solve(eqn1, eqn2, eqn3)
x = 
8. Use the solve command to find the solutions to the following system of nonlinear equations
eqn1 = p ^ 2 + q ^ 2 == 10;
[p, q] = solve(eqn1, eqn2)
p =

q =

9. Use the dsolve command to find the solutions to the following system of differential equations.
eqn1 = diff(y, t) == 4 * y + 7 * z;
eqn2 = diff(z, t) == - 2 * y - 5 * z;
[y, z] = dsolve(eqn1, eqn2)
y =

z = 