Main Content

subs

R2026b

Symbolic substitution

Description

Substitute Variables, Functions, or Subexpressions

exprnew = subs(expr,match,replacement) replaces all occurrences of match in expr with replacement, evaluates the expression, and returns the result as a symbolic expression.

  • If match and replacement are both vectors or cell arrays of the same size, then 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 replaces all instances of match with replacement, performing all operations element-wise. The function replaces all constant terms in expr with the constant multiplied by a vector or matrix of all ones.

  • If expr involves symbolic matrix variables or matrix functions, then replacement and match must be the same size.

example

exprnew = subs(expr,replacement) replaces all occurrences of the default symbolic variable in expr with replacement, evaluates the expression, and returns the result as a symbolic expression. The default variable is defined by symvar(expr,1).

example

exprnew = subs(expr) replaces symbolic variables and functions in expr with their assigned values in the MATLAB® workspace, evaluates the expression, and returns the result as a symbolic expression. Variables with no assigned values remain as variables.

example

Substitute Using Pattern Matching

Since R2026b

exprnew = subs(expr,match,replacement,Wildcards=vars) performs pattern-based substitution by finding subexpressions in expr that match the pattern match and replacing them with replacement, treating the variables specified in vars as wildcards.

example

exprnew = subs(expr,match,replacement,Wildcards=vars,Condition=condition) performs pattern-based substitution using the constraint or condition specified by condition. The function replaces matching subexpressions only if condition evaluates to true for their matched wildcard values.

example

Examples

collapse all

Replace a with 4 in a symbolic expression.

syms a b
expr = a*b^2
expr = a b2
exprnew = subs(expr,a,4)
exprnew = 4 b2

Replace a*b with 5 in the original expression.

exprnew = subs(expr,a*b,5)
exprnew = 5 b

Substitute the default symbolic scalar variable in a symbolic expression with a. If you do not specify the scalar variable or subexpression to replace, subs uses symvar to find the default variable.

syms x y a
expr = x + y
expr = x+y
var = symvar(expr,1)
var = x

For x + y, the default variable is x. Therefore, subs replaces x with a.

exprnew = subs(expr,a)
exprnew = 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 = cos(a) + sin(b)
expr = cos(a)+sin(b)
exprnew = subs(expr,[a,b],[sym("alpha"),2])
exprnew = sin(2)+cos(α)

Alternatively, for multiple substitutions, use cell arrays.

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

Replace the symbolic scalar variable a in a symbolic expression with the 3-by-3 magic square matrix. Note that the constant 1 expands to a 3-by-3 matrix with all its elements equal to 1.

syms a t
expr = exp(a*t) + 1
expr = ea t+1
expr3by3 = subs(expr,a,magic(3))
expr3by3 = 

(e8 t+1et+1e6 t+1e3 t+1e5 t+1e7 t+1e4 t+1e9 t+1e2 t+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)

You cannot use subs to replace a variable that represents a matrix with a scalar value that reduces the matrix size.

Replace the symbolic scalar variables x and y in an expression with 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 = x*y
expr = x y
exprnew = subs(expr,{x,y},{[0 1; -1 0],[1 -1; -2 1]})
exprnew = 

(0-120)

Because x and y are scalars, these substitutions are element-wise. Compare the result with the element-wise multiplication of the two 2-by-2 matrices.

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

     0    -1
     2     0

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

Eliminate scalar variables from an equation by using the variable value from another equation. In the second equation, isolate the variable on the left side using isolate. Then substitute the variable in the first equation with the right side of the second equation.

First, define 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 a 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
eqns = [x^2 + y^2 == 1, x == y];
sols = solve(eqns,[x y]);
sols.x
ans = 

(-2222)

sols.y
ans = 

(-2222)

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

tf = isAlways(subs(eqns,sols))
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) = 

∂2∂t2 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 ∂2∂t2r(t).

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

