Symbolic Math Toolbox | ![]() ![]() |
Substitutions
There are two functions for symbolic substitution: subexpr
and subs
.
subexpr
solve the equation x^3+a*x+1 = 0 for x
:
s = [ 1/6*(-108+12*(12*a^3+81)^(1/2))^(1/3)-2*a/ (-108+12*(12*a^3+81)^(1/2))^(1/3)] [ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/ (-108+12*(12*a^3+81)^(1/2))^(1/3)+1/2*i*3^(1/2)*(1/ 6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/ (-108+12*(12*a^3+81)^(1/2))^(1/3))] [ -1/12*(-108+12*(12*a^3+81)^(1/2))^(1/3)+a/ (-108+12*(12*a^3+81)^(1/2))^(1/3)-1/2*i*3^(1/2)*(1/ 6*(-108+12*(12*a^3+81)^(1/2))^(1/3)+2*a/ (-108+12*(12*a^3+81)^(1/2))^(1/3))]
Use the pretty
function to display s
in a more readable form:
pretty(s) s = [ 1/3 a ] [ 1/6 %1 - 2 ----- ] [ 1/3 ] [ %1 ] [ ] [ 1/3 a 1/2 / 1/3 a \] [- 1/12 %1 + ----- + 1/2 i 3 |1/6 %1 + 2 -----|] [ 1/3 | 1/3|] [ %1 \ %1 /] [ ] [ 1/3 a 1/2 / 1/3 a \] [- 1/12 %1 + ----- - 1/2 i 3 |1/6 %1 + 2 -----|] [ 1/3 | 1/3|] [ %1 \ %1 /] 3 1/2 %1 := -108 + 12 (12 a + 81)
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
.
sigma = -108+12*(12*a^3+81)^(1/2) r = [ 1/6*sigma^(1/3)-2*a/sigma^(1/3)] [ -1/12*sigma^(1/3)+a/sigma^(1/3)+1/2*i*3^(1/2)*(1/6*sigma^ (1/3)+2*a/sigma^(1/3))] [ -1/12*sigma^(1/3)+a/sigma^(1/3)-1/2*i*3^(1/2)*(1/6*sigma^ (1/3)+2*a/sigma^(1/3))]
Notice that subexpr
creates the variable sigma
in the MATLAB workspace. You can verify this by typing whos,
or the command
subs
Let's find the eigenvalues and eigenvectors of a circulant matrix A
:
syms a b c A = [a b c; b c a; c a b]; [v,E] = eig(A) v = [ -(a+(b^2-b*a-c*b-c*a+a^2+c^2)^(1/2)-b)/(a-c), -(a-(b^2-b*a-c*b-c*a+a^2+c^2)^(1/2)-b)/(a-c), 1] [ -(b-c-(b^2-b*a-c*b-c*a+a^2+c^2)^(1/2))/(a-c), -(b-c+(b^2-b*a-c*b-c*a+a^2+c^2)^(1/2))/(a-c), 1] [ 1, 1, 1] E = [ (b^2-b*a-c*b- c*a+a^2+c^2)^(1/2), 0, 0] [ 0, -(b^2-b*a-c*b- c*a+a^2+c^2)^(1/2), 0] [ 0, 0, b+c+a]
Suppose we want to replace the rather lengthy expression
throughout v
and E
. We first use subexpr
S = (b^2-b*a-c*b-c*a+a^2+c^2)^(1/2) v = [ -(a+S-b)/(a-c), -(a-S-b)/(a-c), 1] [ -(b-c-S)/(a-c), -(b-c+S)/(a-c), 1] [ 1, 1, 1]
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
Name Size Bytes Class A 3x3 878 sym object E 3x3 888 sym object S 1x1 186 sym object a 1x1 8 double array ans 1x1 140 sym object b 1x1 8 double array c 1x1 8 double array v 3x3 982 sym object
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.
but this plot does not readily help us identify the curves.
Instead, combine subs
, double
, and plot
T = -6:0.05:6; MT = double(subs(M,t,T)); PT = double(subs(P,t,T)); plot(T,MT,'b',T,PT,'r-.') title(' ') legend('M','P') xlabel('t'); grid
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 | ![]() |