From: Nicola Branzoli on
Hi Matlabbers,
note that this code

syms b c d
a=b+c
e=subs(a,d,c)

produces:

e=b+d

you may ask why I want to substitute d since there is no d
in a.
Actually I needed to and I found this error. There might be
cases when you do not know if a symbolic variable contains
an expression and so try substitute this expression "by
default". Clearly it's easy to solve: add an "if" that check
the presence of the variable (this will slow your code
anyway...), otherwise you might get troubles.
Please check this is true.
Nicola
From: helper on
"Nicola Branzoli" <statuario(a)hotmail.com> wrote in message
<fvsbbr$e6t$1(a)fred.mathworks.com>...
> Hi Matlabbers,
> note that this code
>
> syms b c d
> a=b+c
> e=subs(a,d,c)
>
> produces:
>
> e=b+d
>
> you may ask why I want to substitute d since there is no d
> in a.
> Actually I needed to and I found this error. There might
be
> cases when you do not know if a symbolic variable contains
> an expression and so try substitute this expression "by
> default". Clearly it's easy to solve: add an "if" that
check
> the presence of the variable (this will slow your code
> anyway...), otherwise you might get troubles.
> Please check this is true.
> Nicola


From the documentation on SUBS:

If subs(s,old,new) does not change s, subs(s,new,old) is
tried. This provides backwards compatibility with previous
versions and eliminates
the need to remember the order of the arguments. subs
(s,old,new,0) does
not switch the arguments if s does not change.

In other words, use:

e=subs(a,d,c,0)