subs
Symbolic substitution
Syntax
Description
Substitute Symbolic Scalar Variables and Functions
returns a copy of snew
= subs(s
,old
,new
)s
, replacing all occurrences of
old
with new
, and then evaluates
s
. Here, s
is an expression of symbolic scalar
variables or a symbolic function, and old
specifies the symbolic scalar
variables or symbolic function to be substituted.
If
old
andnew
are both vectors or cell arrays of the same size,subs
replaces each element ofold
with the corresponding element ofnew
.If
old
is a scalar, andnew
is a vector or matrix, thensubs(s,old,new)
replaces all instances ofold
ins
withnew
, performing all operations element-wise. All constant terms ins
are replaced with the constant multiplied by a vector or matrix of all ones.
returns a copy of snew
= subs(s
,new
)s
, replacing all occurrences of the default
symbolic scalar variable in s
with new
, and then
evaluates s
. The default variable is defined by symvar(s,1)
.
Substitute Symbolic Matrix Variables and Functions
returns a copy of sMnew
= subs(sM
,oldM
,newM
)sM
, replacing all occurrences of
oldM
with newM
, and then evaluates
sM
. Here, sM
is an expression, equation, or
condition involving symbolic matrix variables and matrix functions, and
oldM
specifies the symbolic matrix variables and matrix functions to
be substituted. The substitution values newM
must have the same size as
oldM
. (since R2021b)
Examples
Input Arguments
Tips
subs(s,__)
does not modifys
. To modifys
, uses = subs(s,__)
.If
s
is a univariate polynomial andnew
is a numeric matrix, usepolyvalm(sym2poly(s),new)
to evaluates
as a matrix. All constant terms are replaced with the constant multiplied by an identity matrix.Starting in R2022b, symbolic substitution involving derivatives or the
diff
function follows the input order of the symbolic objects to be substituted. For example, this code performs symbolic substitution involving thediff
function.Before R2022b, the code returns this output:syms m k x(t) syms x_t x_t_ddot eqSHM = m*diff(x(t),t,2) == -k*x(t); eqSHMnew = subs(eqSHM,[x(t) diff(x(t),t,2)],[x_t x_t_ddot])
eqSHMnew = m*x_t_ddot == -k*x_t
Starting in R2022b, the code returns this output:
The difference in output is due toeqSHMnew = 0 == -k*x_t
subs
now first substitutingx(t)
withx_t
, resulting indiff(x_t,t,2)
, which is equal to0
. To obtain the substitution result as in previous releases, specify thediff(x(t),t,2)
term first so that it is substituted before thex(t)
term.eqSHMnew = subs(eqSHM,[diff(x(t),t,2) x(t)],[x_t_ddot x_t])
eqSHMnew = m*x_t_ddot == -k*x_t