Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pi, Matlab Symbolic math toolbox have a bug?

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?

like image 518
Alberto Avatar asked Dec 21 '22 14:12

Alberto


2 Answers

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,

like image 122
jespestana Avatar answered Jan 06 '23 21:01

jespestana


No it is not a bug:

2778046668940015/281474976710656 = 9.8696
like image 38
Bitwise Avatar answered Jan 06 '23 20:01

Bitwise