Symbolic Math Toolbox | Search Help Desk |

subs | Examples See Also |

Symbolic substitution in a symbolic expression or matrix.

Syntax

R = subs(S) R = subs(S,old,new)

Description

`subs(S)`

replaces all occurrences of variables in the symbolic expression `S`

with values obtained from the calling function, or the MATLAB workspace.
`subs(S,old,new)`

replaces `old`

with `new`

in the symbolic expression `S`

. `old`

is a symbolic variable or a string representing a variable name. `new`

is a symbolic or numeric variable or expression.
If `old`

and `new`

are cell arrays of the same size, each element of `old`

is replaced by the corresponding element of `new`

. If `S`

and `old`

are scalars and `new`

is an array or cell array, the scalars are expanded to produce an array result. If `new`

is a cell array of numeric matrices, the substitutions are performed elementwise (i.e., `subs(x*y,{x,y},{A,B})`

returns `A.*B`

when `A`

and `B`

are numeric).
If `subs(s,old,new)`

does not change `s`

, `subs(s,new,old)`

is tried. This provides backwards compatibility with previous versions and eliminates the need to remember the order of the arguments. `subs(s,old,new)`

does not switch the arguments if `s`

does not change.
Examples

y = dsolve('Dy = -a*y')produces

y = exp(-a*t)*C1Then the statement

subs(y)produces

ans = 3*exp(-980*t)

`subs(a+b,a,4)`

returns `4+b`

.
`subs(cos(a)+sin(b),{a,b},{sym('alpha'),2})`

returns
cos(alpha)+sin(2)

`subs(exp(a*t),'a',-magic(2))`

returns
[ exp(-t), exp(-3*t)] [ exp(-4*t), exp(-2*t)]

`subs(x*y,{x,y},{[0 1;-1 0],[1 -1;-2 1]})`

returns
[ 0, -1] [ 2, 0]

See Also

`simplify`

, `subexpr`