mapSymType
Apply function to symbolic subobjects of specific type
Description
applies the function X
= mapSymType(symObj
,type
,func
)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.
If symObj
contains several subexpressions of type
type
, then mapSymType
applies the function
func
to the largest subexpression.
applies the function X
= mapSymType(symObj
,funType
,vars
,func
)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
Apply Function to Symbolic Numbers in Expression
Create a symbolic expression that contains symbolic numbers using sym
.
expr = sym('2') + 1i*pi
expr =
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 =
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 =
Apply Symbolic Function to Symbolic Subobjects in Equation
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 =
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 =
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 =
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 =
Apply Function to Largest Subexpression of Specific Type
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 =
Apply the log
function to the symbolic subobject of type 'expression'
in the expression expr
.
X = mapSymType(expr,'expression',@log)
X =
When there are several subexpressions of type 'expression'
, mapSymType
applies the log
function to the largest subexpression.
Symbolic Functions of Specific Variables
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 =
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 =
Convert the functions that have a dependency on the variable t
using 'symfunDependingOn'
.
X = mapSymType(expr,'symfunDependingOn',x,func)
X =
Remove Variable Dependency of Symbolic Functions
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 =
Rewrite Symbolic Expression
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 =
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 =
Input Arguments
symObj
— Symbolic objects
symbolic expressions | symbolic functions | symbolic variables | symbolic numbers | symbolic units
Symbolic objects, specified as symbolic expressions, symbolic functions, symbolic variables, symbolic numbers, or symbolic units.
type
— Symbolic types
scalar string | character vector
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 Category | String Values |
---|---|
numbers |
|
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 |
|
arithmetic operators |
|
variables | 'variable' — symbolic variables |
units | 'unit' — symbolic units |
expressions | 'expression' — symbolic expressions, including all of
the preceding symbolic types |
logical expressions |
|
equations and inequalities |
|
unsupported symbolic types |
|
func
— Input function
function handle | symbolic function
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).
funType
— Function type
'symfunOf'
| 'symfunDependingOn'
Function type, specified as 'symfunOf'
or
'symfunDependingOn'
.
'symfunOf'
appliesfunc
to the unassigned symbolic functions that depend on the exact sequence of variables specified by the arrayvars
. For example,syms f(x,y); mapSymType(f,'symfunOf',[x y],@(g)g^2)
returnsf(x,y)^2
.'symfunDependingOn'
appliesfunc
to the unassigned symbolic functions that have a dependency on the variables specified by the arrayvars
. For example,syms f(x,y); mapSymType(f,'symfunDependingOn',x,@(g)g/2)
returnsf(x,y)/2
.
vars
— Input variables
symbolic variables | symbolic array
Input variables, specified as symbolic variables or a symbolic array.
Version History
Introduced in R2019a
See Also
symFunType
| isSymType
| symType
| sym
| syms
| findSymType
| hasSymType
| str2sym
| symfun
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)