subs
Substitutes values or expressions for symbolic variables.
Category: Symbolic Computations in MATLAB > Conversion Between Symbolic and Numeric
Syntax
r = subs(s, old, new)
Replaces old by new in the expression s.
r = subs(s, {old1, old2}, {new1, new2})
Makes several substitutions at once.
r = subs(s, new)
Substitutes new for the default variable of s.
r = subs(s)
Substitutes values the variables have in the workspace.
Description
The substitution is performed simultaneously and the result is evaluated: subs(x^2, x, 3) gives 9, and subs(x + y, x, 2) gives y + 2. If new is an array, the result is an array with one value per element, so a single call evaluates an expression on a whole set of points.
With one variable replaced by an expression in other variables, the result stays symbolic: subs(x^2, x, y + 1) gives (y + 1)^2. Exact numbers stay exact; pass a double to vpa or double afterwards if a decimal value is wanted.
Input arguments
s- Symbolic expression, vector or matrix.
old- Symbolic variable (or expression, or its name as text) to replace.
new- Number, expression, or array of values.
Output arguments
r- Expression with the substitution done. If every variable is replaced by a number, the result is a number.
Example
Numbers, expressions and several points
syms x y
subs(x + y, x, 2)
subs(x*y, {x, y}, {2, 3})
subs(x^2, x, y + 1)
subs(x^2 + 1, x, [1 2 3])
ans = y + 2
ans = 6
ans = (y + 1)^2
ans =
[2, 5, 10]