
Department of Engineering 


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
Mupadbased documentation.
For further
information look at the References
Introduction
Matlab has lots of addsons (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 cutdown 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!
 Integrating with respect to x.
or, more challengingly
 Factorising
maple('factor((x^3y^3)/(x^4y^4));')

(note that if you try to use factor directly, you'll call
matlab's own routine, which does prime factorisation).
 Solving simultaneous equations
[a,b]=solve('a+b=7','ab=1')

or just as easily for the computer
[a,b]=solve('a^2+cos(b)=7','cosh(a)b=1')

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
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','ab=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'.
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.
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^27*x+3 
subs(f,x,5) 
Here f will automatically be a symbolic object so you can do
assume
Sometimes maple seems to struggle with a simpleseeming 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 nonnegative roots.
maple('fsolve(x^2=1,x,0..infinity)') 
Useful Routines
 diff differentiates. You can multiply differentiate.
differentiates 3 times.
 int 
Indefinite or definite integration. For example
integrates between x=1 and x=2. To get the final numerical answer (4.0111),
use
eval(int('tan(x)+x^3',1,2))

 dsolve 
Matlab's ode23 and ode45 functions can find numeric
solutions to ODEs. The Symbolic toolbox's dsolve solves ODEs symbolically.
For example,
dsolve('D2y = a^2*y', 'y(0) = 1', 'Dy(pi/a) = 0')

returns
Here 'D' denotes differentiation with respect to the independent variable
(by default 't'), and a number after 'D' denotes repeated differentiation.
 factor works with expression or integers. Try
factor(sym('12345678901234567890'))

 collect  collects terms. You can choose which term you want to
focus on. For instance,
maple('collect((x+y)*(x^2+y^2+1), y)')

collects in terms of y.
 simplify simplifies expressions, evaluating numerically where
possible.
maple('simplify(exp(c*log(sqrt(a+b))))')

returns
 limit finds limits. If you wanted to find the
derivative of x*x the hard way you could do
syms x dx 
limit( ((x+dx)*(x+dx)  x*x)/dx, dx, 0) 
to get the answer 2*x as dx tends to 0.
 latex 
If you want an expression to be expressed as LaTeX code, use the latex
command. For example,
syms x 
f = taylor(log(1+x)); 
latex(f)

You can also represent expressions in Fortran and C using
fortran and ccode respectively.
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.