Main Content

subs

Symbolic substitution

Description

Substitute Symbolic Scalar Variables and Functions

snew = subs(s,match,replacement) returns a copy of s, replacing all occurrences of match with replacement, and then evaluates s. Here, s is an expression of symbolic scalar variables or a symbolic function, and match specifies the symbolic scalar variables or symbolic function to be substituted.

  • If match and replacement are both vectors or cell arrays of the same size, subs replaces each element of match with the corresponding element of replacement.

  • If match is a scalar, and replacement is a vector or matrix, then subs(s,match,replacement) replaces all instances of match in s with replacement, performing all operations element-wise. All constant terms in s are replaced with the constant multiplied by a vector or matrix of all ones.

example

snew = subs(s,replacement) returns a copy of s, replacing all occurrences of the default symbolic scalar variable in s with replacement, and then evaluates s. The default variable is defined by symvar(s,1).

example

snew = subs(s) returns a copy of 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.

example

Substitute Symbolic Matrix Variables and Functions

sMnew = subs(sM,matchM,replacementM) returns a copy of sM, replacing all occurrences of matchM with replacementM, and then evaluates sM. Here, sM is an expression, equation, or condition involving symbolic matrix variables and matrix functions, and matchM specifies the symbolic matrix variables and matrix functions to be substituted. The substitution values replacementM must have the same size as matchM. (since R2021b)

example

sMnew = subs(sM,replacementM) returns a copy of sM, replacing all occurrences of the default symbolic matrix variable in sM with replacementM, and then evaluates sM. (since R2021b)

example

sMnew = subs(sM) returns a copy of 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)

example

Examples

collapse all

Replace a with 4 in this expression.

syms a b
expr = subs(a + b,a,4)
expr = b+4

Replace a*b with 5 in this expression.

expr = subs(a*b^2,a*b,5)
expr = 5b

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 a
var = symvar(x + y,1)
var = x

Therefore, subs replaces x with a.

expr = subs(x + y,a)
expr = a+y

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 x
y = x^2;

Assign 2 to x. The value of y is still x^2 instead of 4.

x = 2;
y
y = x2

Evaluate y with the new value of x by using subs.

yeval = subs(y)
yeval = 4

Make multiple substitutions by specifying the variables to substitute and the new values as vectors.

syms a b
expr = subs(cos(a) + sin(b), [a,b], [sym("alpha"),2])
expr = sin(2)+cos(α)

Alternatively, for multiple substitutions, use cell arrays.

expr = subs(cos(a) + sin(b), {a,b}, {sym("alpha"),2})
expr = sin(2)+cos(α)

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 t
expr3by3 = subs(exp(a*t) + 1, a, -magic(3))
expr3by3 = 

(e-8t+1e-t+1e-6t+1e-3t+1e-5t+1e-7t+1e-4t+1e-9t+1e-2t+1)

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 = 

(A1,1A1,2A2,1A2,2)

B = sym("B",[2,2])
B = 

(B1,1B1,2B2,1B2,2)

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.

A4by4 = subs(A, A(1,1), B)
A4by4 = 

(B1,1B1,2A1,2A1,2B2,1B2,2A1,2A1,2A2,1A2,1A2,2A2,2A2,1A2,1A2,2A2,2)

subs does not let you replace a nonscalar or matrix with a scalar that shrinks the matrix size.

Create a structure array with symbolic expressions as the field values.

syms x y z
S = 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

Replace the symbolic scalar variables x and y in an expression with these 2-by-2 matrices. When you make multiple substitutions involving vectors or matrices, use cell arrays to specify the variables to substitute and the new values.

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

(0-120)

Note that because x and y are scalars, these substitutions are element-wise.

expr = [0 1; -1 0].*[1 -1; -2 1]
expr = 2×2

     0    -1
     2     0

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 y
eqn1 = sin(x)+y == x^2 + y^2;
eqn2 = y*x == cos(x);

Isolate y in eqn2 by using isolate.

eqn2 = isolate(eqn2,y)
eqn2 = 

y=cos(x)x

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 = 

sin(x)+cos(x)x=cos(x)2x2+x2

Replace x with a in this symbolic function.

syms x y a
syms f(x,y)
f(x,y) = x + y;
f = subs(f,x,a)
f(x, y) = a+y

subs replaces the values in the symbolic function formula, but it does not replace input arguments of the function.

formula = formula(f)
formula = a+y
args = argnames(f)
args = (xy)

Replace the arguments of a symbolic function explicitly.

syms x y
f(x,y) = x + y;
f(a,y) = subs(f,x,a);
f
f(a, y) = a+y

Suppose you want to verify the solutions of this system of equations.

syms x y
eqs = [x^2 + y^2 == 1, x == y];
S = solve(eqs,[x y]);
S.x
ans = 

(-2222)

S.y
ans = 

(-2222)

Verify the solutions by substituting the solutions into the original system.

tf = isAlways(subs(eqs,S))
tf = 2×2 logical array

   1   1
   1   1

Create a symbolic expression that involves first-order and second-order derivatives.

syms r(t)
expr = diff(r,t,t) + diff(r,t)
expr(t) = 

2t2 r(t)+t r(t)

Substitute the first-order derivative with another variable v0 and the second-order derivative with another variable D2r. To do so, you must first substitute the derivative with the higher order, which is the second-order derivative.

syms v_0 D2r
exprnew = subs(expr,diff(r,t,t),D2r);
exprnew = subs(exprnew,diff(r,t),v_0)
exprnew(t) = D2r+v0

You can also make these substitutions in a single subs call by specifying the variables to replace and their replacements as vectors.

