# Funny problem. What is cos(pi/2) in matlab?

• Jan 27th 2010, 02:37 PM
zarka
Funny problem. What is cos(pi/2) in matlab?
Hey everyone

While I was programming on matlab I encountered something funny. I don't know if its a bug or what.

Now we all know that cos(pi/2) should be 0. Right??

Now do this:

Code:

a = 55; theta = pi/2; P = [a1*cos(theta); a1*sin(theta)]
run this, you should get that outcome:

Code:

P =     0.0000   55.0000

Now here is the messed up part: Input this line now:

Code:

P(1)                  %to get the value of first row

Code:

ans =   3.3678e-015

See it here:
Code:

>> a = 55 a =     55 >> theta = pi/2 theta =     1.5708 >> P = [a1*cos(theta); a1*sin(theta)] P =     0.0000   55.0000 >> P(1) ans =   3.3678e-015

Why the heck is this happening?? what am I doing wrong??

Please try it on your own matlab. I am using Matlab 7.9 (R2009b)

This should be the simplest code in matlab.

help!
• Jan 27th 2010, 03:06 PM
TKHunny
0.0000000000000033678 is pretty close to zero. Where's the problem?

This is a common programming error. Zero isn't really zero in the real world. You must get used to it.

Find your settings. Somewhere, there should be defined something called a "Comparison Tolerance" or something like that. Find that value. It answers your question. Anything less than that comparison tolerance, in absolute value, IS zero. TOL = What? Print out the value of the system variable TOL and see what you get. If you can't find it anywhere else, look up the definition of the fzero() or quad(). There should be a nice discussion in there. Somethig like $\displaystyle \sqrt[4]{\epsilon_{mach}}$ may be cited.

Machine error is an important consideration. You must not overlook it.
• Jan 27th 2010, 03:32 PM
zarka
Thanks TKHunny. I know its too close to zero but I kinda bugged me a bit (Smirk)

I guess I'll just try to stick to this poor number :)

Regards,
• Jan 27th 2010, 06:42 PM
TKHunny
You do have to be careful with it.

This happens often:

1) I want to divide x by y.
2) I will check first, to see if y is zero.
3) If y = 0, don't do the division.

See the problem? If you really, REALLY mean y = 0, then y = 0.00000000000000000000000000001 will not trigger the error trap and you will not be happy with the results.
• Jan 27th 2010, 07:09 PM
zarka
True thing. Luckily there is no place where I will have a 0 in denominator. But thanks for the note (Hi)
• Jan 27th 2010, 11:11 PM
CaptainBlack
Quote:

Originally Posted by zarka
Hey everyone

While I was programming on matlab I encountered something funny. I don't know if its a bug or what.

Now we all know that cos(pi/2) should be 0. Right??

Now do this:

Code:

a = 55; theta = pi/2; P = [a1*cos(theta); a1*sin(theta)]
run this, you should get that outcome:

Code:

P =       0.0000   55.0000

Now here is the messed up part: Input this line now:

Code:

P(1)                  %to get the value of first row

Code:

ans =     3.3678e-015

See it here:
Code:

>> a = 55   a =       55   >> theta = pi/2   theta =       1.5708   >> P = [a1*cos(theta); a1*sin(theta)]   P =       0.0000   55.0000   >> P(1)   ans =     3.3678e-015

Why the heck is this happening?? what am I doing wrong??

Please try it on your own matlab. I am using Matlab 7.9 (R2009b)

This should be the simplest code in matlab.

help!

Another thing: what is "a1"? you appear only to have defined "a"

CB
• Jan 28th 2010, 02:01 PM
zarka
sorry that was a typo. They are the same. a is a1.

Cheers,
• Jan 28th 2010, 03:20 PM
TKHunny
Quote:

Originally Posted by zarka
True thing. Luckily there is no place where I will have a 0 in denominator. But thanks for the note (Hi)

It was only an example. There are many other places where it is important.
• Jan 29th 2010, 09:07 AM
CaptainBlack
What everyone doing numerical computation should know about floating point: see here

CB