clear all;
Symbolic Math Toolbox
This tutorial will utilize the symbolic math toolbox. This short tutorial will explain some of the basic functionalities that will be used in future tutorials.
The Symbolic Math Toolbox is a powerful tool for calculations, as it lets us use variables instead of numbers. This can be useful to reuse complex matrices or equations and substitute values when needed.
Please enable "Output inline" on the right side of your scroll bar.

Create a Symbolic Variable
to create a symbolic variable you can use the syms command followed by your desired variables.
syms var
If you only work with real numbers, as will be the case for this tutorial, extend the syms with a real to reduce symbolic computations for imaginary numbers.
syms var real
You can create multiple variables at once, if you extend it with a number, it will be shown as an indices on display
syms var1 var2 var3 real real
MyVars = [var1 var2 var3]
$$ \displaystyle \left(\begin{array}{ccc} {\textrm{var}}_1 & {\textrm{var}}_2 & {\textrm{var}}_3 \end{array}\right) $$
When spelling out greek letters as variables, they be converted into their symbols on display:
syms alpha beta gamma Delta delta
MyVars = [alpha, beta, gamma, Delta, delta]
$$ \displaystyle \left(\begin{array}{ccccc} \alpha & \beta & \gamma & \Delta & \delta \end{array}\right) $$
Substituting variables
using the subs() function we can substitute symbolic variables for values, making it easy to reuse e.g. equations :
syms alpha beta x y z real
Equation1 = x^2 ;
Equation2 = 2 * x + y;
Value1 = subs(Equation1, x, 3)
Value2 = subs(Equation2, [x, y], [3, 1])
We can combine functions like:
Equation3 = Equation1 + Equation2
Equation4 = Equation1 * Equation2
Equation5 = subs(Equation4, [x, y], [alpha, beta])
You can also substitute vectors for variables resulting in a vector output where each row relates to the corresponding input row.
timevec= linspace(0,10,5)' %this creates an equally spaced row vector from 0 to 10 in 5 steps.
timevec = 5x1
0
2.5000
5.0000
7.5000
10.0000
EquationVector = subs(Equation4,x,timevec)
$$ \displaystyle \left(\begin{array}{c} 0\newline \frac{25\,y}{4}+\frac{125}{4}\newline 25\,y+250\newline \frac{225\,y}{4}+\frac{3375}{4}\newline 100\,y+2000 \end{array}\right) $$
Converting symbolic variables
Sometimes you get error messages when attempting to combining numerical and symbolic variables. Avoid this by converting one of them:
After substituting the Workspace variable is still considered a symbolic variable. Even if your new variable only contains numbers, MATLAB will consider them a symbolic variable, which may lead to difficulties. You can convert it to a numeric by:
ValueDouble = double(Value2)
ValueDouble = 7
If you try to insert symbolic variables into numeric variables you need to convert the numeric matrix into a symbolic matrix:
Matrix_1 = ones(3)
Matrix_1 = 3x3
1 1 1
1 1 1
1 1 1
Matrix_symb = [alpha, beta;
gamma, delta]
$$ \displaystyle \left(\begin{array}{cc} \alpha & \beta \newline \gamma & \delta \end{array}\right) $$
Matrix_combined = sym(Matrix_1)
$$ \displaystyle \left(\begin{array}{ccc} 1 & 1 & 1\newline 1 & 1 & 1\newline 1 & 1 & 1 \end{array}\right) $$
Matrix_combined(1:2,1:2) = Matrix_symb
$$ \displaystyle \left(\begin{array}{ccc} \alpha & \beta & 1\newline \gamma & \delta & 1\newline 1 & 1 & 1 \end{array}\right) $$
Working with Symbolic Variables
The Symbolic Math Toolbox gives us some powerful tools when doing calculus.
Differentiating with Symbolic Variables
We can differentiate an expression like:
Equation = x^2 + x * y^2 + y^3 + 5
diff_Eq1 = diff(Equation, x)
You can also differentiate w.r.t. multiple variables at once:
diff_Eq2 = diff(Equation, x, y)
diff_Eq3 = diff(Equation, x, x)
diff_Eq4 = diff(Equation, y, y)
This can also be used in a matrix, where each element
MatrixEquation = [x^2, x*y, x*y*z;
x*y, y^2, y*z ;
x*y*z, y*z, z^2 ]
$$ \displaystyle \left(\begin{array}{ccc} x^2 & x\,y & x\,y\,z\newline x\,y & y^2 & y\,z\newline x\,y\,z & y\,z & z^2 \end{array}\right) $$
diff_Matrix1 = diff(MatrixEquation, x)
$$ \displaystyle \left(\begin{array}{ccc} 2\,x & y & y\,z\newline y & 0 & 0\newline y\,z & 0 & 0 \end{array}\right) $$
diff_Matrix2 = diff(MatrixEquation, x,y)
$$ \displaystyle \left(\begin{array}{ccc} 0 & 1 & z\newline 1 & 0 & 0\newline z & 0 & 0 \end{array}\right) $$
diff_Matrix3 = diff(MatrixEquation, x,y,z)
$$ \displaystyle \left(\begin{array}{ccc} 0 & 0 & 1\newline 0 & 0 & 0\newline 1 & 0 & 0 \end{array}\right) $$
Rewriting Equations
We can rewrite expressions using the symbolic toolbox:
Eq = (x + 1) * (x + y)
The collect() function will return an equation where all expressions are pooled based on the desired variable and its identical powers
Eq_collect_x = collect(Eq, x)
The simplify() function lets us factorize an expression and applying cancellation rules
Simple_Eq = simplify(Eq_collect_x)
Solving Equations
The symbolic toolbox also allows us to solve for variables:
syms a b real
Eq1 = a * 5 + b == 0
Eq2 = a * 15 + b == 4
solutions = solve([Eq1, Eq2], [a, b])
solutions = struct with fields:
a: 2/5
b: -2
We can also let it solve for a variable that depends on others:
parameter_solution_a = solve(Eq1, a)