Main Content

mapSymType

Apply function to symbolic subobjects of specific type

Description

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)).

example

Examples

collapse all

Create a symbolic expression that contains symbolic numbers using sym.

expr = sym('2') + 1i*pi
expr = 2+π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+π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 = πei+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 = 

x2+sin(x)=t4

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+sin(2x)=t2

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+sin(2x)=t2

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 = 

x2+g(sin(x))=t4

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(x)-2y+sin(x)

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

X = mapSymType(expr,'expression',@log)
X = log(f(x)-2y+sin(x))

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 = 2g(t)+f(x)+sin(x)h(x,t)

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 = 2g(t)+f(x)+zsin(x)

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

X = mapSymType(expr,'symfunDependingOn',x,func)
X = z+2g(t)+zsin(x)

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 = 

(f1(t)+f2(t)=0f1(t)=2g1(t)g1(t)=t g2(t))

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 = (f1+f2=0f1=2g1g1=0)

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 = 

19e-tcos(3t)+3sin(3t)1939-19e2tcosh(3t)-183sinh(3t)1939

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 = 

19e-tcos(3t)+3sin(3t)1939-19e2te3t2+e-3t2-183e3t2-e-3t21939

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.

Version History

Introduced in R2019a