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

1. ## 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!

2. 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 $\sqrt[4]{\epsilon_{mach}}$ may be cited.

Machine error is an important consideration. You must not overlook it.

3. Thanks TKHunny. I know its too close to zero but I kinda bugged me a bit

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

Regards,

4. 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.

5. True thing. Luckily there is no place where I will have a 0 in denominator. But thanks for the note

6. 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

7. sorry that was a typo. They are the same. a is a1.

Cheers,

8. Originally Posted by zarka
True thing. Luckily there is no place where I will have a 0 in denominator. But thanks for the note
It was only an example. There are many other places where it is important.

9. What everyone doing numerical computation should know about floating point: see here

CB