This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Symbolic substitution





subs(s,old,new) returns a copy of s replacing all occurrences of old with new, and then evaluating s.


subs(s,new) returns a copy of s replacing all occurrences of the default variable in s with new, and then evaluating s. The default variable is defined by symvar.


subs(s) returns a copy of s replacing symbolic variables in s with their values obtained from the calling function and the MATLAB® workspace, and then evaluating s. Variables with no assigned values remain as variables.


Single Substitution

Replace a with 4 in this expression.

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

Replace a*b with 5 in this expression.

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

Value That Gets Substituted by Default

Substitute the default value in this expression with a. If you do not specify which variable or expression that you want to replace, subs uses symvar to find the default variable. For x + y, the default variable is x.

syms x y a
symvar(x + y, 1)
ans =

Therefore, subs replaces x with a.

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

Single Input

Solve this ordinary differential equation.

syms a y(t)
y = dsolve(diff(y) == -a*y)
y =

Now, specify the values of the symbolic parameters a and C2.

a = 980;
C2 = 3;

Although the values a and C2 are now in the MATLAB workspace, y is not evaluated with the account of these values.

y =

To evaluate y taking into account the new values of a and C2, use subs.

ans =

Multiple Substitutions

Make multiple substitutions by specifying the old and new values as vectors.

syms a b
subs(cos(a) + sin(b), [a, b], [sym('alpha'), 2])
ans =
sin(2) + cos(alpha)

You also can use cell arrays for that purpose.

subs(cos(a) + sin(b), {a, b}, {sym('alpha'), 2})
ans =
sin(2) + cos(alpha)

Scalar and Matrix Expansion

Replace 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
subs(exp(a*t) + 1, a, -magic(3))
ans =
[ exp(-8*t) + 1,   exp(-t) + 1, exp(-6*t) + 1]
[ exp(-3*t) + 1, exp(-5*t) + 1, exp(-7*t) + 1]
[ exp(-4*t) + 1, exp(-9*t) + 1, exp(-2*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])
B = sym('B', [2,2])
A =
[ A1_1, A1_2]
[ A2_1, A2_2]
B =
[ B1_1, B1_2]
[ B2_1, B2_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.

A44 = subs(A, A(1,1), B)
A44 =
[ B1_1, B1_2, A1_2, A1_2]
[ B2_1, B2_2, A1_2, A1_2]
[ A2_1, A2_1, A2_2, A2_2]
[ A2_1, A2_1, A2_2, A2_2]

subs does not let you replace a nonscalar with a scalar.

Multiple Scalar Expansion

Replace 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 y
subs(x*y, {x, y}, {[0 1; -1 0], [1 -1; -2 1]})
ans =
[ 0, -1]
[ 2,  0]

Note that these substitutions are elementwise.

[0 1; -1 0].*[1 -1; -2 1]
ans =
     0    -1
     2     0

Substitutions in Equations

Replace sin(x + 1) with a in this equation.

syms x a
subs(sin(x + 1) + 1 == x, sin(x + 1), a)
ans =
a + 1 == x

Substitutions in Functions

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 does not replace input arguments of the function.

ans =
a + y
ans =
[ x, y]

You can replace the arguments of a symbolic function explicitly.

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

Original Expression

Assign the expression x + y to s.

syms x y
s = x + y;

Replace y in this expression with the value 1. Here, s itself does not change.

subs(s, y, 1);
s =
x + y

To replace the value of s with the new expression, assign the result returned by subs to s.

s = subs(s, y, 1);
s =
x + 1

Structure Array

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);
ans =
ans =

To verify the correctness of the returned solutions, substitute the solutions into the original system.

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

Input Arguments

collapse all

Input specified as a symbolic variable, expression, equation, function, array, vector, or matrix.

Existing element that needs to be replaced specified as a symbolic variable, expression, character vector, array, vector, or matrix.

New element specified as a number, variable, expression, character vectors, array, vector, matrix, or structure array.


  • subs(s,old,new) does not modify s. To modify s, use s = subs(s,old,new).

  • If old and new are both vectors or cell arrays of the same size, subs replaces each element of old by the corresponding element of new.

  • If old is a scalar, and new is a vector or matrix, then subs(s,old,new) replaces all instances of old in s with new, performing all operations elementwise. All constant terms in s are replaced with the constant times a vector or matrix of all 1s.

  • If s is a univariate polynomial and new is a numeric matrix, use polyvalm(sym2poly(s), new) to evaluate s in the matrix sense. All constant terms are replaced with the constant times an identity matrix.

Introduced before R2006a

Was this topic helpful?