# Substituting a variable by a value: subst

### 5.12.18 Substituting a variable by a value: subst

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

The first way:

- subst takes 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.

#### Examples.

- Input (if the variable a is purged, otherwise first enter purge(a)):
  subst(a^2+1,a=2)
  Output:

  5

- Input (if the variables a and b are purged, otherwise first enter purge(a,b)):
  subst(a^2+b,[a=2,b=1])
  Output:

  5

The second way:

- subst takes 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.

- Input (if the variable a is purged, otherwise first enter purge(a)):
  subst(a^2+1,a,2)
  Output:

  5

- Input (if the variables a and b are purged, otherwise first enter purge(a,b)):
  subst(a^2+b,[a,b],[2,1])
  Output:

  5

subst may also be used to make a change of variable in an integral. In this case the integrate command (see Section 5.20.1) should be quoted (see Section 5.12.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.

- Input:
  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))
  Output:

  ∫

  π 2
  4

  0

  1
  2

  sint·

  √

  t

  √

  t

  −1 dt

  Input:
  subst(’integrate(sin(x^2)*x,x)’,x=sqrt(t))
  or:
  subst(Int(sin(x^2)*x,x),x=sqrt(t))
  Output:

  ∫

  1
  2

  sint·

  √

  t

  √

  t

  −1 dt