exprnew = subs(expr,[diff(r,t,t) diff(r,t)],[D2r v_0])
exprnew(t) = D2r+v0

If you want to substitute only the first-order derivative while keeping the second-order derivative intact, you can continue with the previous workflow by changing the temporary variable D2r back to 2t2r(t).

exprnew = subs(exprnew,D2r,diff(r,t,t))
exprnew(t) = 

2t2 r(t)+v0

For comparison, if you first substitute the first-order derivative in the original expression with v0, then the result is v0. Here, subs replaces the first-order derivative with v0 and the second-order derivative with tr(t)t=tv0=0. This substitution results in an expression that does not contain a second-order derivative.

syms v_0
exprnew = subs(expr,diff(r,t),v_0)
exprnew(t) = v0

Since R2021b

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] matrix
sM = X*Y
sM = XY

Replace the matrix variables X and Y 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 = 

Σ1where  Σ1=(-2222-2)

Convert the expression S to the sym data type to show the result of the substituted matrix multiplication.

Ssym = symmatrix2sym(S)
Ssym = 

(-2222-2)

Since R2021b

Create a matrix of symbolic numbers.

A = sym([1 4 2; 4 1 2; 2 2 3])
A = 

(142412223)

Compute the coefficients of the characteristic polynomial of A using the charpoly function.

c = charpoly(A)
c = (1-5-1721)

Next, define X as a 3-by-3 symbolic matrix variable. Use the coefficients c to create the polynomial p(X)=c1X3+c2X2+c3X+c4I3, where X is an indeterminate that represents a 3-by-3 matrix.

syms X [3 3] matrix
p = c(1)*X^3 + c(2)*X^2 + c(3)*X + c(4)*X^0
p = 21I3-17X-5X2+X3

Substitute X in the polynomial p(X) 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 = 

-17Σ1-5Σ12+Σ13+21I3where  Σ1=(142412223)

Z = symmatrix2sym(Y)
Z = 

(000000000)

Since R2022a

Define the function f(A)=A2-2A+I2, where A is a 2-by-2 matrix and I2 is a 2-by-2 identity matrix. Substitute the variable A with another expression and evaluate the new function.

Create a 2-by-2 symbolic matrix variable A. Create a symbolic matrix function f(A), keeping the existing definition of A in the workspace. Assign the polynomial expression of f(A).

syms A 2 matrix
syms f(A) 2 matrix keepargs
f(A) = A*A - 2*A + eye(2)
f(A) = I2-2A+A2

Next, create new symbolic matrix variables B and C. Create a new symbolic matrix function g(B,C), keeping the existing definitions of B and C in the workspace.

syms B C 2 matrix
syms g(B,C) 2 matrix keepargs

Substitute the variable A in f(A) with B+C. Assign the substituted result to the new function g(B,C).

g(B,C) = subs(f,A,B+C)
g(B, C) = B+C2+I2-2B-2C

Evaluate g(B,C) for the matrix values B=[01-10] and C=[1-1-21] using subs.

S = subs(g(B,C),{B,C},{[0 1; -1 0],[1 -1; -2 1]})
S = 

-2Σ1-2Σ2+Σ1+Σ22+I2where  Σ1=(01-10)  Σ2=(1-1-21)

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 = 

(0000)

Since R2022b

Define the equation XT X f(X,A)=2A, where A is a 3-by-3 matrix and X is a 3-by-1 matrix. Substitute f(X,A) with another symbolic expression and A with symbolic values. Check if the equation is true for these values.

Create two symbolic matrix variables A and X. Create a symbolic matrix function f(X,A), keeping the existing definitions of A and X in the workspace. Create the equation.

syms A [3 3] matrix
syms X [3 1] matrix
syms f(X,A) [1 1] matrix keepargs
eq = diff(diff(f,X),X.') == 2*A
eq(X, A) = 

XT X f(X,A)=2A

Substitute f(X,A) with XTAX and evaluate the second-order differential function in the equation for this expression.

eq = subs(eq,f,X.'*A*X)
eq(X, A) = AT+A=2A

Substitute A with the Hilbert matrix of order 3.

eq = subs(eq,A,hilb(3))
eq(X, A) = 

Σ1+Σ1T=2Σ1where  Σ1=(11213121314131415)

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 = 3×3 logical array

   1   1   1
   1   1   1
   1   1   1

Since R2023b

Define the expression XY2-YX2, where X and Y are 3-by-3 matrices. Create the matrices as symbolic matrix variables.

syms X Y [3 3] matrix
C = X*Y^2 - Y*X^2
C = XY2-YX2

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 = 

-Σ1Σ22+Σ2Σ12where  Σ1=(322-12112-1)  Σ2=(-12π0122210)

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

Symbolic input, specified as a symbolic scalar variable, expression, equation, function, array, matrix, or a structure.

Data Types: sym | symfun | struct

Scalar variable to substitute, specified as a symbolic scalar variable, function, expression, array, or a cell array.

Data Types: sym | symfun | cell

New replacement 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

Symbolic input, specified as a symbolic matrix variable, matrix function, expression, equation, or condition.

Data Types: symmatrix | symfunmatrix

Matrix variable or function to substitute, specified as a symbolic matrix variable, matrix function, expression, or a cell array.

Data Types: symmatrix | symfunmatrix | cell

New replacement value to substitute with, specified as a number, symbolic number, matrix variable, matrix function, expression, array, or a cell array. replacementM must have the same size as matchM 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 modify s, use s = subs(s,__).

  • If s is a univariate polynomial and replacement is a numeric matrix, use polyvalm(sym2poly(s),replacement) to evaluate s as a matrix. All constant terms are replaced with the constant multiplied by an identity matrix.

Version History

Introduced before R2006a

expand all