Symbolic Math Toolbox |
 |
Simplifications and Substitutions
There are several functions that simplify symbolic expressions and are used to perform symbolic substitutions.
Simplifications
Here are three different symbolic expressions.
syms x
f = x^3-6*x^2+11*x-6
g = (x-1)*(x-2)*(x-3)
h = x*(x*(x-6)+11)-6
Here are their prettyprinted forms, generated by
pretty(f), pretty(g), pretty(h)
3 2
x - 6 x + 11 x - 6
(x - 1) (x - 2) (x - 3)
x (x (x - 6) + 11) - 6
These expressions are three different representations of the same mathematical function, a cubic polynomial in x
.
Each of the three forms is preferable to the others in different situations. The first form, f
, is the most commonly used representation of a polynomial. It is simply a linear combination of the powers of x
. The second form, g
, is the factored form. It displays the roots of the polynomial and is the most accurate for numerical evaluation near the roots. But, if a polynomial does not have such simple roots, its factored form may not be so convenient. The third form, h
, is the Horner, or nested, representation. For numerical evaluation, it involves the fewest arithmetic operations and is the most accurate for some other ranges of x
.
The symbolic simplification problem involves the verification that these three expressions represent the same function. It also involves a less clearly defined objective -- which of these representations is "the simplest"?
This toolbox provides several functions that apply various algebraic and trigonometric identities to transform one representation of a function into another, possibly simpler, representation. These functions are collect
, expand
, horner
, factor
, simplify
, and simple
.
collect
The statement
collect(f)
views f
as a polynomial in its symbolic variable, say x
, and collects all the coefficients with the same power of x
. A second argument can specify the variable in which to collect terms if there is more than one candidate. Here are a few examples.
f
|
collect(f)
|
(x-1)*(x-2)*(x-3)
|
x^3-6*x^2+11*x-6
|
x*(x*(x-6)+11)-6
|
x^3-6*x^2+11*x-6
|
(1+x)*t + x*t
|
2*x*t+t
|
expand
The statement
expand(f)
distributes products over sums and applies other identities involving functions of sums. Here are a few examples.
f
|
expand(f)
|
a*(x + y)
|
a*x+a*y
|
(x-1)*(x-2)*(x-3)
|
x^3-6*x^2+11*x-6
|
x*(x*(x-6)+11)-6
|
x^3-6*x^2+11*x-6
|
exp(a+b)
|
exp(a)*exp(b)
|
cos(x+y)
|
cos(x)*cos(y)-sin(x)*sin(y)
|
cos(3*acos(x))
|
4*x^3-3*x
|
horner
The statement
horner(f)
transforms a symbolic polynomial f
into its Horner, or nested, representation. Here are a few examples.
f
|
horner(f)
|
x^3-6*x^2+11*x-6
|
-6+(11+(-6+x)*x)*x
|
1.1+2.2*x+3.3*x^2
|
11/10+(11/5+33/10*x)*x
|
factor
If f
is a polynomial with rational coefficients, the statement
factor(f)
expresses f
as a product of polynomials of lower degree with rational coefficients. If f
cannot be factored over the rational numbers, the result is f
itself. Here are a few examples.
f
|
factor(f)
|
x^3-6*x^2+11*x-6
|
(x-1)*(x-2)*(x-3)
|
x^3-6*x^2+11*x-5
|
x^3-6*x^2+11*x-5
|
x^6+1
|
(x^2+1)*(x^4-x^2+1)
|
Here is another example involving factor
. It factors polynomials of the form x^n + 1
. This code
syms x;
n = (1:9)';
p = x.^n + 1;
f = factor(p);
[p, f]
returns a matrix with the polynomials in its first column and their factored forms in its second.
[ x+1, x+1 ]
[ x^2+1, x^2+1 ]
[ x^3+1, (x+1)*(x^2-x+1) ]
[ x^4+1, x^4+1 ]
[ x^5+1, (x+1)*(x^4-x^3+x^2-x+1) ]
[ x^6+1, (x^2+1)*(x^4-x^2+1) ]
[ x^7+1, (x+1)*(1-x+x^2-x^3+x^4-x^5+x^6) ]
[ x^8+1, x^8+1 ]
[ x^9+1, (x+1)*(x^2-x+1)*(x^6-x^3+1) ]
As an aside at this point, we mention that factor
can also factor symbolic objects containing integers. This is an alternative to using the factor
function in MATLAB's specfun
directory. For example, the following code segment
N = sym(1);
for k = 2:11
N(k) = 10*N(k-1)+1;
end
[N' factor(N')]
displays the factors of symbolic integers consisting of 1s.
[ 1, 1]
[ 11, (11)]
[ 111, (3)*(37)]
[ 1111, (11)*(101)]
[ 11111, (41)*(271)]
[ 111111, (3)*(7)*(11)*(13)*(37)]
[ 1111111, (239)*(4649)]
[ 11111111, (11)*(73)*(101)*(137)]
[ 111111111, (3)^2*(37)*(333667)]
[ 1111111111, (11)*(41)*(271)*(9091)]
[ 11111111111, (513239)*(21649)]
simplify
The simplify
function is a powerful, general purpose tool that applies a number of algebraic identities involving sums, integral powers, square roots and other fractional powers, as well as a number of functional identities involving trig functions, exponential and log functions, Bessel functions, hypergeometric functions, and the gamma function. Here are some examples.
f
|
simplify(f)
|
x*(x*(x-6)+11)-6
|
x^3-6*x^2+11*x-6
|
(1-x^2)/(1-x)
|
x+1
|
(1/a^3+6/a^2+12/a+8)^(1/3)
|
((2*a+1)^3/a^3)^(1/3)
|
syms x y positive
log(x*y)
|
log(x)+log(y)
|
exp(x) * exp(y)
|
exp(x+y)
|
besselj(2,x) + besselj(0,x)
|
2/x*besselj(1,x)
|
gamma(x+1)-x*gamma(x)
|
0
|
cos(x)^2 + sin(x)^2
|
1
|
simple
The simple
function has the unorthodox mathematical goal of finding a simplification of an expression that has the fewest number of characters. Of course, there is little mathematical justification for claiming that one expression is "simpler" than another just because its ASCII representation is shorter, but this often proves satisfactory in practice.
The simple
function achieves its goal by independently applying simplify
, collect
, factor
, and other simplification functions to an expression and keeping track of the lengths of the results. The simple
function then returns the shortest result.
The simple
function has several forms, each returning different output. The form
simple(f)
displays each trial simplification and the simplification function that produced it in the MATLAB command window. The simple
function then returns the shortest result. For example, the command
simple(cos(x)^2 + sin(x)^2)
displays the following alternative simplifications in the MATLAB command window
simplify:
1
radsimp:
cos(x)^2+sin(x)^2
combine(trig):
1
factor:
cos(x)^2+sin(x)^2
expand:
cos(x)^2+sin(x)^2
convert(exp):
(1/2*exp(i*x)+1/2/exp(i*x))^2-1/4*(exp(i*x)-1/exp(i*x))^2
convert(sincos):
cos(x)^2+sin(x)^2
convert(tan):
(1-tan(1/2*x)^2)^2/(1+tan(1/2*x)^2)^2+4*tan(1/2*x)^2/
(1+tan(1/2*x)^2)^2
collect(x):
cos(x)^2+sin(x)^2
and returns
ans =
1
This form is useful when you want to check, for example, whether the shortest form is indeed the simplest. If you are not interested in how simple
achieves its result, use the form
f = simple(f)
This form simply returns the shortest expression found. For example, the statement
f = simple(cos(x)^2+sin(x)^2)
returns
f =
1
If you want to know which simplification returned the shortest result, use the multiple output form.
[F, how] = simple(f)
This form returns the shortest result in the first variable and the simplification method used to achieve the result in the second variable. For example, the statement
[f, how] = simple(cos(x)^2+sin(x)^2)
returns
f =
1
how =
combine
The simple
function sometimes improves on the result returned by simplify
, one of the simplifications that it tries. For example, when applied to the examples given for simplify
, simple
returns a simpler (or at least shorter) result in two cases.
f
|
simplify(f)
|
simple(f)
|
(1/a^3+6/a^2+12/a+8)^(1/3)
|
((2*a+1)^3/a^3)^(1/3)
|
(2*a+1)/a
|
syms x y positive
log(x*y)
|
log(x)+log(y)
|
log(x*y)
|
In some cases, it is advantageous to apply simple
twice to obtain the effect of two different simplification functions. For example, the statements
f = (1/a^3+6/a^2+12/a+8)^(1/3);
simple(simple(f))
return
1/a+2
The first application, simple(f)
, uses radsimp
to produce (2*a+1)/a
; the second application uses combine(trig)
to transform this to 1/a+2
.
The simple
function is particularly effective on expressions involving trigonometric functions. Here are some examples.
f
|
simple(f)
|
cos(x)^2+sin(x)^2
|
1
|
2*cos(x)^2-sin(x)^2
|
3*cos(x)^2-1
|
cos(x)^2-sin(x)^2
|
cos(2*x)
|
cos(x)+(-sin(x)^2)^(1/2)
|
cos(x)+i*sin(x)
|
cos(x)+i*sin(x)
|
exp(i*x)
|
cos(3*acos(x))
|
4*x^3-3*x
|
| Extended Calculus Example | | Substitutions |  |