Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cos(pi/2) error in the symbolic toolbox?

16 views
Skip to first unread message

Daniel Karssen

unread,
Sep 2, 2010, 9:57:04 AM9/2/10
to
Today I upgraded to the 2009b version of Matlab and now I got a strange error while using the symbolic tool. For the input:
syms a
cos(pi/2)*a
matlab returns
ans =
(4967757600021511*a)/81129638414606681695789005144064
and not zero.

I believe I did not have this error with the 2007b version. However my license does not allow me anymore to use that version, so I can not test it. Is there a way to fix this error or is there a workaround?

Daniel Karssen

unread,
Sep 2, 2010, 10:07:20 AM9/2/10
to
"Daniel Karssen" <NoSpamj.g...@NoSpamtudelft.nl> wrote in message <i5oafg$ju6$1...@fred.mathworks.com>...

Found the answer myself.
pi = sym('pi')
Does the trick.

Steven_Lord

unread,
Sep 2, 2010, 1:56:01 PM9/2/10
to

"Daniel Karssen" <NoSpamj.g...@NoSpamtudelft.nl> wrote in message

news:i5ob2o$9p$1...@fred.mathworks.com...

When you compute cos(pi/2) you're computing that in double precision (and
this does not give an exact 0, for reasons that have been discussed in the
past on this newsgroup.) At the next step, when you multiply that very
small double precision result by a, the small double precision result is
converted into a symbolic number. That number is also not exactly 0, but
is:

(4967757600021511)/81129638414606681695789005144064

Rather than redefining the variable pi, I would probably do something like:

piOver2 = sym('pi')/2;
cos(piOver2)*a

or simply move the SYM call into the expression directly, if you only need
to use pi/2 once.

cos(sym('pi')/2)*a

Dividing a symbolic pi by 2 converts the 2 into a symbolic value and then
performs the division symbolically, so you avoid double precision roundoff
completely.

This is the same general reason that:

x1 = sym(10^1000)

gives a much different result from:

x2 = sym(10)^1000

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

0 new messages