How can I substitute a sub-expression in a symbolic expression using Symbolic Math Toolbox 5.4 (R2010a)?

9 views (last 30 days)
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)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
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>

More Answers (0)

Products


Release

R2010a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!