Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Algebraic simplification

`S = simplify(expr)`

`S = simplify(expr,Name,Value)`

performs
algebraic simplification of `S`

= simplify(`expr`

)`expr`

. If `expr`

is a symbolic vector or matrix, this function simplifies each element of
`expr`

.

performs algebraic simplification of `S`

= simplify(`expr`

,`Name,Value`

)`expr`

using additional
options specified by one or more `Name,Value`

pair
arguments.

Simplify these symbolic expressions:

syms x a b c S = simplify(sin(x)^2 + cos(x)^2) S = simplify(exp(c*log(sqrt(a+b))))

S = 1 S = (a + b)^(c/2)

Call `simplify`

for this symbolic
matrix. When the input argument is a vector or matrix, `simplify`

tries
to find a simpler form of each element of the vector or matrix.

syms x M = [(x^2 + 5*x + 6)/(x + 2), sin(x)*sin(2*x) + cos(x)*cos(2*x); (exp(-x*i)*i)/2 - (exp(x*i)*i)/2, sqrt(16)]; S = simplify(M)

S = [ x + 3, cos(x)] [ sin(x), 4]

Simplify a symbolic expression that contain logarithms and powers. By
default, `simplify`

does not combine powers and logarithms
because combining them is not valid for generic complex values.

syms x expr = (log(x^2 + 2*x + 1) - log(x + 1))*sqrt(x^2); S = simplify(expr)

S = -(log(x + 1) - log((x + 1)^2))*(x^2)^(1/2)

To apply the simplification rules that allow the `simplify`

function to
combine powers and logarithms, set `'IgnoreAnalyticConstraints'`

to
`true`

:

S = simplify(expr, 'IgnoreAnalyticConstraints', true)

S = x*log(x + 1)

Simplify this expression:

syms x expr = ((exp(-x*i)*i) - (exp(x*i)*i))/(exp(-x*i) + exp(x*i)); S = simplify(expr)

S = -(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1)

By default, `simplify`

uses one internal
simplification step. You can get different, often shorter, simplification
results by increasing the number of simplification steps:

S10 = simplify(expr,'Steps',10) S30 = simplify(expr,'Steps',30) S50 = simplify(expr,'Steps',50)

S10 = 2i/(exp(x*2i) + 1) - 1i S30 = ((cos(x) - sin(x)*1i)*1i)/cos(x) - 1i S50 = tan(x)

If you are unable to return the desired result, try alternate simplification functions. See Choose Function to Rearrange Expression.

Get equivalent results for a symbolic expression by setting
the value of `'All'`

to `true`

.

syms x expr = cos(x)^2 - sin(x)^2; S = simplify(expr,'All',true)

S = cos(2*x) cos(x)^2 - sin(x)^2

Increase the number of simplification steps to 10. Find the other equivalent results for the same expression.

S = simplify(expr,'Steps',10,'All',true)

S = cos(2*x) 1 - 2*sin(x)^2 2*cos(x)^2 - 1 cos(x)^2 - sin(x)^2 cot(2*x)*sin(2*x) exp(-x*2i)/2 + exp(x*2i)/2

Attempt to separate real and imaginary parts of an expression by setting the
value of `'Criterion'`

to
`'preferReal'`

.

syms x f = (exp(x + exp(-x*i)/2 - exp(x*i)/2)*i)/2 -... (exp(- x - exp(-x*i)/2 + exp(x*i)/2)*i)/2; S = simplify(f, 'Criterion','preferReal', 'Steps', 100)

S = sin(sin(x))*cosh(x) + cos(sin(x))*sinh(x)*1i

If `'Criterion'`

is not set to `'preferReal'`

, then
`simplify`

returns a shorter result but the real and
imaginary parts are not separated.

S = simplify(f,'Steps',100)

S = sin(sin(x) + x*1i)

When you set `'Criterion'`

to `'preferReal'`

, the simplifier
disfavors expression forms where complex values appear inside subexpressions. In
nested subexpressions, the deeper the complex value appears inside an expression,
the least preference this form of an expression gets.

Attempt to avoid imaginary terms in exponents by setting
`'Criterion'`

to `'preferReal'`

.

Show this behavior by simplifying a complex symbolic expression with and without setting
`'Criterion'`

to `'preferReal'`

. When
`'Criterion'`

is set to `'preferReal'`

, then
`simplify`

places the imaginary term outside the
exponent.

expr = sym(i)^(i+1); withoutPreferReal = simplify(expr,'Steps',100)

withoutPreferReal = (-1)^(1/2 + 1i/2)

withPreferReal = simplify(expr,'Criterion','preferReal','Steps',100)

withPreferReal = exp(-pi/2)*1i

Simplify expressions containing symbolic units
of the same dimension by using `simplify`

.

u = symunit; expr = 300*u.cm + 40*u.inch + 2*u.m; S = simplify(expr)

S = (3008/5)*[cm]

`simplify`

automatically chooses the unit
to rewrite into. To choose a specific unit, use `rewrite`

.

Simplification of 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 might be complicated or even unsuitable for another problem.

When you use `IgnoreAnalyticConstraints`

, then `simplify`

follows
these rules:

log(

*a*) + log(*b*) = log(*a*·*b*) for all values of*a*and*b*. In particular, the following equality is valid for all values of*a*,*b*, and*c*:(

*a*·*b*)^{c}=*a*^{c}·*b*^{c}.log(

*a*^{b}) =*b*·log(*a*) for all values of*a*and*b*. In particular, the following equality is valid for all values of*a*,*b*, and*c*:(

*a*^{b})^{c}=*a*^{b·c}.If

*f*and*g*are standard mathematical functions and*f*(*g*(*x*)) =*x*for all small positive numbers,*f*(*g*(*x*)) =*x*is assumed to be valid for all complex values of*x*. In particular:log(

*e*^{x}) =*x*asin(sin(

*x*)) =*x*, acos(cos(*x*)) =*x*, atan(tan(*x*)) =*x*asinh(sinh(

*x*)) =*x*, acosh(cosh(*x*)) =*x*, atanh(tanh(*x*)) =*x*W

_{k}(*x*·*e*^{x}) =*x*for all branch indices*k*of the Lambert W function.