Matlab Symbolic tool has a very annoying feature to me. It automatically convert to rational/decimal values.
Example 1
z = sym('z', 'real');
f = 3^(1/2)*z;
we get expected Matlab return f = 3^(1/2)*z
Example 2
z = sym('z', 'real');
f = 3^(1/3)*z;
Then I got crappy result f = (3247657313705851*z)/2251799813685248
, not desired f=3^(1/3)*z
The reason for this can be found here. By default sym()
use rational approximation to the numbers.
My question is, how to disable this feature? For example, how to get return f=3^(1/3)*z
from the Example 2?
The documentation you link to tells you how to work around this by forcing numeric values to be computed symbolically. In this case, it's the 1/3
piece that can't be represented with sufficient precision in double
, so you can do:
f = 3 ^ (sym(1)/sym(3)) * z;