Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## 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 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)

```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)```

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.

```ans =
[   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)*(x^6 - x^5 + x^4 - x^3 + x^2 - x + 1)]
[ 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, factor can also factor symbolic objects containing integers. This is an alternative to using the factor function in the MATLAB® specfun folder. 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:

```ans =
[           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,   21649*513239]```

### 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```