4
votes

Hello i have one question. When I calculate a division in matlab: x/(pi.^2)

syms x
x/(pi.^2)
ans =
(281474976710656*v)/2778046668940015

the correct answer is x/9.8696, so why is matlab giving me this result?

Is it a bug?

2
that's the rational approximation MATLAB is giving you. No it is not a bug, use the numerical approximation symbolic toolbox function ( it is N[] in mathematica, forget what matlab's is) - im so confused
Indeed. Note that 281474976710656/2778046668940015 ~= 1/9.8696. The symbolic math toolbox tries to preserve as much precision as possible by deferring floating point computations. Since pi is inherently irrational, it uses this approximation. - mbauman
also note that if you're doing symbolic calculations with pi, etc, use sym('pi') to get a symbolic representation of pi rather than an approximation - im so confused

2 Answers

9
votes

You have to use the vpa() command "Variable-precision arithmetic". Check this code:

syms x real;       % define x as a real symbolic variable (not a complex variable)
vpa( x/(pi.^2), 5) % second argument define number of significant digits

For trigonometric expressions involving pi, it is sometimes good to define sym('pi'):

syms x real;
pi_s = sym('pi');
expr = x/pi_s^2

I try to always use the 'real' tag when using the symbolic toolbox. If you do not use it you are going to see a lot of complex conjugates and other things that are not important for your problem, because x is probably real variable.

Hope this helps,

2
votes

No it is not a bug:

2778046668940015/281474976710656 = 9.8696