next up previous
Next: Plotting with Maple Up: Preliminaries about Maple Previous: The int (and Int)

Subsections


The subs command

You have learned in the previous sections that if you assign a particular value to a variable, Maple will remember it for the rest of the session until you unassign it. At times, you might like to plug in some values into an expression without altering the variable (or variables) involved. Maple has the subs command which reports the result of making such a substitution:
> 
  y:=x+3;


\begin{maplelatex}
\begin{displaymath}y := x + 3 \end{displaymath}\end{maplelatex}

> 
  subs(x=2,y);


\begin{maplelatex}
\begin{displaymath}5 \end{displaymath}\end{maplelatex}
Notice that Maple reports the value obtained when plugging in x=2 into y without giving that value to x or altering y in any way:

> 
  x;


\begin{maplelatex}
\begin{displaymath}x \end{displaymath}\end{maplelatex}

> 
  y;


\begin{maplelatex}
\begin{displaymath}x + 3 \end{displaymath}\end{maplelatex}

You can make more than one substitution:

> 
  z:=x+sin(2*h):
  subs(x=3*u+1,h=4*Pi*q,z);


\begin{maplelatex}
\begin{displaymath}3 u + 1 + \sin(8\pi q) \end{displaymath}\end{maplelatex}

In this example, the first assignment is ended with a colon : to suppress the display. You can do this whenever you want to make an assignment which you do not want to see displayed. Then, plugging x = 3u + 1 and h = 4$ \pi$q into the expression for z, we obtain 3u + 1 + sin(8$ \pi$q). Notice that the values of x and h that we plug in do not need to be enclosed in braces when issuing the command.

You should be aware of a few things when using the subs command:

Why is subs useful?

Often it is better to use subs rather than to make assignments to the variables in an expression. For example, if
> 
  y:=3*x^2+5*x+2; 


\begin{maplelatex}
\begin{displaymath}
y := 3 x^2 + 5 x + 2
\end{displaymath}\end{maplelatex}
and you want to compute the difference quotient (y(x + h) - y(x))/h, then you should execute

> 
  dq:=(subs(x=x+h,y)-y)/h; 


\begin{maplelatex}
\begin{displaymath}
dq:=\frac{3 (x+h)^2 + 5 h - 3x^2}{h}
\end{displaymath}\end{maplelatex}
because if we set

> 
  x:=x+h; 


\begin{maplettyout}
Warning: Recursive definition of name
\end{maplettyout}

\begin{maplelatex}
\begin{displaymath}
x := x + h
\end{displaymath}\end{maplelatex}
any call to y will result in

> 
  y;


\begin{maplettyout}
Error, too many levels of recursion
\end{maplettyout}

In earlier versions of Maple, this action would have crashed your session.

On the other hand,

> 
  x:=4;


\begin{maplelatex}
\begin{displaymath}x := 4 \end{displaymath}\end{maplelatex}

> 
  y;


\begin{maplelatex}
\begin{displaymath}70 \end{displaymath}\end{maplelatex}
is alright, but now y is a number and we cannot plug any other values of x into it. Using subs is a better way to do the plugging in, because we need not unassign y.


next up previous
Next: Plotting with Maple Up: Preliminaries about Maple Previous: The int (and Int)

Translated from LaTeX by Scott Sutherland
2002-08-29