Substituting a variable by a value

### 9.1.13 Substituting a variable by a value

The subst command replaces specified variables in an expression by specified values. Unlike the | operator, the subst command evaluates the expression before replacing the variables. Since subst does not quote its argument, in a normal evaluation process the substitution variable should be purged (see Section 3.3.8), otherwise it will be replaced by its assigned value before substitution is done.

The subst command can specify the values of variables in two different ways.

- subst can take two arguments:
  - expr, an expression.
  - eqs, an equation of the form x=a, or a list of such equalities.
- subst(expr,eqs) returns the expression with the variables replaced by their values.
- Alternatively, subst can take three arguments:
  - expr, an expression.
  - vars, a variable or a list of variables.
  - vals, a value or a list of values for substitution.
- subst(expr,vars,vals) returns the expression with the variables replaced by their values.

#### Examples

Assuming that a and b are not assigned:

subst(a^2+1,a=2)

5 |  |  |  |  |  |  |  |  |  |

subst(a^2+b,[a=2,b=1])

5 |  |  |  |  |  |  |  |  |  |

subst(a^2+1,a,2)

5 |  |  |  |  |  |  |  |  |  |

subst(a^2+b,[a,b],[2,1])

5 |  |  |  |  |  |  |  |  |  |

##### Changing integration variables.

subst may also be used to make a change of variable in an integral. In this case the integrate command (see Section 13.3.1) should be quoted (see Section 9.1.4, otherwise, the integral would be computed before substitution) or the inert form Int should be used. In both cases, the name of the integration variable must be given as an argument of Int or integrate even you are integrating with respect to x.

#### Examples

subst('integrate(sin(x^2)*x,x,0,pi/2)',x=sqrt(t))

or:

subst(Int(sin(x^2)*x,x,0,pi/2),x=sqrt(t))

| ∫ |
π 2
4
0
1
2
| sint | √ |
t
|  | √ |
t
| −1 dt

subst('integrate(sin(x^2)*x,x)',x=sqrt(t))

or:

subst(Int(sin(x^2)*x,x),x=sqrt(t))

| ∫ |
1
2
| sint | √ |
t
|  | √ |
t
| −1 dt