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.

Here are their prettyprinted forms, generated by

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

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

distributes products over sums and applies other identities involving functions of sums as shown in the examples below.

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

transforms a symbolic polynomial f into its Horner, or nested, representation as shown in the following 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

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 several 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

returns a matrix with the polynomials in its first column and their factored forms in its second.

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 the MATLAB specfun directory. For example, the following code segment

displays the factors of symbolic integers consisting of 1s:

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

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

displays the following alternative simplifications in the MATLAB command window

and returns

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

This form simply returns the shortest expression found. For example, the statement

returns

If you want to know which simplification returned the shortest result, use the multiple output form:

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

returns

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

return

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