mapSymType

Apply function to symbolic subobjects of specific type

Description

example

X = mapSymType(symObj,type,func) applies the function func to the symbolic subobjects of type type in the symbolic object symObj. The input type must be a case-sensitive string scalar or character vector, and it can include a logical expression.

• func must be a function handle or a symbolic function of type 'symfun'.

• func must return a scalar that can be converted to a symbolic object using the sym or str2sym function.

If symObj contains several subexpressions of type type, then mapSymType applies the function func to the largest subexpression.

example

X = mapSymType(symObj,funType,vars,func) applies the function func to the unassigned symbolic functions that depend on the variables vars in the symbolic object symObj.

You can set the function type funType to 'symfunOf' or 'symfunDependingOn'. For example, syms f(x); mapSymType(f,'symfunOf',x,@(u)cos(u)) returns cos(f(x)).

Examples

collapse all

Create a symbolic expression that contains symbolic numbers using sym.

expr = sym('2') + 1i*pi
expr = $2+\pi \mathrm{i}$

Construct a function handle that computes the square of a number.

sq = @(y) y^2;

Apply the function sq to the symbolic subobject of type 'integer' in the expression expr.

X = mapSymType(expr,'integer',sq)
X = $4+\pi \mathrm{i}$

You can also apply an existing MATLAB® function, such as exp. Apply the exp function to the symbolic subobject of type 'complex' in the expression expr.

X = mapSymType(expr,'complex',@exp)
X = $\pi {\mathrm{e}}^{\mathrm{i}}+2$

Apply a symbolic function to specific subobjects in a symbolic equation.

Create a symbolic equation.

syms x t
eq = 0.5*x + sin(x) == t/4
eq =

$\frac{x}{2}+\mathrm{sin}\left(x\right)=\frac{t}{4}$

Construct a symbolic function that multiplies an input by 2.

syms f(u)
f(u) = 2*u;

Apply the symbolic function f to the symbolic subobjects of type 'variable' in the equation eq.

X = mapSymType(eq,'variable',f)
X =

$x+\mathrm{sin}\left(2 x\right)=\frac{t}{2}$

The symbolic variables x and t in the equation are multiplied by 2.

You can also apply the same symbolic function that is created using symfun.

X = mapSymType(eq,'variable',symfun(2*u,u))
X =

$x+\mathrm{sin}\left(2 x\right)=\frac{t}{2}$

Now create an unassigned symbolic function. Apply the unassigned function to the symbolic subobjects of type 'sin' in the equation eq.

syms g(u)
X = mapSymType(eq,'sin',g)
X =

$\frac{x}{2}+g\left(\mathrm{sin}\left(x\right)\right)=\frac{t}{4}$

Convert the largest symbolic subexpression of specific type in an expression.

Create a symbolic expression.

syms f(x) y
expr = sin(x) + f(x) - 2*y
expr = $f\left(x\right)-2 y+\mathrm{sin}\left(x\right)$

Apply the log function to the symbolic subobject of type 'expression' in the expression expr.

X = mapSymType(expr,'expression',@log)
X = $\mathrm{log}\left(f\left(x\right)-2 y+\mathrm{sin}\left(x\right)\right)$

When there are several subexpressions of type 'expression', mapSymType applies the log function to the largest subexpression.

Convert unassigned symbolic functions with specific variable dependencies in an expression.

Create a symbolic expression.

syms f(x) g(t) h(x,t)
expr = f(x) + 2*g(t) + h(x,t)*sin(x)
expr = $2 g\left(t\right)+f\left(x\right)+\mathrm{sin}\left(x\right) h\left(x,t\right)$

Construct a function handle that converts an input to a symbolic variable with name 'z'.

func = @(obj) sym('z');

Apply the conversion function func to the unassigned symbolic functions in the expression expr.

Convert the functions that depend on the exact sequence of variables [x t] using 'symfunOf'.

X = mapSymType(expr,'symfunOf',[x t],func)
X = $2 g\left(t\right)+f\left(x\right)+z \mathrm{sin}\left(x\right)$

Convert the functions that have a dependency on the variable t using 'symfunDependingOn'.

X = mapSymType(expr,'symfunDependingOn',x,func)
X = $z+2 g\left(t\right)+z \mathrm{sin}\left(x\right)$

Remove variable dependency of unassigned symbolic functions in a symbolic array.

Create a symbolic array consisting of multiple equations.

syms f1(t) f2(t) g1(t) g2(t)
eq = [f1(t) + f2(t) == 0, f1(t) == 2*g1(t), g1(t) == diff(g2(t))]
eq =

Apply the symFunType function to replace an unassigned symbolic function with a variable of the same name.

Find all functions that have a dependency on the variable t using 'symfunOf' and convert them using symFunType.

