Symbolic Math Toolbox    

Symbolic and Numeric Conversions

Consider the ordinary MATLAB quantity

The sym function has four options for returning a symbolic representation of the numeric value stored in t. The 'f' option

returns a symbolic floating-point representation

The 'r' option

returns the rational form

This is the default setting for sym. That is, calling sym without a second argument is the same as using sym with the 'r' option:

The third option 'e' returns the rational form of t plus the difference between the theoretical rational expression for t and its actual (machine) floating-point value in terms of eps (the floating-point relative accuracy):

The fourth option 'd' returns the decimal expansion of t up to the number of significant digits specified by digits:

The default value of digits is 32 (hence, sym(t,'d') returns a number with 32 significant digits), but if you prefer a shorter representation, use the digits command as follows:

A particularly effective use of sym is to convert a matrix from numeric to symbolic form. The command

generates the 3-by-3 Hilbert matrix:

By applying sym to A

you can obtain the (infinitely precise) symbolic form of the 3-by-3 Hilbert matrix:

Constructing Real and Complex Variables

The sym command allows you to specify the mathematical properties of symbolic variables by using the 'real' option. That is, the statements

or more efficiently

create symbolic variables x and y that have the added mathematical property of being real variables. Specifically this means that the expression

is strictly nonnegative. Hence, z is a (formal) complex variable and can be manipulated as such. Thus, the commands

return the complex conjugates of the variables

The conj command is the complex conjugate operator for the toolbox. If conj(x) == x returns 1, then x is a real variable.

To clear x of its "real" property, you must type

or

The command

does not make x a nonreal variable.

Creating Abstract Functions

If you want to create an abstract (i.e., indeterminant) function , type

Then f acts like and can be manipulated by the toolbox commands. To construct the first difference ratio, for example, type

or

which returns

This application of sym is useful when computing Fourier, Laplace, and z-transforms.

Using sym to Access Maple Functions

Similarly, you can access Maple's factorial function k!, using sym:

To compute 6! or n!, type

Or, if you want to compute, for example, 12!, simply use the prod function

Example: Creating a Symbolic Matrix

A circulant matrix has the property that each row is obtained from the previous one by cyclically permuting the entries one step forward. We create the circulant matrix A whose elements are a, b, and c, using the commands

which return

Since A is circulant, the sum over each row and column is the same. Let's check this for the first row and second column. The command

returns

The command

returns

Now replace the (2,3) entry of A with beta and the variable b with alpha. The commands

return

From this example, you can see that using symbolic objects is very similar to using regular MATLAB numeric objects.

The Default Symbolic Variable

When manipulating mathematical functions, the choice of the independent variable is often clear from context. For example, consider the expressions in the table below.

Mathematical Function
MATLAB Command

f = x^n

g = sin(a*t + b)

h = besselj(nu,z)

If we ask for the derivatives of these expressions, without specifying the independent variable, then by mathematical convention we obtain , , and

. Let's assume that the independent variables in these three expressions are , , and , respectively. The other symbols, , , , and , are usually regarded as "constants" or "parameters." If, however, we wanted to differentiate the first expression with respect to , for example, we could write

to get .

By mathematical convention, independent variables are often lower-case letters found near the end of the Latin alphabet (e.g., x, y, or z). This is the idea behind findsym, a utility function in the toolbox used to determine default symbolic variables. Default symbolic variables are utilized by the calculus, simplification, equation-solving, and transform functions. To apply this utility to the example discussed above, type

This creates the symbolic expressions f, g, and h to match the example. To differentiate these expressions, we use diff.

returns

See the section Differentiation for a more detailed discussion of differentiation and the diff command.

Here, as above, we did not specify the variable with respect to differentiation. How did the toolbox determine that we wanted to differentiate with respect to x? The answer is the findsym command

which returns

Similarly, findsym(g,1) and findsym(h,1) return t and z, respectively. Here the second argument of findsym denotes the number of symbolic variables we want to find in the symbolic object f, using the findsym rule (see below). The absence of a second argument in findsym results in a list of all symbolic variables in a given symbolic expression. We see this demonstrated below. The command

returns the result

Here are some examples.

Expression
Variable Returned by findsym
x^n
x
sin(a*t+b)
t
besselj(nu,z)
z
w*y + v*z
y
exp(i*theta)
theta
log(alpha*x1)
x1
y*(4+3*i) + 6*j
y
sqrt(pi*alpha)
alpha


  Creating Symbolic Variables and Expressions Creating Symbolic Math Functions