At least in this case you should be able to do this just using subs
, which works for expressions as well as variables:
syms x f1;
f = 1/(1+exp(-x))
f2 = exp(-x)/(1+exp(-x))^2
f2 = subs(f2,f,f1)
which in R2014b and R2015a returns f1^2*exp(-x)
. For more complex expression this may not be completely robust or may need to be applied more than once or in conjunction with simplify
.
If you just need to compare expressions, you can use sym/isequaln
or isAlways
:
syms x;
f = 1/(1+exp(-x));
f2 = exp(-x)/(1+exp(-x))^2;
isequaln(f*(1-f),f2)
isAlways(f*(1-f)==f2)
However, both of these return Boolean false
(0). for some reason. This may be a bug or maybe it's because the to two forms have different singularities when evaluated at -Inf
. The following
syms x;
f = 1/(1+exp(-x));
f2 = exp(-x)/(1+exp(-x))^2;
subs(f2,x,-Inf)
subs(f*(1-f),x,-Inf)
yields NaN
and 0
, respectively. Taking the limit
at -Inf
does yield the correct values, however. The documentation does not indicate how sym/isequaln
and/or isAlways
handle this situation. This might be a good case to file a service request with The MathWorks.
eliminate([f = 1/(1+exp(-x)), f2 = exp(-x)/(1+exp(-x))^2], x)
and the output isf2 + f^2 - f = 0
which is the sort of result you wanted. – user3717023f3=f2+f^2-f; simpify(f3)
. I am not sure how the proposal from @FamousBlueRaincoat can be implemented in matlab, but if that works it seems as a good solution as well. – patrik