X = mapSymType(eq,'symfunOf',t,@symFunType)
X = $\left(\begin{array}{ccc}{f}_{1}+{f}_{2}=0& {f}_{1}=2 {g}_{1}& {g}_{1}=0\end{array}\right)$

Create a symbolic expression. Find its inverse Laplace transform.

syms s;
G = (s+10)/(s^2+2*s+4)/(s^2-4*s+1);
expr = ilaplace(G)
expr =

$\frac{19 {\mathrm{e}}^{-t} \left(\mathrm{cos}\left(\sqrt{3} t\right)+\frac{\sqrt{3} \mathrm{sin}\left(\sqrt{3} t\right)}{19}\right)}{39}-\frac{19 {\mathrm{e}}^{2 t} \left(\mathrm{cosh}\left(\sqrt{3} t\right)-\frac{18 \sqrt{3} \mathrm{sinh}\left(\sqrt{3} t\right)}{19}\right)}{39}$

The result is in terms of the exp, sin, cos, sinh, and cosh functions.

Rewrite sinh and cosh in the result as exp. Use mapSymType to apply the rewrite function to subexpressions that contain sinh or cosh.

expr = mapSymType(expr,"sinh|cosh",@(subexpr) rewrite(subexpr,"exp"))
expr =

$\frac{19 {\mathrm{e}}^{-t} \left(\mathrm{cos}\left(\sqrt{3} t\right)+\frac{\sqrt{3} \mathrm{sin}\left(\sqrt{3} t\right)}{19}\right)}{39}-\frac{19 {\mathrm{e}}^{2 t} \left(\frac{{\mathrm{e}}^{\sqrt{3} t}}{2}+\frac{{\mathrm{e}}^{-\sqrt{3} t}}{2}-\frac{18 \sqrt{3} \left(\frac{{\mathrm{e}}^{\sqrt{3} t}}{2}-\frac{{\mathrm{e}}^{-\sqrt{3} t}}{2}\right)}{19}\right)}{39}$

Input Arguments

collapse all

Symbolic objects, specified as symbolic expressions, symbolic functions, symbolic variables, symbolic numbers, or symbolic units.

Symbolic types, specified as a case-sensitive scalar string or character vector. The input type can contain a logical expression. The value options follow.

Symbolic Type CategoryString Values
numbers
• 'integer' — integer numbers

• 'rational' — rational numbers

• 'vpareal' — variable-precision floating-point real numbers

• 'complex' — complex numbers

• 'real' — real numbers, including 'integer', 'rational', and 'vpareal'

• 'number' — numbers, including 'integer', 'rational', 'vpareal', 'complex', and 'real'

constants'constant' — symbolic mathematical constants, including 'number'
symbolic math functions'vpa', 'sin', 'exp', and so on — symbolic math functions in symbolic expressions
unassigned symbolic functions
• 'F', 'g', and so on — function name of an unassigned symbolic function

• 'symfun' — unassigned symbolic functions

arithmetic operators
• 'plus' — addition operator + and subtraction operator -

• 'times' — multiplication operator * and division operator /

• 'power' — power or exponentiation operator ^ and square root operator sqrt

variables'variable' — symbolic variables
units'unit' — symbolic units
expressions'expression' — symbolic expressions, including all of the preceding symbolic types
logical expressions
• 'or' — logical OR operator |

• 'and' — logical AND operator &

• 'not' — logical NOT operator ~

• 'xor' — logical exclusive-OR operator xor

• 'logicalconstant' — symbolic logical constants symtrue and symfalse

• 'logicalexpression' — logical expressions, including 'or', 'and', 'not', 'xor', symtrue and symfalse

equations and inequalities
• 'eq' — equality operator ==

• 'ne' — inequality operator ~=

• 'lt' — less-than operator < or greater-than operator >

• 'le' — less-than-or-equal-to operator <= or greater-than-or-equal-to operator >=

• 'equation' — symbolic equations and inequalities, including 'eq', 'ne', 'lt', and 'le'

unsupported symbolic types

'unsupported' — unsupported symbolic types

Input function, specified as a function handle or symbolic function. For more information about function handles and symbolic function, see Create Function Handle and symfun, respectively.

If symObj contains several subexpressions of type type, then mapSymType applies the function func to the largest subexpression (topmost matching node in a tree data structure).

Function type, specified as 'symfunOf' or 'symfunDependingOn'.

• 'symfunOf' applies func to the unassigned symbolic functions that depend on the exact sequence of variables specified by the array vars. For example, syms f(x,y); mapSymType(f,'symfunOf',[x y],@(g)g^2) returns f(x,y)^2.

• 'symfunDependingOn' applies func to the unassigned symbolic functions that have a dependency on the variables specified by the array vars. For example, syms f(x,y); mapSymType(f,'symfunDependingOn',x,@(g)g/2) returns f(x,y)/2.

Input variables, specified as symbolic variables or a symbolic array.