Inverse Hyperbolic secant and cosecant don't yield correct result

Discussion in 'Mac Programming' started by jsmwoolf, Aug 26, 2011.

  1. jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    I'm trying to insert hyperbolic functions into my Calculator application in Java. So far, everything has been good except for the inverse hyperbolic functions for secant and cosecant.
    [​IMG]
    Code:
    void acsch(double x)
    	{
    	control.currentNumber = Math.log((1/x) + (Math.sqrt((1/(x*x)) + 1.0)));
    	}
    
    	void asech(double x)
    	{
    	control.currentNumber = Math.log((1/x) + Math.sqrt((1/(x*x)) - 1.0));
    	}
    The formulas from the picture vs. the code for inverse hyperbolic secant and cosecant, look the same. But vs. a TI-84 Calculator calculation(1.134592657 for cosecant(1)), my calculator yields 0.8813735870195429 for cosecant(1) which is incorrect. As for secant(2)(.7593257175 for the TI-Calculator), my calculator yields NaN for secant(2). Even though they look the same, why don't they function the same?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    So for acsch it looks like you had the right result, but in the code you posted the + 1 was outside of the square root. I changed the formula for asech to something completely different, because i didn't understand how you'd get a not-imaginary result for most values with the formula you posted.

    return 2/(Math.pow(Math.E,x) + Math.pow(Math.E,-1*x));

    I got the formula from Wolfram, which was what came up first. I have no idea what's going on with your calculator. Are you using the hyperbolic versions?

    -Lee
     
  3. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Oops, i was looking at not-inverse versions.

    I don't have a graphing calculator available, so i can't account for the differences, but:
    http://www.wolframalpha.com/input/?i=hyperbolic+arccosecant+1
    http://www.wolframalpha.com/input/?i=hyperbolic+arcsecant+2

    I couldn't work out inverse hyperbolic secant of 2 using the formulas you provided because:
    sqrt(1/2 - 1) is imaginary.
    Wolfram went ahead and rolled with it and gives an imaginary result.

    I did work out the hyperbolic arccosecant of 1 using the formula you posted:
    ln(1/1 + sqrt(1/1 + 1)
    ln(1 + sqrt(2))
    ln(1 + 1.414213562373095)
    0.881373587019543

    Which is what wolfram gives.

    Java's math functions operate on primitives (double, mostly), which can't represent imaginary numbers. This is why you're getting NaN, I'm guessing. Once you get a NaN from sqrt(-1/2), the NaN spreads through the rest of the calculation.

    -Lee
     
  5. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #5
    But, my question is how come inverse hyperbolic secant of 2 is imaginary for my calculator and your results from wolfram, but it doesn't appear to be this way for my TI-84 or the Calculator app on the Mac? Who's exactly right? Our results, or the TI-84 and the Calculator app on the Mac? It appears that I found a site where there are ranges for these inverse functions.

    sinh-1 x = ln (x +√(x^2 +1)) -∞ < x < ∞

    cosh-1 x = ln (x + √(x^2-1)) x ≥ 1 [cosh-1 x > 0 is principal value]

    tanh-1x = ½ln((1 + x)/(1 - x)) -1 < x < 1

    coth-1 x = ½ln((x + 1)/(x - 1)) x > 1 or x < -1

    sech-1 x = ln ( 1/x + √(x^-2-1)) 0 < x ≤ 1 [sech-1 a; > 0 is principal value]

    csch-1 x = ln(1/x + √(x^-2+1)) x ≠ 0

    Yet, my question is how come the TI-84 and the Calculator app on the Mac does the exact opposite for the ranges of sech-1 x? Are we missing something to these results? My calculator tends to abide with these limits just fine.

    Sorry if I'm throwing all these "How come...?" questions at you. This is very strange. Even the Texas Institute TI-84 tends to abide by the antithesis of the limits.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    I'm pretty good at programming, but pretty mediocre at math. I took Calculus, and probability, and something that focused on matrices, but it's been a long time. Between that and not having a graphing calculator I can't be of much more help (honestly, there's a TI-82 or 83 in a closet somewhere, but not readily accessible).

    Does the OS X calculator have inverse hyperbolic trigonometric available? I guess I've never looked.

    I guess what I do know is that I'll take Wolfram's word for it. They're a successful math company, so I'm guessing they're pretty good with math. Maybe your calculator is using some odd coordinate system? Have you graphed these or just calculated results?

    -Lee
     
  7. dylanryan, Aug 26, 2011
    Last edited: Aug 26, 2011

    dylanryan macrumors member

    Joined:
    Aug 21, 2011
    #7
    The Mac OSX Calculator also gives Not a number. sech^-1(x) = cosh^-1(1/x) (see Wolfram). So, sech^-1(2) = cosh^-1(1 /2) and the Mac calculator says "Not a number" for that.... Don't have another calculator handy, but I suspect what you are doing (based on the number that you said your calculator gave) is doing 1/cosh^-1(2), which is not the same as sech^-1(2).
     
  8. jsmwoolf, Aug 26, 2011
    Last edited: Aug 26, 2011

    jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #8
    Well, I get the same results as the Wolfram company with my calculator(not the TI-84), so it appears that my calculator leans towards their results.

    So far, I'm only calculating results. Later on, I will consider on add a graphing feature to match the TI-84's power. However, it's a long ways from there.

    dylanryan,

    I'm doing Inverse Hyperbolic Secant through this:
    Code:
    HyperBolic.asech(currentNumber);
    which the function contains
    Code:
    	void asech(double x)
    	{
    	control.currentNumber = Math.log((1/x) + Math.sqrt((1/(x*x)) - 1.0));
    	}
    However, how come 1/cosh^-1(2) isn't the same as sech^-1(2)? If 1/sin(x) = csc(x), then how come 1/sin^-1(x) doesn't equal csc^-1(x)?
     
  9. dylanryan, Aug 26, 2011
    Last edited: Aug 26, 2011

    dylanryan macrumors member

    Joined:
    Aug 21, 2011
    #9
    I think you are looking at things wrong. sec(x) = 1/cos(x), but sec^-1(x)=cos^-1(1/x). That's just how the trig functions work (I forget why exactly). The same holds for the hyperbolic variants. Look them up on Wolfram or Wikipedia.

    Example links:

    http://en.wikipedia.org/wiki/Trigonometric_functions#Reciprocal_functions
    search for "but other trigonometric functions can be defined by", it is the image right after the only occurrence of that.

    http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
    search for "Reciprocal arguments:". Again, the images just below that.

    Again, the same holds for the hyperbolics as well. Sadly, if you want more of a "why" than "because that's what wikipedia and wolfram say", I can't help. Suffice it to say, that is just how they work.

    Edit: Is there any code I can use to actually make superscripts in this forum?
     
  10. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #10
    Oh okay, thanks for pointing it out for me. Now I have another Calculator source and I finally fixed my issue about inverse csc, sec, and cot for regular and hyperbolic for my application.
     

Share This Page