Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  programs >  matlab

Matlab - the Symbolic Toolbox

This little document hopes to convince you that spending a few minutes learning to use the Symbolic Toolbox might save you hours of time. For further information look at the References

Introduction

Matlab has lots of adds-ons (called toolboxes). One of the most useful that we have installed is called the Symbolic Toolbox which performs symbolic maths commands (factorising, simplifying, integrating, differentiating, etc) and also has some solving routines. For common operations it's easy to use - e.g.

To make the most of the Symbolic Toolbox you need to be aware of a few concepts, in particular symbolic objects

sym

With the Symbolic Math Toolbox comes a new matlab datatype - symbolic object. Things of this type are created using the sym and syms. symbolic objects are essentially strings, but even if those strings contain only digits, arithmetic operations will be different to those in ordinary matlab. The following, for example, produces the result 5/6 rather than 0.8333.
a=sym(1)/sym(2)
b=sym(1)/sym(3)
a+b
To convert a symbolic object into a number use double - e.g.
double(a+b)
The command x=sym('x') creates a symbolic object called x which has the corresponding string representation x (i.e. in future x will be treated as a symbol entity). When (as in this case) the symbol name matches the symbol's value it's easier to use the equivalent "syms x". To evaluate a symbolic object for a particular value of a variable, use subs - e.g.
syms x
f=x^2-7*x+3
subs(f,x,5)
Here f will automatically be a symbolic object so you can do
diff(f)

Using the symbolic toolbox from Matlab

The most common problem that people have is with how Matlab and the symbolic toolbox interact. If for instance you haven't made x a symbol and you try

int(1/(1+x^2))

matlab will say "??? Undefined function or variable 'x'" because matlab's unhappy that x doesn't have a value. To get round this you can quote the expression as a string (int('1/(1+x^2)'), or define x to be a symbol as in the following

x=sym('x');
int(1/(1+x^2))

The following also works

[a,b]= solve('a+b=7','a-b=1','a','b')

Note that the symbolic toolbox doesn't automatically know the values of variables you've set in matlab. So for example, if you try something similar to the simultaneous equations example above

seven=7;
[a,b]= solve('a+b=seven','a-b=1','a','b')

you'll find that a and b are given in terms of the symbol seven. The answers are right, but not in a very useful form. By using matlab's eval command you can make seven be evaluated, getting the numerical answers, so adding the 2 lines below will print out '4' and '3'.

eval(a)
eval(b)

assumptions

Sometimes the symbolic toolbox seems to struggle with a simple-seeming task. For example you might expect

simplify('(a^(1/n))^n)')

to give you 'a'. In fact it doesn't simplify the expression at all, because of the possibility of variables being 0. You can get round this by forcing the variables to be positive when you make them symbolic - e.g.

syms a n positive
simplify((a^(1/n))^n)

Useful Routines

Using MuPad directly

The toolbox is based on MuPad. You can start MuPad from inside matlab by typing mupad. It might take 30 seconds or so to start, but eventually you'll see a menu-driven interface (see right) which makes common operations easy. Here's a sample session

 

diff(x^2, x)

2*x

factor(x^3-y^3)/(x^4-y^4)

((x - y)*(x^2 + x*y + y^2))/(x^4 - y^4)

int(1/(x^2 + a), x)

arctan(x/a^(1/2))/a^(1/2)

 

 

This HTML output is produced by mupad's export option. It can also produce PDF.

References

See Matlab - the Symbolic Toolbox based on Maple if your Matlab release is older than 2008b

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: March 2011