MaplePortal/MakingSubstitutions - Help

Making Substitutions

 Introduction In this document, you will learn different methods to substitute values for variables in mathematical expressions. Both simple substitutions, such as "evaluate $\mathrm{sin}\left(x\right)$ when $x=\mathrm{\pi }",\mathbf{}$and substitutions of algebraic subexpressions, such as "evaluate $a\cdot b\cdot \mathrm{sin}\left(a\cdot b\cdot c\right)$ when $a\cdot b=n-1"$ are explained.

Simple Substitutions

Substituting a value for a variable, $x$, in an expression $F$ containing $x$ is the same as evaluating $F\mathbf{}$at the point $x$. This is the terminology Maple uses to describe the problem. Knowing this terminology will make it much easier to remember the appropriate menus and commands (or at least, recognize them when you see them).

The following explains both interactive and command methods for evaluating an expression at a point.

Interactive Methods

 Palettes The Expression palette provides an "evaluate at a point" template, $\genfrac{}{}{0}{}{{f}\left({x}\right)}{\phantom{x=a}}|\genfrac{}{}{0}{}{\phantom{\mathrm{f\left(x\right)}}}{{x}={a}}$. Fill in the values of the template to evaluate your expression at a point.   For example, $\genfrac{}{}{0}{}{\mathrm{sin}\left(x\right)}{\phantom{x=a}}|\genfrac{}{}{0}{}{\phantom{\mathrm{f\left(x\right)}}}{x=\mathrm{\pi }}$ = ${0}$ Tip: Press Tab to move through the placeholders in the template. Press Shift + Tab to go back a placeholder.



 Context Menu Right-click on the expression you wish to evaluate, and select Evaluate at a Point. A dialog is displayed which allows you to set variables for some or all of the variables in the expression.   For example, setting $x=$$5$ and $y=2\mathbf{}$gives: ${x}^{2}+{y}^{2}$$\stackrel{\text{evaluate at point}}{\to }$${29}$   Setting $x=\mathbf{}a+b$ and leaving $y$ without a value gives: ${x}^{2}+{y}^{2}$$\stackrel{\text{evaluate at point}}{\to }$${\left({a}{+}{b}\right)}^{{2}}{+}{{y}}^{{2}}$

Commands

The recommended command for substituting a value for a variable in an expression is the eval command.

$\mathrm{eval}\left(\mathrm{sin}\left(x\right),\mathbf{}x=\mathrm{\pi }\right)$

 ${0}$ (1)

$\mathrm{eval}\left({x}^{2}+{y}^{2},\mathbf{}x=a+b\right)$

 ${\left({a}{+}{b}\right)}^{{2}}{+}{{y}}^{{2}}$ (2)

To substitute multiple values, put them in a list.

$\mathrm{poly}:=x\cdot y+3\mathbf{}z+2$

 ${\mathrm{poly}}{:=}{x}{\mathbf{}}{y}{+}{3}{\mathbf{}}{z}{+}{2}$ (3)

$\mathrm{eval}\left(\mathrm{poly},\mathbf{}\left[x=2,\mathbf{}y=3,z=t\right]\right)$

 ${8}{+}{3}{\mathbf{}}{t}$ (4)

Comparing eval and subs

Another command for making simple substitutions is the subs command. It is important to note that after making a substitution, the subs command does not evaluate the results. If the developer neglects to do the evaluation afterwards, this can result in unexpected behavior (and long debugging sessions!).

Using subs:

$\mathrm{subs}\left(y=0,\mathbf{}\mathrm{sin}\left(y\right)\right)$

 ${\mathrm{sin}}{}\left({0}\right)$ (5)

and the same question using eval:

$\mathrm{eval}\left(\mathrm{sin}\left(y\right),y=0\right)$

 ${0}$ (6)

Notice that the calling sequences for eval and subs are different: eval expects its first argument to be the expression and its second argument to be the substitution, while subs command takes the substitution expression as its first argument and the expression as its second argument.

