Expand expressions and simplify inputs of functions by using identities



expand(S) multiplies all parentheses in S, and simplifies inputs to functions such as cos(x + y) by applying standard identities.


expand(S,Name,Value) uses additional options specified by one or more name-value pair arguments. For example, specifying 'IgnoreAnalyticConstraints' as true uses convenient identities to simplify the input.


collapse all

syms x
p = (x - 2)*(x - 4);
ans =
x^2 - 6*x + 8

Expand the trigonometric expression cos(x + y). Simplify the cos function input x + y to x or y by applying standard identities.

syms x y
expand(cos(x + y))
ans =
cos(x)*cos(y) - sin(x)*sin(y)

Expand e(a + b)2. Simplify the exp function input, (a + b)^2, by applying standard identities.

syms a b
f = exp((a + b)^2);
ans =

Expand expressions in a vector. Simplify the inputs to functions in the expressions by applying identities.

syms t
V = [sin(2*t), cos(2*t)];
ans =
[ 2*cos(t)*sin(t), 2*cos(t)^2 - 1]

By default, expand both expands terms raised to powers and expands functions by applying identities that simplify inputs to the functions. Expand only terms raised to powers and suppress expansion of functions by using 'ArithmeticOnly'.

Expand (sin(3*x) - 1)^2. By default, expand will expand the power ^2 and simplify the sin input 3*x to x.

syms x
f = (sin(3*x) - 1)^2;
ans =
2*sin(x) + sin(x)^2 - 8*cos(x)^2*sin(x) - 8*cos(x)^2*sin(x)^2...
 + 16*cos(x)^4*sin(x)^2 + 1

Suppress expansion of functions, such as sin(3*x), by setting ArithmeticOnly to true.

expand(f, 'ArithmeticOnly', true)
ans =
sin(3*x)^2 - 2*sin(3*x) + 1

Simplify the input of log function calls. By default, expand does not simplify logarithm input because the identities used are not valid for complex values of variables.

syms a b c
f = log((a*b/c)^2);
ans =

Apply identities to simplify the input of logarithms by setting 'IgnoreAnalyticConstraints' to true.

ans =
 2*log(a) + 2*log(b) - 2*log(c)

Input Arguments

collapse all

Input, specified as a number, vector, matrix, or array, or a symbolic number, variable, array, function, or expression.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: expand(S,'ArithmeticOnly',true)

Expand only algebraic expressions, specified as the comma-separated pair consisting of 'ArithmeticOnly' and true or false. If the value is true, the function expands the arithmetic part of an expression without expanding trigonometric, hyperbolic, logarithmic, and special functions. This option does not prevent the expansion of powers and roots.

Use convenient identities for simplification, specified as the comma-separated pair consisting of 'IgnoreAnalyticConstraints' and true or false.

Setting 'IgnoreAnalyticConstraints' to true can give you simpler solutions, which could lead to results not generally valid. In other words, this option applies mathematical identities that are convenient, but the results might not hold for all values of the variables. In some cases, this option can let expand return simpler results that might not be equivalent to the initial expression. See Algorithms.


When you use 'IgnoreAnalyticConstraints', expand applies 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 = ac·bc.

  • log(ab) = 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:

      (ab)c = ab·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 x.

    • log(ex) = 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

    • Wk(x·ex) = x for all values of k

Introduced before R2006a