Main Content

expand

Expand expressions and simplify inputs of functions by using identities

Description

example

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

example

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.

Examples

collapse all

syms x
p = (x - 2)*(x - 4);
expand(p)
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);
expand(f)
ans =
exp(a^2)*exp(b^2)*exp(2*a*b)

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)];
expand(V)
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;
expand(f)
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);
expand(f)
ans =
log((a^2*b^2)/c^2)

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

expand(f,'IgnoreAnalyticConstraints',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 Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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.

Algorithms

When you use 'IgnoreAnalyticConstraints', expand applies some of 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

Version History

Introduced before R2006a