# Department of Engineering

## IT Services

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.

• Integration - int('1/(1+x^2)')
• Simultaneous Linear Equations - [a,b]= solve('a+3*b=7','7*a-b=1.5','a','b')
• Harder Equations - [a,b]=solve('a^2+cos(b)=7','cosh(a)-b=1')

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

• diff differentiates. You can multiply differentiate.
 diff('sin(x^2)',3)
differentiates 3 times.
• int - Indefinite or definite integration. For example
 int('tan(x)+x^3',2,3)
integrates between x=2 and x=3. To get the final numerical answer (4.0111), use
 eval(int('tan(x)+x^3',2,3))
• 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
 cos(a*t)
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,
 syms x y collect((x+y)*(x^2+y^2+1), y))
collects in terms of y, outputting
 y^3 + x*y^2 + (x^2 + 1)*y + x*(x^2 + 1)
• simplify simplifies expressions, evaluating numerically where possible.
 syms a b c simplify(exp(c*log(sqrt(a+b))))
returns
 (a + b)^(c/2)
• 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. 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) factor(x^3-y^3)/(x^4-y^4) int(1/(x^2 + a), x) This HTML output is produced by mupad's export option. It can also produce PDF.

## References

• Mathworks' MuPAD page has links to tutorials (some over 500 pages long)
• Mathworks' Symbolic Math Toolbox page shows how to use MuPAD from within Matlab.

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

• © Cambridge University, Engineering Department, Trumpington Street, Cambridge CB2 1PZ, UK (map)
Tel: +44 1223 332600, Fax: +44 1223 332662