Simplification of a mathematical expression is not a clearly defined subject. There is no universal idea as to which form of an expression is simplest. The form of a mathematical expression that is simplest for one problem turns out to be complicated or even unsuitable for another problem. For example, the following two mathematical expressions present the same polynomial in different forms:
(x + 1)(x - 2)(x + 3)(x - 4),
2x3 - 13x2 +
14x + 24.
The first form clearly shows the roots of this polynomial. This form is simpler for working with the roots. The second form serves best when you want to see the coefficients of the polynomial. For example, this form is convenient when you differentiate or integrate polynomials.
If the problem you want to solve requires a particular form of an expression, the best approach is to choose the appropriate simplification function. See Choose Function to Rearrange Expression.
Besides specific simplifiers, Symbolic Math Toolbox™ offers
a general simplifier,
If you do not need a particular form of expressions (expanded,
factored, or expressed in particular terms), use
shorten mathematical expressions. For example, use this simplifier
to find a shorter form for a final result of your computations.
simplify works on various types of symbolic
expressions, such as polynomials, expressions with trigonometric,
logarithmic, and special functions. For example, simplify these polynomials.
syms x y simplify((1 - x^2)/(1 - x)) simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))
ans = x + 1 ans = x^12 - 1
Simplify expressions involving trigonometric functions.
simplify(cos(x)^(-2) - tan(x)^2) simplify(cos(x)^2 - sin(x)^2)
ans = 1 ans = cos(2*x)
Simplify expressions involving exponents and logarithms. In
the third expression, use
log(sym(3)) instead of
If you use
log(3), then MATLAB® calculates
the double precision, and then converts the result to a symbolic number.
simplify(exp(x)*exp(y)) simplify(exp(x) - exp(x/2)^2) simplify(log(x) + log(sym(3)) - log(3*x) + (exp(x) - 1)/(exp(x/2) + 1))
ans = exp(x + y) ans = 0 ans = exp(x/2) - 1
Simplify expressions involving special functions.
simplify(gamma(x + 1) - x*gamma(x)) simplify(besselj(2, x) + besselj(0, x))
ans = 0 ans = (2*besselj(1, x))/x
You also can simplify symbolic functions by using
syms f(x,y) f(x,y) = exp(x)*exp(y) f = simplify(f)
f(x, y) = exp(x)*exp(y) f(x, y) = exp(x + y)
simplify uses strict simplification rules and ensures that
simplified expressions are always mathematically equivalent to initial expressions. For
example, it does not combine logarithms for complex values in general.
syms x simplify(log(x^2) + log(x))
ans = log(x^2) + log(x)
You can apply additional simplification rules which are not
correct for all values of parameters and all cases, but using which
return shorter results. For this approach, use
For example, simplifying the same expression with
you get the result with combined logarithms.
simplify(log(x^2) + log(x),'IgnoreAnalyticConstraints',true)
ans = 3*log(x)
IgnoreAnalyticConstraints provides a shortcut
allowing you to simplify expressions under commonly used assumptions
about values of the variables. Alternatively, you can set appropriate
assumptions on variables explicitly. For example, combining logarithms
is not valid for complex values in general. If you assume that
a real value,
simplify combines logarithms without
assume(x,'real') simplify(log(x^2) + log(x))
ans = log(x^3)
For further computations, clear the assumption on
x by recreating it using
Another approach that can improve simplification of an expression
or function is the syntax
n is a positive integer that controls how
simplify takes. Specifying more simplification
steps can help you simplify the expression better, but it takes more
time. By default,
n = 1. For example, create and
simplify this expression. The result is shorter than the original
expression, but it can be simplified further.
syms x y = (cos(x)^2 - sin(x)^2)*sin(2*x)*(exp(2*x) - 2*exp(x) + 1)/... ((cos(2*x)^2 - sin(2*x)^2)*(exp(2*x) - 1)); simplify(y)
ans = (sin(4*x)*(exp(x) - 1))/(2*cos(4*x)*(exp(x) + 1))
Specify the number of simplification steps for the same expression. First, use 25 steps.
ans = (tan(4*x)*(exp(x) - 1))/(2*(exp(x) + 1))
Use 50 steps to simplify the expression even further.
ans = (tan(4*x)*tanh(x/2))/2
Suppose, you already simplified an expression or function, but you want the other forms of the
same expression. To do this, you can set the
'All' option to
true. The syntax
simplify(f,'Steps',n,'All',true) shows other equivalent results
of the same expression in the simplification steps.
syms x y = cos(x) + sin(x) simplify(y,'Steps',10,'All',true)
ans = 2^(1/2)*sin(x + pi/4) 2^(1/2)*cos(x - pi/4) cos(x) + sin(x) 2^(1/2)*((exp(- x*1i - (pi*1i)/4)*1i)/2 - (exp(x*1i + (pi*1i)/4)*1i)/2)
To return even more equivalent results, increase the number of steps to 25.
ans = 2^(1/2)*sin(x + pi/4) 2^(1/2)*cos(x - pi/4) cos(x) + sin(x) -2^(1/2)*(2*sin(x/2 - pi/8)^2 - 1) 2^(1/2)*(exp(- x*1i + (pi*1i)/4)/2 + exp(x*1i - (pi*1i)/4)/2) 2^(1/2)*((exp(- x*1i - (pi*1i)/4)*1i)/2 - (exp(x*1i + (pi*1i)/4)*1i)/2)
Some expressions cannot be simplified in general, but become much shorter under particular assumptions. For example, simplifying this trigonometric expression without additional assumptions returns the original expression.
syms n simplify(sin(2*n*pi))
ans = sin(2*pi*n)
However, if you assume that variable
an integer, the same trigonometric expression simplifies to 0.
ans = 0
For further computations, clear the assumption.
You can use the general simplification function,
to simplify fractions. However, Symbolic Math Toolbox offers a
more efficient function specifically for this task:
simplifyFraction(f) represents the
f as a fraction, where both the numerator
and denominator are polynomials whose greatest common divisor is 1.
For example, simplify these expressions.
syms x y simplifyFraction((x^3 - 1)/(x - 1))
ans = x^2 + x + 1
simplifyFraction((x^3 - x^2*y - x*y^2 + y^3)/(x^3 + y^3))
ans = (x^2 - 2*x*y + y^2)/(x^2 - x*y + y^2)
simplifyFraction does not expand
expressions in the numerator and denominator of the returned result.
To expand the numerator and denominator in the resulting expression,
Expand option. For comparison, first simplify
this fraction without
simplifyFraction((1 - exp(x)^4)/(1 + exp(x))^4)
ans = (exp(2*x) - exp(3*x) - exp(x) + 1)/(exp(x) + 1)^3
Now, simplify the same expressions with
simplifyFraction((1 - exp(x)^4)/(1 + exp(x))^4,'Expand',true)
ans = (exp(2*x) - exp(3*x) - exp(x) + 1)/(3*exp(2*x) + exp(3*x) + 3*exp(x) + 1)