Accelerating the pace of engineering and science

• Trials

# subs

Symbolic substitution

## Description

example

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

example

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.

example

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.

## Examples

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

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

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

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

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

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

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

## Input Arguments

expand all

### s — Inputsymbolic variable | symbolic expression | symbolic equation | symbolic function | symbolic array | symbolic vector | symbolic matrix

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

### old — Existing element that needs to be replacedsymbolic variable | symbolic expression | string representing variable or expression | symbolic array | symbolic vector | symbolic matrix | array of strings | vector of strings | matrix of strings

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

### new — New elementnumber | symbolic variable | symbolic expression | string representing variable or expression | symbolic array | symbolic vector | symbolic matrix | array of strings | vector of strings | matrix of strings | structure array

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

expand all

### Tips

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