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.

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)`

`subs(s,new)`

`subs(s)`

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 = 5*b

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 = x

Therefore, subs replaces `x`

with `a`

.

subs(x + y, a)

ans = a + y

Solve this ordinary differential equation.

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

y = C3*exp(-a*t)

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

y = C3*exp(-a*t)

To evaluate `y`

taking into account the new
values of `a`

and `C2`

, use `subs`

.

subs(y)

ans = C3*exp(-980*t)

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)

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.

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

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

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.

formula(f) argnames(f)

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

f(a, y) = a + y

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

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

s = x + 1

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 S.y

ans = -2^(1/2)/2 2^(1/2)/2 ans = -2^(1/2)/2 2^(1/2)/2

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

`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.

Was this topic helpful?