Symbolic Math Toolbox    

Substitutions

There are two functions for symbolic substitution: subexpr and subs.

subexpr

These commands

solve the equation x^3+a*x+1 = 0 for x:

Use the pretty function to display s in a more readable form:

The pretty command inherits the %n (n, an integer) notation from Maple to denote subexpressions that occur multiple times in the symbolic object. The subexpr function allows you to save these common subexpressions as well as the symbolic object rewritten in terms of the subexpressions. The subexpressions are saved in a column vector called sigma.

Continuing with the example

returns

Notice that subexpr creates the variable sigma in the MATLAB workspace. You can verify this by typing whos, or the command

which returns

subs

Let's find the eigenvalues and eigenvectors of a circulant matrix A:

Suppose we want to replace the rather lengthy expression

throughout v and E. We first use subexpr

which returns

Next, substitute the symbol S into E with

Now suppose we want to evaluate v at a = 10. We can do this using the subs command:

This replaces all occurrences of a in v with 10.

Notice, however, that the symbolic expression represented by S is unaffected by this substitution. That is, the symbol a in S is not replaced by 10. The subs command is also a useful function for substituting in a variety of values for several variables in a particular expression. Let's look at S. Suppose that in addition to substituting a = 10, we also want to substitute the values for 2 and 10 for b and c, respectively. The way to do this is to set values for a, b, and c in the workspace. Then subs evaluates its input using the existing symbolic and double variables in the current workspace. In our example, we first set

To look at the contents of our workspace, type whos, which gives

a, b, and c are now variables of class double while A, E, S, and v remain symbolic expressions (class sym).

If you want to preserve a, b, and c as symbolic variables, but still alter their value within S, use this procedure.

Typing whos reveals that a, b, and c remain 1-by-1 sym objects.

The subs command can be combined with double to evaluate a symbolic expression numerically. Suppose we have

and want to see how M and P differ graphically.

One approach is to type

but this plot does not readily help us identify the curves.

Instead, combine subs, double, and plot

to produce a multicolored graph that indicates the difference between M and P.

Finally the use of subs with strings greatly facilitates the solution of problems involving the Fourier, Laplace, or z-transforms.


  Simplifications and Substitutions Variable-Precision Arithmetic