Results 1 to 9 of 9

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

  1. #1
    Newbie
    Joined
    Dec 2009
    Posts
    4

    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

    And here is the answer

    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!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Aug 2007
    From
    USA
    Posts
    3,110
    Thanks
    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.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Dec 2009
    Posts
    4
    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,
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor
    Joined
    Aug 2007
    From
    USA
    Posts
    3,110
    Thanks
    2
    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.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Dec 2009
    Posts
    4
    True thing. Luckily there is no place where I will have a 0 in denominator. But thanks for the note
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by zarka View Post
    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

    And here is the answer

    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
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Dec 2009
    Posts
    4
    sorry that was a typo. They are the same. a is a1.

    Cheers,
    Follow Math Help Forum on Facebook and Google+

  8. #8
    MHF Contributor
    Joined
    Aug 2007
    From
    USA
    Posts
    3,110
    Thanks
    2
    Quote Originally Posted by zarka View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    What everyone doing numerical computation should know about floating point: see here

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Funny Munny
    Posted in the Math Topics Forum
    Replies: 0
    Last Post: May 29th 2011, 06:27 AM
  2. Interesting (and funny) exercise
    Posted in the Differential Geometry Forum
    Replies: 2
    Last Post: October 6th 2010, 05:45 AM
  3. [SOLVED] Funny double-equation problem
    Posted in the Trigonometry Forum
    Replies: 10
    Last Post: July 20th 2010, 07:33 AM
  4. funny trigonometric equation
    Posted in the Trigonometry Forum
    Replies: 2
    Last Post: August 23rd 2008, 03:31 AM
  5. Funny problem I had...
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: October 22nd 2007, 09:36 AM

Search Tags


/mathhelpforum @mathhelpforum