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:
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).
syms x y f(x) g(x, y)
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.
syms x y f(x) g(x)
f(x) = exp(x) + cos(x)
g(x) = x^2 + 1
f(2)
f(g(x))
g(f(4))
Enter them into the code cell below and run the live script.
syms x y f(x) g(x)
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).
expand(expression)
Example 2
The commands below expand the expression .
syms x y
expand((x + y)^4)
Enter them into the code cell below and run the live script.
syms x y
expand((x + y) ^ 4)
ans = 

Simplifying expressions

To simplify an expression we can use the simplify command (simplify help page).
simplify(expression)
Example 3
The commands below simplify the expression .
syms x
simplify(x^3 - 3*x^2 + 3*x - 1)
Enter them into the code cell below and run the live script.
syms x
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).
partfrac(expression)
Example 4
The commands below determine the partial fraction decomposition of the following expression
syms x
partfrac((3*x + 1)/(x^3 - 3*x + 2))
Enter them into the code cell below and run the live script.
syms x
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) ;
syms x
expand((x + 2) ^ 2)
ans = 
(b) ;
syms x y a b c
expand((x + 1)*(a * x ^ 2 + a * b * y + c * x * y))
ans = 
(c) .
syms x y z
expand((x + y + z) ^ 3)
ans = 
2. Use the simplify command to simplify the following expressions:
(a) ;
syms x
simplify(x ^ 3 + 4 * x ^ 2 + 4 * x)
ans = 
(b) ;
syms x
simplify(x ^ 4 - 12 * x ^ 3 + 54 * x ^ 2 - 108 * x + 81)
ans = 
(c) .
syms x
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) ;
syms f(x) g(x) h(x)
f(x) = -2 * x - 3;
g(x) = x ^ 2 + 3 * x + 1;
h(x) = x / 2;
 
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) ;
syms t
partfrac(-(13 * t - 23) / (2 * (3 * t - 1) * (t - 3)))
ans = 
(b) ;
syms t
partfrac(-(9 * t ^2 + 10 * t + 15) / (3 * (t ^ 2 + 1) * (3 * t - 5)))
ans = 
(c) ;
syms t
partfrac((2 * t ^ 2 - 11 * t + 14) / ((5 * t - 4) * (t + 1) ^ 2))
ans = 
(d) .
syms t
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
we can use the diff command (diff help page).
syms f(x)
diff(function, variable, n)
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.
syms f(x)
f(x) = 2 * x ^ 3;
df = diff(f, x)
d2f = diff(f, x, 2)
d3f = diff(f, x, 3)
syms f(x)
f(x) = 2 * x ^ 3;
df = diff(f, x)
df(x) = 
d2f = diff(f, x, 2)
d2f(x) = 
d3f = diff(f, x, 3)
d3f(x) = 
12
Example 6
The commands below produce the product rule, quotient rule and chain rule for differentiation of single variable functions
syms f(x) g(x)
diff(f * g, x)
simplify(diff(f / g, x))
diff(f(g), x)
Enter them into the code cell below and run the live script.
syms f(x) g(x)
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,
we can use the int command (int help page)
int(f, x)
Example 7
The commands below evaluate the integral
syms f(x)
f(x) = 2*x^3;
int(f, x)
Enter them into the code cell below and run the live script.
syms f(x)
f(x) = 2 * x ^ 3;
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
int(f, x, a, b)
Example 8
The commands below evaluates the definite integral
syms f(x)
f(x) = cos(x);
int(f, x, pi/4, pi/2)
Enter them into the code cell below and run the live script.
syms f(x)
f(x) = cos(x);
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) ;
syms f(x)
f(x) = x ^ 5 + 3 * x ^ 4 - 5 * x ^ 2 + 10;
diff(f, x)
ans(x) = 
(b) ;
syms g(x)
g(x) = 3 * x ^ (1/2) - x ^ (3/2) + 2 * x ^ (-1/2);
diff(g, x)
ans(x) = 
(c) ;
syms lambda(t)
lambda(t) = sqrt(3 * t) + 3 * sqrt(t);
diff(lambda, t)
ans(t) = 
(d) ;
syms mu(y)
mu(y) = (2 + 5 * y + 1 / 2 * y ^ 2) ^ (1 / 2);
diff(mu, y)
ans(y) = 
(e) ;
syms phi(z)
phi(z) = (z + 1) * sqrt(z ^ 4 - 2 * z + 2);
diff(phi, z)
ans(z) = 
(f) ;
syms h(x)
h(x) = x / sqrt(1 - 4 * x^2);
diff(h, x)
ans(x) = 
6. Use the int command to determine the following integrals.
(a) ;
syms f(x)
f(x) = 4 * x ^ 3 + 2 * x ^ 2 - 3 * x + 4;
int(f, x)
ans(x) = 
(b) ;
syms f(x)
f(x) = cos(x) * sin(x);
int(f, x)
ans(x) = 
(c) ;
syms f(x)
f(x) = x + exp(x ^ (1 / 3));
int(f, x)
ans(x) = 
(d) ;
syms f(x)
f(x) = x ^ 2 + cos(x);
int(f, x, 1, 3)
ans = 
(e) .
syms f(x)
f(x) = x ^ 3 * cos(x);
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).
[solutions] = solve(equation1, equation2, ...)
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.,
eqn1 = y == x + 1
Example 9
The commands below solve the equation for x.
syms x y
x = solve(y == 2*x - 5)
Enter them into the code cell below and run the live script.
syms x y
x = solve(y == 2 * x - 5)
x = 
Example 10
The commands below solves the following linear system of equations
syms x y z
eqn1 = x + y + z == 0;
eqn2 = 2 * x + z == -1;
eqn3 = x - 2 * y + 3 * z = -12;
[x, y, z] = solve(eqn1, eqn2, eqn3)
Enter them into the code cell below and run the live script.
syms x y z
eqn1 = x + y + z == 0;
eqn2 = 2 * x + z == -1;
eqn3 = x - 2 * y + 3 * z == -12;
[x, y, z] = solve(eqn1, eqn2, eqn3)
x = 
1
y = 
2
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
syms a b c x
solve(a * x ^ 2 + b * x + c)
Enter them into the code cell below and run the live script.
syms a b c x
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).
[solutions] = dsolve(equation1, equation2, ...)
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
syms y(t) lambda
eqn = diff(y, t) == lambda*y;
y = dsolve(eqn)
Enter them into the code cell below and run the live script.
syms y(t) lambda
eqn = diff(y, t) == lambda * y;
y = dsolve(eqn)
y = 
Example 13
The commands below use dsolve to solve the second-order ODE
syms y(x)
eqn = diff(y, 2) + diff(y, 1) + y == 0
y = dsolve(eqn)
Enter them into the code cell below and run the live script.
syms y(x)
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
syms y(t) z(t)
eqn1 = diff(y, t) == z;
eqn2 = diff(z, t) == -y;
[y, z] = dsolve(eqn1, eqn2)
Enter them into the code cell below and run the live script.
syms y(t) z(t)
eqn1 = diff(y, t) == z;
eqn2 = diff(z, t) == -y;
[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
syms x y z
eqn1 = x + y + z == 0;
eqn2 = 2 * x + 3 * y + z == 1;
eqn3 = - x - 2 * y + 3 * z == 2;
[x, y, z] = solve(eqn1, eqn2, eqn3)
x = 
y = 
2
z = 
1
8. Use the solve command to find the solutions to the following system of nonlinear equations
syms p q
eqn1 = p ^ 2 + q ^ 2 == 10;
eqn2 = 2 * p + q == 1;
[p, q] = solve(eqn1, eqn2)
p = 
q = 
9. Use the dsolve command to find the solutions to the following system of differential equations.
syms y(t) z(t)
eqn1 = diff(y, t) == 4 * y + 7 * z;
eqn2 = diff(z, t) == - 2 * y - 5 * z;
[y, z] = dsolve(eqn1, eqn2)
y = 
z =