Why does the ABS function on symbolic variables not give the real and imaginary parts separately in the Symbolic Math Toolbox 5.5 (R2010b)?

4 views (last 30 days)
Following is the reproduction code:
syms x y real
syms z complex
z=x+i*y;
abs(z);
This returns abs(x + y*i), instead of (x^2 + y^2)^(1/2).

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Apr 2012
The ability to do so has been incorporated in Release 2012a (R2012a) using the REWRITE function which was introduced in this release. The following code gives the desired result:
syms x y real
z = x + i*y;
a = abs(z);
rewrite(a,'sqrt')
For previous product releases, read below for possible workarounds:
The ability to separate real and imaginary parts separately using ABS is not available in the Symbolic Math Toolbox.
As a workaround, you could use the MuPAD function RECTFORM as follows:
syms x y real;
z = x + i*y;
feval(symengine, 'rectform', abs(z))
Note that MuPAD assumes variables to be complex if not declared to
be real. Hence the need to use the REAL keyword.
Another possible workaround is as follows:
SIMPLIFY can be used to get the desired result. The example code is as follows:
syms x y real
z = x + i*y;
abs(z)
simplify(abs(z))

More Answers (0)

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!