MATLAB: How to substitute a sub-expression in a symbolic expression using Symbolic Math Toolbox 5.4 (R2010a)

enginemupadsubexpressionSymbolic Math Toolbox

I have a symbolic expression. I want to replace a sub-expression in this expression with another variable or expression. I am using SUBS but it does not make the substitution as expected. For example:
syms a b C L t
u=a*cos(1/(C*L)^2*t+3)+b*sin(1/(C*L)^2*5*t+3);
w0=1/(C*L)^2;
p = subs(u, w0, 'w0')
p =
a*cos(t/(C^2*L^2) + 3) + b*sin((5*t)/(C^2*L^2) + 3)

Best Answer

This enhancement has been incorporated in Release 2013a (R2013a). For previous product releases, read below for any possible workarounds:
In Symbolic Math Toolbox 5.4 (R2010a), SUBS can only be used to substitute a variable in an expression with another variable or expression. SUBS may not be used to substitute a sub-expression in an expression.
As a workaround, you can use SUBSEX, available in the MuPAD language, to substitute a sub-expression in an expression. You may call this function with the help of FEVAL as follows:
q = feval(symengine,'subsex',u,[char(w0) '=w0'])
q =
a*cos(t*w0 + 3) + b*sin(5*t*w0 + 3)
You can also create an anonymous function to mimic SUBS:
subsx = @(u,w0,w0str) feval(symengine,'subsex',u,[char(w0) '=' w0str]);
%then issuing the command:
p3 = subsx(u, w0, 'w0')
You may read more about SUBSEX in the MuPAD help documentation at:
<http://www.mathworks.com/help/releases/R2013a/symbolic/mupad_ref/subsex.html>