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

Matlab - the Symbolic Toolbox based on Maple

This little document hopes to convince you that spending a few minutes learning to use the Symbolic Toolbox might save you hours of time. It refers to the Symbolic Toolbox based on Maple rather than Mupad, so it's only any use with matlab releases prior to 2008b. See Matlab - the Symbolic Toolbox based on Mupad for Mupad-based documentation. 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.

The toolbox is actually a cut-down version of the Maple program. You can send maple commands to it using matlab's maple command, or for common routines like int (to integrate) you can use int directly. First, some examples to convince you that it's worth reading on!

Matlab/Maple interaction

Maple's usually straightforward to use. The most common problem that people have is with how Matlab and Maple interact. If for instance 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 (see the first example above), or define x to be a symbol as in the following
x=sym('x');
int(1/(1+x^2))
Note that maple 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')
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)

sym

With the Symbolic Math Toolbox comes a new matlab datatype - symbolic object. Things of this type are created using the sym and syms. Familiarity with them will help when using maple. 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.
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)

assume

Sometimes maple seems to struggle with a simple-seeming task. For example you might expect
maple('simplify((a^(1/n))^n)')
to give you 'a'. In fact it doesn't simplify the expression at all, because of the possibility of n being 0. maple has an assume command that lets you restrict variables. For example the following are possible
maple('assume(n>0)')
maple('assume(n,positive)')
maple('assume(n,natural)')
Any of these will let maple perform the simplification. To find out about the assumptions of a variable, use maple's about command.

Not all commands seem to obey the assume settings. An alternative approach is to use fsolve, which has an option that lets you set constraints. For example, the following only shows non-negative roots.

maple('fsolve(x^2=1,x,0..infinity)')

Useful Routines

References

You'll find the WWW version of the documentation (available only if you've running the browser on the Unix Teaching System) easier to read than what you get when you type help from within matlab, especially when maths notation is used. For example, the Taylor series expansion page provides useful information.
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: August 2003