syms a b; subs(cos(a) + sin(b), {a, b}, {sym('alpha'), 2})
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);
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 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]
[