f



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

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?
 
0
Daniel
9/2/2010 1:57:04 PM
comp.soft-sys.matlab 210033 articles. 11 followers. lunamoonmoon (258) is leader. Post Follow

2 Replies
1439 Views

Similar Articles

[PageSpeed] 58

"Daniel Karssen" <NoSpamj.g.d.karssen@NoSpamtudelft.nl> wrote in message <i5oafg$ju6$1@fred.mathworks.com>...
> 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?
>  

Found the answer myself.
pi = sym('pi')
Does the trick.
0
Daniel
9/2/2010 2:07:20 PM

"Daniel Karssen" <NoSpamj.g.d.karssen@NoSpamtudelft.nl> wrote in message 
news:i5ob2o$9p$1@fred.mathworks.com...
> "Daniel Karssen" <NoSpamj.g.d.karssen@NoSpamtudelft.nl> wrote in message 
> <i5oafg$ju6$1@fred.mathworks.com>...
>> 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?
>>
>
> Found the answer myself.
> pi = sym('pi')
> Does the trick.

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
slord@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
Steven_Lord
9/2/2010 5:56:01 PM
Reply: