Results 1 to 5 of 5

Math Help - Programming the sin function

  1. #1
    Newbie
    Joined
    Mar 2007
    Posts
    11

    Programming the sin function

    If I were to write my own sine function, in some programming language, how would I do it?

    I know if given an angle, you can simply substitute that into a right triangle with hypotenuse 1 and determine the triangle. But to get to return the result, I keep coming back to using the sine function, which is what I'm trying to program.

    So I'm just curious as to how the sine function works to a computer.

    Thanks everyone.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by lucius View Post
    If I were to write my own sine function, in some programming language, how would I do it?

    I know if given an angle, you can simply substitute that into a right triangle with hypotenuse 1 and determine the triangle. But to get to return the result, I keep coming back to using the sine function, which is what I'm trying to program.

    So I'm just curious as to how the sine function works to a computer.

    Thanks everyone.
    First because \sin(x) is periodic with period 2\pi and has other symmetries we need only calculate it for x \in [0,\pi/2]

    Then:

     \sin(x)= x(1-x^2/6)+\varepsilon \ \ \ \ \ (1)

    where

    |\varepsilon|\le |x|^5/120\ \ \ \ \ \ \ \ (2)

    Also:

    \sin(x)=2\sin^2(x/2)-1 \ \ \ \ \ \ (3)

    So if x is small enough so that the error in (2) is acceptable then use (1). If the error is too big use (3) and \sin (x/2^n) in (1) with n choosen to give acceptable error.

    In practice a better polynomial approximation is used as are neater ways of doing the calculations in general.
    Last edited by CaptainBlack; April 9th 2010 at 09:19 PM.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Sep 2009
    Posts
    40
    Thanks
    5
    You can also use a Taylor Series Approximation, which requires some degree of iteration to get a good approximation. The formula for sine is: Summation from n = 0 to infinity of (-1^n)/(2n+1)! * x^(2n+1).
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member

    Joined
    May 2006
    From
    Lexington, MA (USA)
    Posts
    11,659
    Thanks
    600
    Hello, lucius!

    . . . So I'm just curious as to how the sine function works to a computer.

    I would use the infinite series for \sin x, where  x is in radians.

    . . \sin x \;=\;x - \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!} - \frac{x^9}{9!} - \frac{x^{11}}{11!} +\; \hdots

    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by macosxnerd101 View Post
    You can also use a Taylor Series Approximation, which requires some degree of iteration to get a good approximation. The formula for sine is: Summation from n = 0 to infinity of (-1^n)/(2n+1)! * x^(2n+1).
    Quote Originally Posted by Soroban View Post
    Hello, lucius!


    I would use the infinite series for \sin x, where  x is in radians.

    . . \sin x \;=\;x - \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!} - \frac{x^9}{9!} - \frac{x^{11}}{11!} +\; \hdots

    In my earlier post you will note that I used a Taylor/McLaurin series truncated at the x^3 term for my small angle approximation, and provided a bound for the error. Almost universally a long Taylor series is never used. The general approach is to use a polynomial approximation for small angles and then the properties of the trig functions to reduce the required angle to something in the valid range of the approximation.

    The fact that I used a Taylor approximation is of course misleading, in practice an optimal polynomial for the required angle range is used.

    Also Google for "cordic algorithms"

    Note in the approximation that I used the maximum error <10^{-12} when |x|<0.01 radian.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: September 23rd 2010, 08:33 AM
  2. Linear Programming~Please help!
    Posted in the Math Topics Forum
    Replies: 5
    Last Post: April 13th 2010, 01:12 PM
  3. Programming For HP 48G+
    Posted in the Calculators Forum
    Replies: 0
    Last Post: February 3rd 2009, 03:09 AM
  4. Replies: 1
    Last Post: November 17th 2008, 03:18 AM
  5. Please help on R programming
    Posted in the Math Software Forum
    Replies: 0
    Last Post: August 16th 2008, 02:51 AM

Search Tags


/mathhelpforum @mathhelpforum