In MATLAB, writing equations and functions can look quite complex with parenthesis and operators scattered all about. However, what if there was a way to have more easily legible results to observe? The answer here is the MATLAB symbolic toolbox!
The symbolic toolbox is an extremely powerful MATLAB feature that has many useful capabilities waiting to be used by us programmers. In addition to providing more legible code, the symbolic toolbox allows for derivatives and integrals to be taken of symbolic functions. We can also take the inverse of a matrix with variables still inside of the matrix, undefined! These are only a few of the many exciting features, but we will highlight them in this tutorial. Let's dive in!
Think of a symbolic variable as a placeholder symbol for manipulating equations and functions. We can define symbolic variables like below.
matlab
syms x y z;
The code above creates three 1x1 symbolic variables, x, y, and z. To define more than one symbolic variable at a time, simply separate them by a space in their definition.
Now that some symbolic variables have been defined, let's make a function!
matlab
f = x^2 + 3*x - 4;
Now notice when you define the function f in your command window or script, it is listed as a 1x1 sym, similar to x, y, and z. This is because MATLAB recognizes that x is a symbolic variable and since it is used in f, it then treats f as a symbolic variable as well. This means, we can use all of the handy symbolic toolbox features on f.
The first spotlighted symbolic toolbox function is the diff() function. diff() has two main inputs, the first being the symbolic equation to take the derivative of, and the second being the symbolic variable to take the derivative with respect to. Let's see it in action.
matlab
% from before
syms x y z;
f = x^2 + 3*x - 4;
diff(f,x);
% output: 1x1 sym : 2*x + 3
What is exciting about this is we can take the derivative of functions symbolically without having to do any calculations ourselves!
The next function is the int() function. int() allows us to integrate functions, either by definite or indefinite integrals. Let's see how it works.
matlab
% from before
syms x y z;
f = x^2 + 3*x - 4;
% indefinite integral
int(f,x);
% output: 1x1 sym : (x*(2*x^2 + 9*x - 24))/6
% definite integral
int(f,x,0,1);
% output: 1x1 sym : -13/6
The first input to int() is the function to be integrated, and the second input is the symbolic variable to be integrated with respect to. This is what was done with the indefinite integral. Now if you add two more integers as inputs, those would act as the bounds to a definite integral, similar to the second int() execution above.
The last spotlighted symbolic toolbox trick is using symbolic variables in matrices. If you were to define a general matrix with variables inside of it with no assigned values, MATLAB would throw an error back. However, if we defined some symbolic variables, then we could form a matrix with these vars within it and then we could multiply by other matrices or take the inverse, etc.! Let's define a symbolic variable and form a matrix with it included.
matlab
syms s;
A = [ s 3
7 s+2 ];
Notice, when we defined A, it showed up in our workspace as a 2x2 sym. Now we can do neat things with A such as take the inverse of it! Let's try it.
matlab
% from before
syms s;
A = [ s 3
7 s+2 ];
A_inv = inv(A);
% output: [(s + 2)/(s^2 + 2*s - 21), -3/(s^2 + 2*s - 21)]
% [ -7/(s^2 + 2*s - 21), s/(s^2 + 2*s - 21)]
The following output is what you would see when in a MATLAB script (.m file). However, another very exciting thing is MATLAB live script's compatibility with symbolic variables. Try opening up a live script (.mlx file) and take the inverse of A, but leave the semicolon off. See how nice the output looks? That is much easier to digest than looking in the command window.
These tips for the symbolic toolbox in MATLAB are only a few of many incredible features so hopefully this helped inspire some creativity in what can be done with more complex operations that require visual clarity.