Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Unexpected matlab answer

  1. Aug 20, 2011 #1
    1. The problem statement, all variables and given/known data

    I put the following code into MATLAB:



    The answer gives:


    and i know that d/0.1 is equal to 1. so, i expect the answer of y to be
    y=1.570796326794897 (which is pi/2)
    but, MATLAB gives:

    y= 1.570796326794897 - 0.000000021073424i

    2. Relevant equations

    3. The attempt at a solution

    if i put:
    then it would give:
    y= 1.570796326794897

    But, i want to know why the answer is complex valued when it uses 0.1000000000000000 ?

    Thank you in advance.
  2. jcsd
  3. Aug 20, 2011 #2


    User Avatar
    Homework Helper

    The only reason I can think of is rounding error. The computer thinks .1000000000000 > .1, so when computing d/.1, you obtain a value greater than 1. Taking the arcsin of a value greater than 1, as you know, does not exist for real numbers. This is why there's a small complex term which contributes to the answer.
  4. Aug 21, 2011 #3


    User Avatar
    Science Advisor

    Hi Sara87. Floating point numbers on a computer generally cant be represented exactly, but this is usually hidden from the user by limiting the number of significant digits to which results are displayed. What's unusual in this case is that you are operating precisely on the boundary of the real domain (and the extended complex domain) of the asin() function.

    BTW. Are you familiar with complex numbers?

    Code (Text):

    > format long

    > d=0.3
    d =  0.300000000000000
    > ((d - 0.2)/0.1)*1e16
    ans = 9999999999999998

    > d = 0.2+0.1
    d = 0.300000000000000
    > ((d - 0.2)/0.1)*1e16
    ans = 10000000000000002

    Last edited: Aug 21, 2011
  5. Aug 21, 2011 #4
    Thank you both.
    Yes, i am familiar with the complex numbers.
    So, how can i change my code so that is gives pi/2 instead of pi/2+ some complex number ?
  6. Aug 21, 2011 #5


    User Avatar
    Science Advisor

    There are several possible ways.

    The simplest, if you're certain your result should be real, is: y = real(asin(...))

    OR you could test if the arguement is in [-1-delta ... +1+delta], then throw an error if it isn't and clip it back to [-1...1] if it is.
  7. Aug 21, 2011 #6
    Thank you. I will use y=real(asin(...))
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook