Main Content

children

Subexpressions or terms of symbolic expression

Starting in R2020b, the syntax subexpr = children(expr) for a scalar input expr returns subexpr as a nonnested cell array instead of a vector. You can use subexpr = children(expr,ind) to index into the returned cell array of subexpressions. For more information, see Compatibility Considerations.

Description

example

subexpr = children(expr) returns a nonnested cell array containing the child subexpressions of the symbolic expression expr. For example, the child subexpressions of a sum are its terms.

example

subexpr = children(A) returns a nested cell array containing the child subexpressions of each expression in the symbolic matrix A.

example

subexpr = children(___,ind) returns the child subexpressions of a symbolic expression expr or a symbolic matrix A as a cell array indexed by ind.

Examples

collapse all

Find the child subexpressions of the symbolic expression x2+xy+y2. The subexpressions are returned in a nonnested cell array. children uses internal sorting rules when returning the subexpressions. You can index into each element of the cell array by using subexpr{i}, where i is the cell index. The child subexpressions of a sum are its terms.

syms x y
subexpr = children(x^2 + x*y + y^2)
subexpr=1×3 cell array
    {1x1 sym}    {1x1 sym}    {1x1 sym}

s1 = subexpr{1}
s1 = xyx*y
s2 = subexpr{2}
s2 = x2x^2
s3 = subexpr{3}
s3 = y2y^2

You can also index into each element of the subexpressions by specifying the index ind in the children function.

s1 = children(x^2 + x*y + y^2,1)
s1 = xyx*y
s2 = children(x^2 + x*y + y^2,2)
s2 = x2x^2
s3 = children(x^2 + x*y + y^2,3)
s3 = y2y^2

To convert the cell array of subexpressions into a vector, you can use the command [subexpr{:}].

V = [subexpr{:}]
V = (xyx2y2)[x*y, x^2, y^2]

Find the child subexpressions of the equation x2+xy=y2+1. The child subexpressions of the equation are returned in a 1-by-2 cell array. Index into all elements of the cell array. The subexpressions of an equation are the left and right sides of that equation.

syms x y
subexpr = children(x^2 + x*y == y^2 + 1)
subexpr=1×2 cell array
    {1x1 sym}    {1x1 sym}

subexpr{:}
ans = x2+yxx^2 + y*x
ans = y2+1y^2 + 1

Next, find the child subexpressions of the inequality sin(x)<cos(x). Index into all elements of the returned cell array. The child subexpressions of an inequality are the left and right sides of that inequality.

subexpr = children(sin(x) < cos(x))
subexpr=1×2 cell array
    {1x1 sym}    {1x1 sym}

subexpr{:}
ans = sin(x)sin(x)
ans = cos(x)cos(x)

Find the child subexpressions of an integral abf(x)dx. The child subexpressions are returned as a cell array of symbolic expressions.

syms f(x) a b
subexpr = children(int(f(x),a,b))
subexpr=1×4 cell array
    {1x1 sym}    {1x1 sym}    {1x1 sym}    {1x1 sym}

V = [subexpr{:}]
V = (f(x)xab)[f(x), x, a, b]

Find the Taylor approximation of the cos(x) function near x=2.

syms x
t = taylor(cos(x),x,2)
t = 

cos(2)+sin(2)x-236-sin(2)x-25120-sin(2)x-2-cos(2)x-222+cos(2)x-2424cos(sym(2)) + (sin(sym(2))*(x - 2)^3)/6 - (sin(sym(2))*(x - 2)^5)/120 - sin(sym(2))*(x - 2) - (cos(sym(2))*(x - 2)^2)/2 + (cos(sym(2))*(x - 2)^4)/24

The Taylor expansion has six terms that are separated by + or sign.

Plot the cos(x) function. Use children to separate out the terms of the expansion. Show that the Taylor expansion approximates the function more closely as more terms are included.

fplot(cos(x),[0 4])
hold on
s = 0;
for i = 1:6
    s = s + children(t,i);
    fplot(s,[0 4],'--')
end
legend({'cos(x)','1 term','2 terms','3 terms','4 terms','5 terms','6 terms'})

Call the children function to find the child subexpressions of the following symbolic matrix input. The result is a 2-by-2 nested cell array containing the child subexpressions of each element of the matrix.

syms x y
symM = [x + y, sin(x)*cos(y); x^3 - y^3, exp(x*y^2) + 3]
symM = 

(x+ycos(y)sin(x)x3-y3exy2+3)[x + y, cos(y)*sin(x); x^3 - y^3, exp(x*y^2) + 3]

s = children(symM)
s=2×2 cell array
    {1x2 cell}    {1x2 cell}
    {1x2 cell}    {1x2 cell}

To unnest or access the elements of the nested cell array s, use braces. For example, the {1,1}-element of s is a 1-by-2 cell array of symbolic expressions.

s11 = s{1,1}
s11=1×2 cell array
    {1x1 sym}    {1x1 sym}

Unnest each element of s using braces. Convert the nonnested cell arrays to vectors using square brackets.

s11vec = [s{1,1}{:}]
s11vec = (xy)[x, y]
s21vec = [s{2,1}{:}]
s21vec = (x3-y3)[x^3, -y^3]
s12vec = [s{1,2}{:}]
s12vec = (cos(y)sin(x))[cos(y), sin(x)]
s22vec = [s{2,2}{:}]
s22vec = (exy23)[exp(x*y^2), sym(3)]

If each element of the nested cell array s contains a nonnested cell array of the same size, then you can also use the ind input argument to access the elements of the nested cell array. The index ind allows children to access each column of subexpressions of the symbolic matrix input symM.

scol1 = children(symM,1)
scol1=2×2 cell array
    {1x1 sym}    {1x1 sym}
    {1x1 sym}    {1x1 sym}

[scol1{:}].'
ans = 

(xx3cos(y)exy2)[x; x^3; cos(y); exp(x*y^2)]

scol2 = children(symM,2)
scol2=2×2 cell array
    {1x1 sym}    {1x1 sym}
    {1x1 sym}    {1x1 sym}

[scol2{:}].'
ans = 

(y-y3sin(x)3)[y; -y^3; sin(x); sym(3)]

Input Arguments

collapse all

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

Input matrix, specified as a symbolic matrix.

Index of child subexpressions to return, specified as a positive integer.

  • If children(expr) returns a nonnested cell array of child subexpressions, then indexing with children(expr,ind) returns the ind-th element of the cell array.

  • If children(A) returns a nested cell array of child subexpressions, where each cell element has the same size, then indexing with children(A,ind) returns the ind-th column of the nonnested cell array.

Compatibility Considerations

expand all

Behavior changed in R2020b

Introduced in R2012a