∂2∂t2 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 ∂∂t∂r(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

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
M = X*Y
M = X Y

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 substitute and their new values. The new values must have the same size as the matrix variables to be substituted.

Mnew = subs(M,{X,Y},{[0 sqrt(sym(2)); sqrt(sym(2)) 0], [1 -1; -2 1]})
Mnew = 

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

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

Mnew = symmatrix2sym(Mnew)
Mnew = 

(-2 222-2)

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-2 A+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-2 B-2 C

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∂∂Xf(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
eqn = diff(diff(f,X),X.') == 2*A
eqn(X, A) = 

∂∂XT ∂∂X f(X,A)=2 A

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

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

Substitute A with the Hilbert matrix of order 3.

eqn = subs(eqn,A,hilb(3))
eqn(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 eqn from type symfunmatrix to type symfun before using isAlways.

tf = isAlways(symfunmatrix2symfun(eqn))
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 = X Y2-Y X2

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

Since R2026b

Create a symbolic expression.

syms x y z
expr = x + y*z
expr = x+y z

Create a symbolic variable to use as a wildcard for pattern-based substitution.

syms w

Replace all terms in the expression that match the pattern w with 3 using subs. The variables x, y, and z in the expression are replaced, resulting in the output 12.

exprnew = subs(expr,w,3,Wildcards=w)
exprnew = 12

Since R2026b

Create a symbolic expression where you want to apply a specific trigonometric identity.

syms x y
expr = cos(x)^2 + sin(x)^2 + tan(y)^2 + x
expr = cos(x)2+sin(x)2+tan(y)2+x

Create a symbolic variable to use as a wildcard for substitution. To apply the trigonometric identity cos2(θ)+sin2(θ)=1, define the pattern to match and its replacement.

syms w
match = cos(w)^2 + sin(w)^2
match = cos(w)2+sin(w)2
replacement = 1
replacement = 
1

Replace the terms that match the pattern based on the trigonometric identity.

exprnew = subs(expr,match,replacement,Wildcards=w)
exprnew = tan(y)2+x+1

For comparison, use the simplify function to simplify your original expression.

res_simp = simplify(expr)
res_simp = 

--x sin(y)2+x+1sin(y)2-1

While simplify applies a broad set of algebraic and trigonometric rules, it might not produce the intended form. Here, simplify rewrites the tan and cos functions in terms of sin before simplifying the original expression in expr.

Since R2026b

Create a symbolic matrix with integer numbers and symbolic variables. Here, the variable c is assumed to be an integer.

syms a b
syms c integer
M = [a 2 3; 3 4 b; 3 3 c]
M = 

(a2334b33c)

Create a symbolic variable to use as a wildcard for substitution. Define a condition for the pattern to match any symbolic object of type integer.

syms w
condition = @(w) isSymType(w,"integer");

Replace all integers in the matrix M with 5.

Mnew = subs(M,w,5,Wildcards=w,Condition=condition)
Mnew = 

(a5555b55c)

Here, the variable c is not replaced by 5. Although c is assumed to have any integer value, the type of this object is variable, which does not satisfy the condition defined in condition.

symType(c)
ans = 
"variable"

Check the existing assumptions on c.

assumptions
ans = c∈Z

Define a new condition for the wildcard variable using in based on this assumption.

condition = @(w) in(w,"integer");

Replace all integers in the matrix M with 5. Now, the variable c is replaced by 5.

Mnew = subs(M,w,5,Wildcards=w,Condition=condition)
Mnew = 

(a5555b555)

Input Arguments

collapse all

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

Data Types: sym | symmatrix | symfun | symfunmatrix (since R2022a) | struct

Symbolic variables, functions, or subexpressions to substitute, specified as a symbolic scalar variable, matrix variable, function, matrix function, expression, array, or a cell array.

When using the Wildcards option, match defines a symbolic pattern where designated variables act as wildcards. In this case, match must be of type sym or symfun, and the subs function replaces each scalar subexpression in expr that matches the symbolic pattern. (since R2026b)

Data Types: sym | symmatrix | symfun | symfunmatrix | cell

New replacement value, specified as a number, symbolic number, scalar variable, matrix variable, function, matrix function, expression, array, structure, or cell array.

When substituting symbolic matrix variables or matrix functions, replacement must have the same size as match or as the default symbolic variable in expr.

When using the Wildcards option, replacement defines a replacement for terms that match a pattern involving wildcards. In this case, the subs function performs scalar substitution on matching subexpressions. The replacement value must be of type sym, symfun, single, double, int8, int16, int32, int64, uint8, uint16, uint32, or uint64. (since R2026b)

Data Types: sym | symmatrix | symfun | symfunmatrix | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | struct | cell

Since R2026b

Wildcard variables for pattern matching, specified as a symbolic scalar variable or vector of symbolic scalar variables. The symbolic variables used as wildcards must be different from the variables used in the input expression expr.

Data Types: sym

Since R2026b

Condition to constrain pattern matching, specified as a function handle that evaluates to true or false for wildcard values. The subs function performs substitution only when condition evaluates to true for the matched wildcard values. This table shows examples of function handles and their corresponding matching conditions.

Function handleMatching Condition
@(w) trueMatches any wildcard value that is complex
@(w) w > 0Matches any wildcard value that is greater than 0
@(w) (0 < w) & (w < 5)Matches any wildcard value that is greater than 0 and less than 5
@(w) ~has(w,sym("t"))Matches any wildcard value that does not include the variable t
@(w) isSymType(w,"symfun")Matches any wildcard value that is a symbolic function
@(w) isSymType(w,"integer")Matches any wildcard value that is explicitly an integer number
@(w) in(w,"integer")Matches any wildcard value that is an integer number by including assumptions

Because symbolic variables are complex by default, pattern-based substitution finds matches for any wildcard value that is complex and satisfies the condition @(w) true. The subs function performs as many independent replacements as possible, starting with the smallest matching subexpressions.

When performing pattern-based substitution, subs takes into account all assumptions made about the variables in expr by using isAlways internally. Therefore, when specifying a condition to constrain pattern matching, you do not need to call isAlways explicitly. For example, you can define the condition to constrain pattern matching as @(w) w > 0 instead of @(w) isAlways(w > 0).

Data Types: function_handle

Output Arguments

collapse all

Symbolic expression after substitution, returned as a symbolic number, scalar variable, matrix variable, function, matrix function, expression, equation, array, or structure.

Data Types: sym | symmatrix | symfun | symfunmatrix

Tips

  • subs(expr,__) does not modify expr. To modify expr, use expr = subs(expr,__).

  • If expr is a univariate polynomial and replacement is a numeric matrix, use polyvalm(sym2poly(expr),replacement) to evaluate expr as a matrix. The function replaces all constant terms with the constant multiplied by an identity matrix.

Version History

Introduced before R2006a

expand all