## Documentation Center |

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 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 = C2*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 = C2*exp(-a*t)

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

subs(y)

ans = 3*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(x, a) = subs(f, x, a); f

f(x, a) = 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.

logical(subs(eqs, S))

ans = 1 1 1 1

`double` | `eval` | `evalAt` | `simplify` | `subexpr` | `subs` | `subset` | `subsex` | `subsop` | `vpa`

Was this topic helpful?