The eval command knows how to correctly evaluate piecewise expressions, while subs may make substitutions that do not make sense mathematically. In the following example subs gives a division by zero error, but eval correctly evaluates the piecewise definition at $x=0$.

$p≔\left\{\begin{array}{cc}\frac{1}{x}& x<0\\ 0& x\ge 0\end{array}\right\$

 ${{}\begin{array}{cc}\frac{{1}}{{x}}& {x}{<}{0}\\ {0}& {0}{\le }{x}\end{array}$ (7)

$\mathrm{subs}\left(x=0,p\right)$

$\mathrm{eval}\left(p,x=0\right)$

 ${0}$ (8)

For these reasons, the eval command is often preferable.

One feature of subs is that it allows a chain of substitutions. If you supply subs with a number of equations, the substitutions are applied sequentially beginning with the left-most equation:

$\mathrm{subs}\left(a=b,b=c,\mathrm{sin}\left(a\right)\right)$

 ${\mathrm{sin}}{}\left({c}\right)$ (9)

eval does not provide that ability with a single call, so to obtain a similar chain of evaluations from eval, you must use nested calls:

$\mathrm{eval}\left(\mathrm{eval}\left(\mathrm{sin}\left(a\right),a=b\right),b=c\right)$

 ${\mathrm{sin}}{}\left({c}\right)$ (10)

Substituting for Combinations of Variables: algsubs

In the examples earlier, the substitutions for variables are simple, stand-alone quantities. You can also substitute for an entire subexpression involving multiple variables, such as replacing $a\cdot b\mathbf{}$with $c.$

The first attempt at making this substitution is to try the eval command. The eval command can substitute for any operand of the expression, such as

 ${u}{+}{3}{\mathbf{}}{z}{+}{2}$ (11)

but it will only substitute for subexpressions that correspond to the operands of a Maple object. That is, it will only replace exact occurrences of an operand. This is called syntactic substitution. (The subs command also does syntactic substitution.)

 ${u}{+}{3}{\mathbf{}}{x}{\mathbf{}}{y}{\mathbf{}}{z}{+}{2}$ (12)

The subexpression was not replaced with $u\cdot z$, because $x\cdot y$ is not an operand of $3\cdot x\cdot y\cdot z$. See op for more information on operands.

For more powerful mathematical substitutions involving sums and products of variables we can use the algsubs command, which stands for algebraic substitution.

 ${u}{+}{3}{\mathbf{}}{z}{\mathbf{}}{u}{+}{2}$ (13)

$\mathrm{algsubs}\left({a}^{2}=0,{ⅇ}^{2-a+\frac{{a}^{2}}{2}-\frac{{a}^{3}}{6}}\right)$

 ${{ⅇ}}^{{2}{-}{a}}$ (14)

The value being substituted into the original expression is not limited to a simple symbol or value.

$\mathrm{algsubs}\left(a\cdot b=n-1,a\cdot b\cdot \mathrm{sin}\left(a\cdot b\cdot c\right)\right)$

 ${\mathrm{sin}}\left({c}{\mathbf{}}\left({n}{-}{1}\right)\right){\mathbf{}}\left({n}{-}{1}\right)$ (15)

$\mathrm{algsubs}\left({s}^{2}=1-{c}^{2},{s}^{3}\right)$

 $\left({1}{-}{{c}}^{{2}}\right){\mathbf{}}{s}$ (16)

The algsubs command accepts some options for specifying the substitution mode and the ordering of variables. See algsubs for more details.

Note: Another approach is to use the simplify command with side relations. This command reduces the expression to normal form with respect to the equations you provide, and also allows you control over the final result by optionally specifying a monomial order for the variables. It may be a more appropriate choice if you need more control over the form of the final result. See simplify/siderels.

In summary,

 • eval does strict syntactic substitution and then evaluates the result. Use for simple applications of replacing a symbol by a value.
 • subs does strict syntactic substitution but does not evaluate the result. Use to substitute subexpressions into an expression.
 • algsubs does algebraic substitution. Use when a more powerful substitution is needed.