Symbolic substitution
collapse all in page
Syntax
snew = subs(s,old,new)
snew = subs(s,new)
snew = subs(s)
sMnew = subs(sM,oldM,newM)
sMnew = subs(sM,newM)
sMnew = subs(sM)
Description
Substitute Symbolic Scalar Variables and Functions
example
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.
example
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).
example
returns a copy of snew
= subs(s)s
, replacing symbolic scalar variables in s
with their assigned values in the MATLAB® workspace, and then evaluates s
. Variables with no assigned values remain as variables.
Substitute Symbolic Matrix Variables and Functions
example
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)
example
returns a copy of sMnew
= subs(sM,newM)sM
, replacing all occurrences of the default symbolic matrix variable in sM
with newM
, and then evaluates sM
. (since R2021b)
example
returns a copy of sMnew
= subs(sM)sM
, replacing symbolic matrix variables in sM
with their assigned values in the MATLAB workspace, and then evaluates sM
. Variables with no assigned values remain as variables. (since R2023b)
Examples
collapse all
Single Substitution
Open Live Script
Replace a
with 4
in this expression.
syms a bsubs(a + b,a,4)
ans =
Replace a*b
with 5
in this expression.
subs(a*b^2,a*b,5)
ans =
Default Substitution Variable
Open Live Script
Substitute the default symbolic scalar variable in this expression with a
. If you do not specify the scalar variable or expression to replace, subs
uses symvar
to find the default variable. For x + y
, the default variable is x
.
syms x y asymvar(x + y,1)
ans =
Therefore, subs
replaces x
with a
.
subs(x + y,a)
ans =
Evaluate Expression with New Values
Open Live Script
When you assign a new value to a symbolic scalar variable, expressions containing the variable are not automatically evaluated. Instead, evaluate expressions by using subs
.
Define the expression y = x^2
.
syms xy = x^2;
Assign 2
to x
. The value of y
is still x^2
instead of 4
.
x = 2;y
y =
Evaluate y
with the new value of x
by using subs
.
subs(y)
ans =
Multiple Substitutions
Open Live Script
Make multiple substitutions by specifying the old and new values as vectors.
syms a bsubs(cos(a) + sin(b), [a,b], [sym('alpha'),2])
ans =
Alternatively, for multiple substitutions, use cell arrays.
subs(cos(a) + sin(b), {a,b}, {sym('alpha'),2})
ans =
Substitute Scalars with Arrays
Open Live Script
Replace the symbolic scalar variable a
in this expression with the 3-by-3 magic square matrix. Note that the constant 1
expands to the 3-by-3 matrix with all its elements equal to 1
.
syms a tsubs(exp(a*t) + 1, a, -magic(3))
You can also replace an element of a vector, matrix, or array with a nonscalar value. For example, create these 2-by-2 matrices.
A = sym('A',[2,2])
A =
B = sym('B',[2,2])
B =
Replace the first element of the matrix A
with the matrix B
. While making this substitution, subs
expands the 2-by-2 matrix A
into this 4-by-4 matrix.
A44 = subs(A, A(1,1), B)
A44 =
subs
does not let you replace a nonscalar or matrix with a scalar that shrinks the matrix size.
Substitute Symbolic Scalar Variables in Structure Array
Open Live Script
Create a structure array with symbolic expressions as the field values.
syms x y zS = struct('f1',x*y,'f2',y + z,'f3',y^2)
S = struct with fields: f1: x*y f2: y + z f3: y^2
Replace the symbolic scalar variables x
, y
, and z
with numeric values.
Sval = subs(S,[x y z],[0.5 1 1.5])
Sval = struct with fields: f1: 1/2 f2: 5/2 f3: 1
Substitute Multiple Scalars with Arrays
Open Live Script
Replace the symbolic scalar variables x
and y
with these 2-by-2 matrices. When you make multiple substitutions involving vectors or matrices, use cell arrays to specify the old and new values.
syms x ysubs(x*y, {x,y}, {[0 1; -1 0], [1 -1; -2 1]})
ans =
Note that because x
and y
are scalars, these substitutions are element-wise.
[0 1; -1 0].*[1 -1; -2 1]
ans = 2×2 0 -1 2 0
Substitutions in Equations
Open Live Script
Eliminate scalar variables from an equation by using the variable's value from another equation. In the second equation, isolate the variable on the left side using isolate
, and then substitute the right side with the variable in the first equation.
First, declare the equations eqn1
and eqn2
.
syms x yeqn1 = sin(x)+y == x^2 + y^2;eqn2 = y*x == cos(x);
Isolate y
in eqn2
by using isolate
.
eqn2 = isolate(eqn2,y)
eqn2 =
Eliminate y
from eqn1
by substituting the left side of eqn2
with the right side of eqn2
.
eqn1 = subs(eqn1,lhs(eqn2),rhs(eqn2))
eqn1 =
Substitutions in Functions
Open Live Script
Replace x
with a
in this symbolic function.
syms x y asyms f(x,y)f(x,y) = x + y;f = subs(f,x,a)
f(x, y) =
subs
replaces the values in the symbolic function formula, but it does not replace input arguments of the function.
formula(f)
ans =
argnames(f)
ans =
Replace the arguments of a symbolic function explicitly.
syms x yf(x,y) = x + y;f(a,y) = subs(f,x,a);f
f(a, y) =
Substitute Variables with Corresponding Values from Structure
Open Live Script
Suppose you want to verify the solutions of this system of equations.
syms x yeqs = [x^2 + y^2 == 1, x == y];S = solve(eqs,[x y]);S.x
ans =
S.y
ans =See Also?入 - MATLAB subs MathWorks 日本
Verify the solutions by substituting the solutions into the original system.
isAlways(subs(eqs,S))
ans = 2x2 logical array 1 1 1 1
Substitute Symbolic Matrix Variables with Arrays
Since R2021b
Open Live Script
Define the product of two 2-by-2 matrices. Declare the matrices as symbolic matrix variables with the symmatrix
data type.
syms X Y [2 2] matrixsM = X*Y
sM =
Replace the matrix variables and with 2-by-2 symbolic matrices. When you make multiple substitutions involving vectors or matrices, use cell arrays to specify the matrix variables to be substituted and their new values. The new values must have the same size as the matrix variables to be substituted.
S = subs(sM,{X,Y},{[0 sqrt(sym(2)); sqrt(sym(2)) 0], [1 -1; -2 1]})
S =
Convert the expression S
to the sym
data type to show the result of the substituted matrix multiplication.
Ssym = symmatrix2sym(S)
Ssym =
Characteristic Polynomial of Matrix
Since R2021b
Open Live Script
Create a matrix of symbolic numbers.
A = sym([1 4 2; 4 1 2; 2 2 3])
A =
Compute the coefficients of the characteristic polynomial of A
using the charpoly
function.
c = charpoly(A)
c =
Next, define as a 3-by-3 symbolic matrix variable. Use the coefficients c
to create the polynomial , where is an indeterminate that represents a 3-by-3 matrix.
syms X [3 3] matrixp = c(1)*X^3 + c(2)*X^2 + c(3)*X + c(4)*X^0
p =
Substitute in the polynomial with A
using the subs
function. According to the Cayley-Hamilton theorem, this substitution results in a 3-by-3 zero matrix because the coefficients c
are the characteristic polynomial of A
. Use symmatrix2sym
to convert the substituted expression to a matrix of symbolic numbers.
Y = subs(p,A)
Y =
Z = symmatrix2sym(Y)
Z =
Substitute Variables in Symbolic Matrix Function
Since R2022a
Open Live Script
Define the function , where is a 2-by-2 matrix and is a 2-by-2 identity matrix. Substitute the variable with another expression and evaluate the new function.
Create a 2-by-2 symbolic matrix variable . Create a symbolic matrix function , keeping the existing definition of in the workspace. Assign the polynomial expression of .
syms A 2 matrixsyms f(A) 2 matrix keepargsf(A) = A*A - 2*A + eye(2)
f(A) =
Next, create new symbolic matrix variables and . Create a new symbolic matrix function , keeping the existing definitions of and in the workspace.
syms B C 2 matrixsyms g(B,C) 2 matrix keepargs
Substitute the variable in with . Assign the substituted result to the new function .
g(B,C) = subs(f,A,B+C)
g(B, C) =
Evaluate for the matrix values and using subs
.
S = subs(g(B,C),{B,C},{[0 1; -1 0],[1 -1; -2 1]})
S =
Convert the expression S
from the symmatrix
data type to the sym
data type to show the result of the substituted polynomial.
Ssym = symmatrix2sym(S)
Ssym =
Substitute Symbolic Matrix Variables and Matrix Functions in Equation
Since R2022b
Open Live Script
Define the equation , where is a 3-by-3 matrix and is a 3-by-1 matrix. Substitute with another symbolic expression and with symbolic values. Check if the equation is true for these values.
Create two symbolic matrix variables and . Create a symbolic matrix function , keeping the existing definitions of and in the workspace. Create the equation.
syms A [3 3] matrixsyms X [3 1] matrixsyms f(X,A) [1 1] matrix keepargseq = diff(diff(f,X),X.') == 2*A
eq(X, A) =
Substitute with and evaluate the second-order differential function in the equation for this expression.
eq = subs(eq,f,X.'*A*X)
eq(X, A) =
Substitute with the Hilbert matrix of order 3.
eq = subs(eq,A,hilb(3))
eq(X, A) =
Check if the equation is true for these values by using isAlways
. Because isAlways
accepts only a symbolic input of type symfun
or sym
, convert eq
from type symfunmatrix
to type symfun
before using isAlways
.
tf = isAlways(symfunmatrix2symfun(eq))
tf = 3x3 logical array 1 1 1 1 1 1 1 1 1
Evaluate Expression Involving Symbolic Matrix Variables
Since R2023b
Open Live Script
Define the expression , where and are 3-by-3 matrices. Create the matrices as symbolic matrix variables.
syms X Y [3 3] matrixC = X*Y^2 - Y*X^2
C =
Assign values to the matrices X
and Y
.
X = [-1 2 pi; 0 1/2 2; 2 1 0];Y = [3 2 2; -1 2 1; 1 2 -1];
Evaluate the expression C
with the assigned values of X
and Y
by using subs
.
Cnew = subs(C)
Cnew =
Convert the result from the symmatrix
data type to the double
data type.
Cnum = double(Cnew)
Cnum = 3×3 -42.8496 -13.3584 -13.4336 -0.7168 3.1416 0.0752 -3.2832 29.8584 16.4248
Input Arguments
collapse all
s
— Symbolic input
symbolic scalar variable | symbolic expression | symbolic equation | symbolic function | symbolic array | symbolic matrix | structure
Symbolic input, specified as a symbolic scalar variable, expression, equation, function, array, matrix, or a structure.
Data Types: sym
| symfun
| struct
old
— Scalar variable to substitute
symbolic scalar variable | symbolic function | symbolic expression | symbolic array | cell array
Scalar variable to substitute, specified as a symbolic scalar variable, function, expression, array, or a cell array.
Data Types: sym
| symfun
| cell
new
— New value
number | symbolic number | symbolic scalar variable | symbolic function | symbolic expression | symbolic array | structure | cell array
New value to substitute with, specified as a number, symbolic number, scalar variable, function, expression, array, structure, or a cell array.
Data Types: sym
| symfun
| single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| struct
| cell
sM
— Symbolic input
symbolic matrix variable | symbolic matrix function | symbolic expression | symbolic equation | symbolic condition
Symbolic input, specified as a symbolic matrix variable, matrix function, expression, equation, or condition.
Data Types: symmatrix
| symfunmatrix
oldM
— Matrix variable or function to substitute
symbolic matrix variable | symbolic matrix function | symbolic expression | cell array
Matrix variable or function to substitute, specified as a symbolic matrix variable, matrix function, expression, or a cell array.
Data Types: symmatrix
| symfunmatrix
| cell
newM
— New value
number | symbolic number | symbolic matrix variable | symbolic matrix function | symbolic expression | symbolic array | cell array
New value to substitute with, specified as a number, symbolic number, matrix variable, matrix function, expression, array, or a cell array. newM
must have the same size as oldM
or the default symbolic matrix variable in sM
.
Data Types: sym
| symmatrix
| symfunmatrix
| single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| struct
| cell
Tips
subs(s,__)
does not modify s. To modifys
, uses = subs(s,__)
.If
s
is a univariate polynomial and new 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_ddoteqSHM = 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
Version History
Introduced before R2006a
expand all
R2023b: Substitute symbolic matrix variables with their workspace values
You can use the syntax subs(sM)
to substitute the symbolic matrix variables and matrix functions in sM
with their assigned values in the MATLAB workspace and then evaluate sM
. Variables with no assigned values remain as variables. For an example, see Evaluate Expression Involving Symbolic Matrix Variables.
R2022b: Substitute symbolic matrix variables and matrix functions in symbolic equations or conditions
The subs
function accepts a symbolic equation or condition of type symmatrix
or symfunmatrix
as the first input argument. For an example, see Substitute Symbolic Matrix Variables and Matrix Functions in Equation.
R2022a: Substitute variables in symbolic matrix functions
The subs
function accepts a symbolic matrix function of type symfunmatrix
as the first input argument. For an example, see Substitute Variables in Symbolic Matrix Function.
R2021b: Substitute symbolic matrix variables in symbolic expressions
The subs
function accepts a symbolic expression of type symmatrix
as the first input argument. For examples, see Substitute Symbolic Matrix Variables with Arrays and Characteristic Polynomial of Matrix.
See Also
Functions
- double | lhs | rhs | simplify | subexpr | vpa
Topics
- Substitutions in Symbolic Expressions
- Substitute Variables in Symbolic Expressions
- Substitute Elements in Symbolic Matrices
- Substitute Scalars with Matrices
- Evaluate Symbolic Expressions Using subs
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office