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 Version History.
Description
subexpr = children(
returns a nonnested
cell array containing the child subexpressions of the symbolic expression
expr
)expr
. For example, the child subexpressions of a sum are its
terms.
subexpr = children(
returns a nested cell
array containing the child subexpressions of each expression in the symbolic matrix
A
)A
.
Examples
Find Child Subexpressions of Symbolic Expression
Find the child subexpressions of the symbolic expression . 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
{[x*y]} {[x^2]} {[y^2]}
s1 = subexpr{1}
s1 =
s2 = subexpr{2}
s2 =
s3 = subexpr{3}
s3 =
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 =
s2 = children(x^2 + x*y + y^2,2)
s2 =
s3 = children(x^2 + x*y + y^2,3)
s3 =
To convert the cell array of subexpressions into a vector, you can use the command [subexpr{:}]
.
V = [subexpr{:}]
V =
Find Child Subexpressions of Equation
Find the child subexpressions of the equation . 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
{[x^2 + y*x]} {[y^2 + 1]}
subexpr{:}
ans =
ans =
Next, find the child subexpressions of the inequality . 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
{[sin(x)]} {[cos(x)]}
subexpr{:}
ans =
ans =
Find Child Subexpressions of Integral
Find the child subexpressions of an integral . 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
{[f(x)]} {[x]} {[a]} {[b]}
V = [subexpr{:}]
V =
Plot Taylor Approximation of Expression
Find the Taylor approximation of the function near .
syms x
t = taylor(cos(x),x,2)
t =
The Taylor expansion has six terms that are separated by or sign.
Plot the 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'})
Find Child Subexpressions of Elements of Matrix
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 =
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
{[x]} {[y]}
Unnest each element of s
using braces. Convert the nonnested cell arrays to vectors using square brackets.
s11vec = [s{1,1}{:}]
s11vec =
s21vec = [s{2,1}{:}]
s21vec =
s12vec = [s{1,2}{:}]
s12vec =
s22vec = [s{2,2}{:}]
s22vec =
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
{[x ]} {[cos(y) ]}
{[x^3]} {[exp(x*y^2)]}
[scol1{:}].'
ans =
scol2 = children(symM,2)
scol2=2×2 cell array
{[y ]} {[sin(x)]}
{[-y^3]} {[3 ]}
[scol2{:}].'
ans =
Input Arguments
expr
— Input expression
symbolic number | symbolic variable | symbolic function | symbolic expression
Input expression, specified as a symbolic number, variable, function, or expression.
A
— Input matrix
symbolic matrix
Input matrix, specified as a symbolic matrix.
ind
— Index of child subexpressions to return
positive integer
Index of child subexpressions to return, specified as a positive integer.
If
children(expr)
returns a nonnested cell array of child subexpressions, then indexing withchildren(expr,ind)
returns theind
-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 withchildren(A,ind)
returns theind
-th column of the nonnested cell array.
Version History
Introduced in R2012aR2020b: children
returns cell arrays
In versions before R2020b, the syntax subexpr =
children(
returns a vector
expr
)subexpr
that contains the child subexpressions of the scalar symbolic
expression expr
. The syntax subexpr =
children(
returns a nonnested cell array
A
)subexpr
that contains the child subexpressions of the symbolic matrix
A
.
Starting in R2020b, the syntax subexpr = children(expr)
returns
subexpr
as a cell array instead of a vector, and the syntax
subexpr = children(A)
returns subexpr
as a nested
cell array instead of a nonnested cell array. You can use subexpr =
children(expr,ind)
to index into the returned cell arrays of subexpressions. For
example, see Plot Taylor Approximation of Expression. You can also unnest
and access the elements of a cell array by indexing into the cell array using curly braces.
To convert subexpr
from a nonnested cell array to a vector, you can use
the command [subexpr{:}]
.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)