Choose Function to Rearrange Expression
Combine Terms of Same Algebraic Structures
Symbolic Math Toolbox™ provides the combine
function
for combining subexpressions of an original expression. The combine
function
uses mathematical identities for the functions you specify. For example,
combine the trigonometric expression.
syms x y combine(2*sin(x)*cos(x),'sincos')
ans = sin(2*x)
If you do not specify a target function, combine
uses
the identities for powers wherever these identities are valid:
ab ac = ab + c
ac bc = (a b)c
(ab)c = abc
For example, by default the function combines the following square roots.
combine(sqrt(2)*sqrt(x))
ans = (2*x)^(1/2)
The function does not combine the square roots sqrt(x)*sqrt(y)
because the
identity is not valid for negative values of variables.
combine(sqrt(x)*sqrt(y))
ans = x^(1/2)*y^(1/2)
To combine these square roots, use the IgnoreAnalyticConstraints
option.
combine(sqrt(x)*sqrt(y),'IgnoreAnalyticConstraints',true)
ans = (x*y)^(1/2)
IgnoreAnalyticConstraints
provides a shortcut
allowing you to combine expressions under commonly used assumptions
about values of the variables. Alternatively, you can set appropriate
assumptions on variables explicitly. For example, assume that x
and y
are
positive values.
assume([x,y],'positive') combine(sqrt(x)*sqrt(y))
ans = (x*y)^(1/2)
For further computations, clear the assumptions on x
and
y
by recreating them using syms
.
syms x y
As target functions, combine
accepts atan
, exp
, gamma
, int
, log
, sincos
,
and sinhcosh
.
Expand Expressions
For elementary expressions, use the expand
function
to transform the original expression by multiplying sums of products.
This function provides an easy way to expand polynomials.
expand((x - 1)*(x - 2)*(x - 3))
ans = x^3 - 6*x^2 + 11*x - 6
expand(x*(x*(x - 6) + 11) - 6)
ans = x^3 - 6*x^2 + 11*x - 6
The function also expands exponential and logarithmic expressions. For example, expand the following expression containing exponentials.
expand(exp(x + y)*(x + exp(x - y)))
ans = exp(2*x) + x*exp(x)*exp(y)
Expand an expression containing logarithm. Expanding logarithms is not valid for generic complex values, but it is valid for positive values.
syms a b c positive expand(log(a*b*c))
ans = log(a) + log(b) + log(c)
For further computations, clear the assumptions.
syms a b c
Alternatively, use the IgnoreAnalyticConstraints
option
when expanding logarithms.
expand(log(a*b*c),'IgnoreAnalyticConstraints',true)
ans = log(a) + log(b) + log(c)
expand
also works on trigonometric expressions.
For example, expand this expression.
expand(cos(x + y))
ans = cos(x)*cos(y) - sin(x)*sin(y)
expand
uses mathematical identities between
the functions.
expand(sin(5*x))
ans = sin(x) - 12*cos(x)^2*sin(x) + 16*cos(x)^4*sin(x)
expand(cos(3*acos(x)))
ans = 4*x^3 - 3*x
expand
works recursively for all subexpressions.
expand((sin(3*x) + 1)*(cos(2*x) - 1))
ans = 2*sin(x) + 2*cos(x)^2 - 10*cos(x)^2*sin(x) + 8*cos(x)^4*sin(x) - 2
To prevent the expansion of all trigonometric, logarithmic,
and exponential subexpressions, use the option ArithmeticOnly
.
expand(exp(x + y)*(x + exp(x - y)),'ArithmeticOnly',true)
ans = exp(x - y)*exp(x + y) + x*exp(x + y)
expand((sin(3*x) + 1)*(cos(2*x) - 1),'ArithmeticOnly',true)
ans = cos(2*x) - sin(3*x) + cos(2*x)*sin(3*x) - 1
Factor Expressions
To return all irreducible factors of an expression, use the factor
function.
For example, find all irreducible polynomial factors of this polynomial
expression. The result shows that this polynomial has three roots: x
= 1
, x = 2
, and x = 3
.
syms x factor(x^3 - 6*x^2 + 11*x - 6)
ans = [ x - 3, x - 1, x - 2]
If a polynomial expression is irreducible, factor
returns
the original expression.
factor(x^3 - 6*x^2 + 11*x - 5)
ans = x^3 - 6*x^2 + 11*x - 5
Find irreducible polynomial factors of the expression x^6 + 1
. By default,
factor
uses factorization over rational numbers keeping rational
numbers in their exact symbolic form. The resulting factors for this expression do not show
polynomial roots.
factor(x^6 + 1)
ans = [ x^2 + 1, x^4 - x^2 + 1]
Using other factorization modes lets you factor this expression further. For example, factor the same expression over complex numbers.
factor(x^6 + 1,'FactorMode','complex')
ans = [ x + 0.86602540378443864676372317075294 + 0.5i,... x + 0.86602540378443864676372317075294 - 0.5i,... x + 1.0i,... x - 1.0i,... x - 0.86602540378443864676372317075294 + 0.5i,... x - 0.86602540378443864676372317075294 - 0.5i]
factor
also works on expressions other
than polynomials and rational expressions. For example, you can factor
the following expression that contains logarithm, sine, and cosine
functions. Internally, factor
converts such expressions
into polynomials and rational expressions by substituting subexpressions
with variables. After computing irreducible factors, the function
restores original subexpressions.
factor((log(x)^2 - 1)/(cos(x)^2 - sin(x)^2))
ans = [ log(x) - 1, log(x) + 1, 1/(cos(x) - sin(x)), 1/(cos(x) + sin(x))]
Use factor
to factor symbolic integers
and symbolic rational numbers.
factor(sym(902834092)) factor(1/sym(210))
ans = [ 2, 2, 47, 379, 12671] ans = [ 1/2, 1/3, 1/5, 1/7]
factor
also can factor numbers larger than flintmax
that the MATLAB® factor
cannot. To represent a large number
accurately, place the number in quotation marks.
factor(sym('41758540882408627201'))
ans = [ 479001599, 87178291199]
Extract Subexpressions from Expression
The children
function returns the subexpressions
of an expression.
Define an expression f
with several subexpressions.
syms x y f = exp(3*x)*y^3 + exp(2*x)*y^2 + exp(x)*y;
Extract the subexpressions of f
by using
children
.
expr = children(f)
expr = 1×3 cell array {[y^2*exp(2*x)]} {[y^3*exp(3*x)]} {[y*exp(x)]}
You can extract lower-level subexpressions by calling children
repeatedly
on the results.
Extract the subexpressions of expr{1}
by calling
children
on this expression.
expr1 = children(expr{1})
expr1 = 1×2 cell array {[y^2]} {[exp(2*x)]}
Access the contents of the cell array expr1
using braces.
expr1{1} expr1{2}
ans = y^2 ans = exp(2*x)
Collect Terms with Same Powers
If a mathematical expression contains terms with the same powers
of a specified variable or expression, the collect
function
reorganizes the expression by grouping such terms. When calling collect
,
specify the variables that the function must consider as unknowns.
The collect
function regards the original expression
as a polynomial in the specified unknowns, and groups the coefficients
with equal powers. Group the terms of an expression with the equal
powers of x
.
syms x y z expr = x*y^4 + x*z + 2*x^3 + x^2*y*z +... 3*x^3*y^4*z^2 + y*z^2 + 5*x*y*z; collect(expr, x)
ans = (3*y^4*z^2 + 2)*x^3 + y*z*x^2 + (y^4 + 5*z*y + z)*x + y*z^2
Group the terms of the same expression with the equal powers
of y
.
collect(expr, y)
ans = (3*x^3*z^2 + x)*y^4 + (x^2*z + 5*x*z + z^2)*y + 2*x^3 + z*x
Group the terms of the same expression with the equal powers
of z
.
collect(expr, z)
ans = (3*x^3*y^4 + y)*z^2 + (x + 5*x*y + x^2*y)*z + 2*x^3 + x*y^4
If you do not specify variables that collect
must
consider as unknowns, the function uses symvar
to
determine the default variable.
collect(expr)
ans = (3*y^4*z^2 + 2)*x^3 + y*z*x^2 + (y^4 + 5*z*y + z)*x + y*z^2
Collect terms of an expression with respect to several unknowns by specifying those unknowns as a vector.
collect(expr, [y,z])
ans = 3*x^3*y^4*z^2 + x*y^4 + y*z^2 + (x^2 + 5*x)*y*z + x*z + 2*x^3
Rewrite Expressions in Terms of Other Functions
To present an expression in terms of a particular function,
use rewrite
. This function uses mathematical
identities between functions. For example, rewrite an expression containing
trigonometric functions in terms of a particular trigonometric function.
syms x rewrite(sin(x),'tan')
ans = (2*tan(x/2))/(tan(x/2)^2 + 1)
rewrite(cos(x),'tan')
ans = -(tan(x/2)^2 - 1)/(tan(x/2)^2 + 1)
rewrite(sin(2*x) + cos(3*x)^2,'tan')
ans = (tan((3*x)/2)^2 - 1)^2/(tan((3*x)/2)^2 + 1)^2 +... (2*tan(x))/(tan(x)^2 + 1)
Use rewrite
to express these
trigonometric functions in terms of the exponential function.
rewrite(sin(x),'exp')
ans = (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2
rewrite(cos(x),'exp')
ans = exp(-x*1i)/2 + exp(x*1i)/2
Use rewrite
to express these
hyperbolic functions in terms of the exponential function.
rewrite(sinh(x),'exp')
ans = exp(x)/2 - exp(-x)/2
rewrite(cosh(x),'exp')
ans = exp(-x)/2 + exp(x)/2
rewrite
also expresses inverse hyperbolic
functions in terms of logarithms.
rewrite(asinh(x),'log')
ans = log(x + (x^2 + 1)^(1/2))
rewrite(acosh(x),'log')
ans = log(x + (x - 1)^(1/2)*(x + 1)^(1/2))
Compute Partial Fraction Decompositions of Expressions
The partfrac
function returns a rational
expression in the form of a sum of a polynomial and rational terms.
In each rational term, the degree of the numerator is smaller than
the degree of the denominator. For some expressions, partfrac
returns
visibly simpler forms.
syms x n = x^6 + 15*x^5 + 94*x^4 + 316*x^3 + 599*x^2 + 602*x + 247; d = x^6 + 14*x^5 + 80*x^4 + 238*x^3 + 387*x^2 + 324*x + 108; partfrac(n/d, x)
ans = 1/(x + 1) + 1/(x + 2)^2 + 1/(x + 3)^3 + 1
The denominators in rational terms represent the factored common denominator of the original expression.
factor(d)
ans = [ x + 1, x + 2, x + 2, x + 3, x + 3, x + 3]
Compute Normal Forms of Rational Expressions
The simplifyFraction
function
represents the original rational expression as a single rational term
with expanded numerator and denominator. The greatest common divisor
of the numerator and denominator of the returned expression is 1.
This function is more efficient for simplifying fractions than the simplify
function.
syms x y simplifyFraction((x^3 + 3*y^2)/(x^2 - y^2) + 3)
ans = (x^3 + 3*x^2)/(x^2 - y^2)
simplifyFraction
cancels common factors
that appear in numerator and denominator.
simplifyFraction(x^2/(x + y) - y^2/(x + y))
ans = x - y
simplifyFraction
also handles expressions
other than polynomials and rational functions. Internally, it converts
such expressions into polynomials or rational functions by substituting
subexpressions with identifiers. After normalizing the expression
with temporary variables, simplifyFraction
restores
the original subexpressions.
simplifyFraction((exp(2*x) - exp(2*y))/(exp(x) - exp(y)))
ans = exp(x) + exp(y)
Represent Polynomials Using Horner Nested Forms
The Horner, or nested, form of a polynomial expression is efficient for numerical evaluation
because it often involves fewer arithmetical operations compared to other mathematically
equivalent forms of the same polynomial. Typically, this form of an expression is numerically
stable. To represent a polynomial expression in a nested form, use the
horner
function.
syms x horner(x^3 - 6*x^2 + 11*x - 6)
ans = x*(x*(x - 6) + 11) - 6
If polynomial coefficients are floating-point numbers, the resulting Horner form represents them as rational numbers.
horner(1.1 + 2.2*x + 3.3*x^2)
ans = x*((33*x)/10 + 11/5) + 11/10
To convert the coefficients in the result to floating-point
numbers, use vpa
.
vpa(ans)
ans = x*(3.3*x + 2.2) + 1.1