Documentation

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 = -6 + (11 + (-6 + x)*x)*x;

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, and simplify.

collect

The statementcollect(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)

syms x
f = (x-1)*(x-2)*(x-3);
collect(f)
ans =
x^3 - 6*x^2 + 11*x - 6
syms x
f = x*(x*(x - 6) + 11) - 6;
collect(f)
ans =
x^3 - 6*x^2 + 11*x - 6
syms x t
f = (1+x)*t + x*t;
collect(f)
ans =
(2*t)*x + t

expand

The statement expand(f) distributes products over sums and applies other identities involving functions of sums as shown in the examples below.

f

expand(f)

syms a x y
f = a*(x + y);
expand(f)
ans =
a*x + a*y
syms x
f = (x - 1)*(x - 2)*(x - 3);
expand(f)
ans =
x^3 - 6*x^2 + 11*x - 6
syms x
f = x*(x*(x - 6) + 11) - 6;
expand(f)
ans =
x^3 - 6*x^2 + 11*x - 6
syms a b
f = exp(a + b);
expand(f)
ans =
exp(a)*exp(b)
syms x y
f = cos(x + y);
expand(f)
ans =
cos(x)*cos(y) - sin(x)*sin(y)
syms x
f = cos(3*acos(x));
expand(f)
ans =
4*x^3 - 3*x
syms x
f = 3*x*(x^2 - 1) + x^3;
expand(f)
ans =
4*x^3 - 3*x

horner

The statement horner(f) transforms a symbolic polynomial f into its Horner, or nested, representation as shown in the following examples.

f

horner(f)

syms x
f = x^3 - 6*x^2 + 11*x - 6;
horner(f)
ans =
x*(x*(x - 6) + 11) - 6
syms x
f = 1.1 + 2.2*x + 3.3*x^2;
horner(f)
ans =
x*((33*x)/10 + 11/5) + 11/10

factor

If f is a polynomial with rational coefficients, the statement

factor(f)

expresses f as a vector of factors 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)

syms x
f = x^3 - 6*x^2 + 11*x - 6;
factor(f)
ans =
[ x - 3, x - 1, x - 2]
syms x
f = x^3 - 6*x^2 + 11*x - 5;
factor(f)
ans =
x^3 - 6*x^2 + 11*x - 5
syms x
f = x^6 + 1;
factor(f)
ans =
[ x^2 + 1, x^4 - x^2 + 1]

As an aside at this point, factor can also factor symbolic objects containing integers. This is an alternative to using the MATLAB® factor function. For example:

N = sym(902834092);
f = factor(N)
f =
[ 2, 2, 47, 379, 12671]

factor can also factor numbers larger than flintmax that the MATLAB factor cannot. For example, to perform prime factorization of a large number, place it in quotation marks to represent it accurately.

n = sym('41758540882408627201');
factor(n)
ans =
[ 479001599, 87178291199]

simplifyFraction

The statement simplifyFraction(f) represents the expression f as a fraction where both the numerator and denominator are polynomials whose greatest common divisor is 1. The Expand option lets you expand the numerator and denominator in the resulting expression.

simplifyFraction is significantly more efficient for simplifying fractions than the general simplification function simplify.

f

simplifyFraction(f)

syms x
f =(x^3 - 1)/(x - 1);
simplifyFraction(f)
ans =
x^2 + x + 1
syms x
f = (x^3 - x^2*y - x*y^2 + y^3)/(x^3 + y^3);
simplifyFraction(f)
ans =
(x^2 - 2*x*y + y^2)/(x^2 - x*y + y^2)
syms x
f = (1 - exp(x)^4)/(1 + exp(x))^4;
simplifyFraction(f)
ans =
(exp(2*x) - exp(3*x) - exp(x) + 1)/(exp(x) + 1)^3
simplifyFraction(f, 'Expand', true)
ans =
(exp(2*x) - exp(3*x) - exp(x) + 1)/(3*exp(2*x) + exp(3*x) + 3*exp(x) + 1)

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)

syms x
f = (1 - x^2)/(1 - x);
simplify(f)
ans =
x + 1
syms a
f = (1/a^3 + 6/a^2 + 12/a + 8)^(1/3);
simplify(f)
ans =
((2*a + 1)^3/a^3)^(1/3)
syms x y
f = exp(x) * exp(y);
simplify(f)
ans =
exp(x + y)
syms x
f = besselj(2, x) + besselj(0, x);
simplify(f)
ans =
(2*besselj(1, x))/x
syms x
f = gamma(x + 1) - x*gamma(x);
simplify(f)
ans =
0
syms x
f = cos(x)^2 + sin(x)^2;
simplify(f)
ans =
1

You can also use the syntax simplify(f, 'Steps', n) where n is a positive integer that controls how many steps simplify takes. By default, n = 1. For example,

syms x
z = (cos(x)^2 - sin(x)^2)*sin(2*x)*(exp(2*x) - 2*exp(x) + 1)/(exp(2*x) - 1);
simplify(z)
ans =
(sin(4*x)*(exp(x) - 1))/(2*(exp(x) + 1))
simplify(z, 'Steps', 30)
ans =
(sin(4*x)*tanh(x/2))/2
Was this topic helpful?