8. Symbolic Toolbox


In all previous sections of this introduction MATLAB has been used as a powerful programmable  graphics calculator. A variable x must have a numerical value (or array values) before expressions involving x can then be evaluated. For example if you merely enter the single command sin(x) after the prompt you receive the error message

??? Undefined function or variable ’x’.

However, commands in the Symbolic Math Toolbox enable you to enter formulae such as sin(2*x), x*exp(x), x^2+3*atan(x), cos(x)^2+sin(x)^2, etc, where x is a symbolic variable. Each expression can be given a variable name (also symbolic) thereby allowing algebraic, trigonometric and other functional manipulations and simplifications as well as permitting differential and integral calculus. You will be solving and computing with mathematical symbols rather than numbers.

The tools in the Symbolic Math Toolbox are built upon the core of the powerful software program called Maple® which is used in many universities in Australia and overseas. For convenience only in this guide we will refer to the smaller Symbolic Math Toolbox attached to Matlab as “Maple”. The interface between Matlab and “Maple” is smooth and it is easy to move in and out of this Symbolic Math Toolbox.

The simplest way to enter “Maple” is to make one or more variables into symbolic symbol(s) using the syms command. Enter the following and examine the output:

syms x

y=x^2-5/x          % y is automatically another symbolic variable

z=y*x-8            % z is automatically another symbolic variable

You notice that no attempt has yet been made to simplify z. Add the command z=simplify(z).

Note that in earlier versions of Matlab the first two lines above were equivalent to the single command y=’x^2-5/x’. This notation for constructing a symbolic expression using single quotation marks is still valid in many parts of “Maple” although it is being phased out because MATLAB already uses single quotation marks in other situations. Often in the “Maple” help menu you will still see the single quotation formulation but it is recommended you ignore it wherever possible to avoid confusion.

Numbers can be held in various possible formats in “Maple”. The default format is an integer or rational fraction. If this is not possible, a real number will be approximated by a fraction of large integers. The sym function converts the argument into a symbolic expression in “Maple”. Conversely, the double function converts a symbolic number back into a double-precision value in MATLAB. In each of the following cases the symbolic expression in the middle column results from the corresponding sym command, while the last column results if you then apply the double(...) command to each symbolic value in the middle column:

sym(11/13) 11/13 0.8462
sym(3.85) 77/20 3.8500
sym(pi) pi 3.1416
sym((4/5)^2) 16/25 0.6400
sym(sqrt(49)) 7 7
sym(sqrt(51)) sqrt(51) 7.1414
sym(exp(1)) 6121026514868073*2^(-51) 2.7183
sym(sin(1)) 7579296827247854*2^(-53) 0.8415

Alternative formats can be viewed by entering help sym.

Exercise: What do you expect to result if you enter the following?

syms x

y=x+1.4*sqrt(x+3)+5.49

pretty(y) % output to look like type-set mathematics