Main Content

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

Substitute the default variable in this expression with `a`

.
If you do not specify the 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 symvar(x + y, 1)

ans = x

Therefore, subs replaces `x`

with `a`

.

subs(x + y, a)

ans = a + y

When you assign a new value to a symbolic 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 = x^2

Evaluate `y`

with the new value of `x`

by using
`subs`

.

subs(y)

ans = 4

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)

Alternatively, for multiple substitutions, use cell arrays.

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.

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: [1×1 sym] f2: [1×1 sym] f3: [1×1 sym]

Replace the symbolic variables `x`

, `y`

, and
`z`

with numeric values.

Sval = subs(S,[x y z],[0.5 1 1.5])

S = struct with fields: f1: [1×1 sym] f2: [1×1 sym] f3: [1×1 sym]

Show the value of each field.

Sval.f1

ans = 1/2

Sval.f2

ans = 5/2

Sval.f3

ans = 1

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 element-wise.

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

ans = 0 -1 2 0

Eliminate 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 right side
of `eqn2`

with the left side of `eqn2`

in
`eqn1`

.

eqn1 = subs(eqn1,lhs(eqn2),rhs(eqn2))

eqn1 = sin(x) + cos(x)/x == cos(x)^2/x^2 + x^2

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]

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

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

Verify the solutions by substituting 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`

with 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 element-wise. All constant terms in`s`

are replaced with the constant multiplied by 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`

as a matrix. All constant terms are replaced with the constant multiplied by an identity matrix.