subs
command which reports the result of making such a
substitution:
> y:=x+3;
> subs(x=2,y);
x=2
into
y
without giving that value to x
or altering y
in any
way:
> x;
> y;
You can make more than one substitution:
>
z:=x+sin(2*h):
subs(x=3*u+1,h=4*Pi*q,z);
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 = 4q into the expression for z, we obtain
3u + 1 + sin(8
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:
> z;
> subs(x=2*x,z);
On the other hand, long chains of self-referential substitutions may produce
results which are hard to predict:
>
subs(h=x,x=z,z);
Simultaneous substitutions are made enclosing the values you plug in in
braces. This is in contrast with the left-to-right substitution above:
>
subs(h=x,x=z,z);
subs
will not be able to find the
expression you are substituting for if it is complicated. In those cases,
try to reword your substitution request so that Maple
understands it, or
break the task into several manageable ones. In other cases, you might
consider using algsubs
, which allows you to substitute one algebraic
expression for another.
subs
does what is called ``syntactic substitution''; it
substitutes one set of symbols for another. If you are using it to evaluate
an expression with certain values ``plugged in'', using eval
might be
more appropriate. Here is an example that should illustrate the
distinction:
>
integral := int(f(t,a), t=a..x);
>
eval(integral,t=0,a=1);
>
subs(t=0,a=1,integral);
The seemingly nonsensical answer using subs
above is because Maple
interpreted the expression as meaning int(f(0,1),0=1..x)
-- what
you get by replacing t
by 0
and a
by 1
without
trying to interpret the expression.
subs
rather than to make assignments
to the variables in an expression. For example, if
> y:=3*x^2+5*x+2;
> dq:=(subs(x=x+h,y)-y)/h;
> x:=x+h;
y
will result in
> y;
In earlier versions of Maple, this action would have crashed your session.
On the other hand,
>
x:=4;
>
y;
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
.