Results 1 to 9 of 9

Thread: Sin and Cos without built in functions

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8

    Sin and Cos without built in functions

    Hi all,

    Not really a VB question but it's maths all the same.

    How do you work out sin and cos without using the built in trig functions in VB?

    Thanks,
    AM

  2. #2
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    use Taylor's Series (think thats whats it called) but it is wayyyy too complex.

  3. #3
    Hyperactive Member DavidHooper's Avatar
    Join Date
    Apr 2001
    Posts
    357
    The Macularin Series is the one you want - just a simplification of the Taylor Series.

    Anyway, look here, about halfway down for the equations you require.
    There are 10 types of people in the world - those that understand binary, and those that don't.

  4. #4
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    How's this?

    Code:
       Dim sinValue As Single, cosValue As Single, N As Single
       Dim X As Single, sinX As Single, cosX As Single
       Dim nTerm As Single, Add As Boolean, pTerm As Single
       
       Const Accuracy = 0.0000000001   ' 10 dec places
    
       If Not IsNumeric(Text1.Text) Then Beep: Exit Sub
       
       X = Text1.Text
       
       cosValue = 1#
       sinValue = X
       
       sinX = Sin(X) ' for comparison
       cosX = Cos(X)
       Label1.Caption = "Sin(X) = " & Format(sinX, "#0.00000000")
       Label2.Caption = "Cos(X) = " & Format(cosX, "#0.00000000")
       
       nTerm = X
       N = 1#
       
       Do
          N = N + 1: nTerm = nTerm * X / N
          If Add Then cosValue = cosValue + nTerm Else cosValue = cosValue - nTerm
          pTerm = nTerm
          
          N = N + 1: nTerm = nTerm * X / N
          If Add Then sinValue = sinValue + nTerm Else sinValue = sinValue - nTerm
          
          Add = Not Add
    
          If Abs(nTerm - pTerm) < Accuracy Then Exit Do
          Loop
        
        Label4.Caption = "Calc sin(X) = " & Format(sinValue, "#0.00000000")
        Label5.Caption = "Calc cos(X) = " & Format(cosValue, "#0.00000000")
        
        Label6.Caption = "Terms required = " & N
        
    End Sub
    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  5. #5
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    Both SIN and COS can be calculated simultaneously from the series

    1, x, x^2/2, x^3/3! .... X^n/n! ....

    The nth term is easily obtained from the previous term by multiplying by X/n, so it's a doddle to code (see above)

    Then it's just a matter of accumulating alternate terms, jiggling the sign of each pair of terms, adding a convergence test for the desired accuracy and Bob's your uncle!

    I'm pretty sure most calculators and intrinsic functions use this method, it converges pretty quickly.....

    Dr Memory
    Last edited by MathImagics; Aug 8th, 2002 at 01:31 PM.
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  6. #6
    Hyperactive Member DavidHooper's Avatar
    Join Date
    Apr 2001
    Posts
    357
    Do I remember that it must converge most quickly out of all series, due to the virtue of it being a Taylor series?
    There are 10 types of people in the world - those that understand binary, and those that don't.

  7. #7
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    I think that's right, but while proving that the T series does converge is easy, proving the rate of convergence, absolutely or relatively, is not!

    As they say in the text books, "we'll leave that as an exercise for the reader!"


    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    Thanks everyone for your help. I'll try those solutions later.

    It was actually for a program in Flash4 that I needeed to find this stuff out. No sin or cos there

  9. #9
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    If you wanted to store a table of sin data (which you probably wouldn't) you could use it to calculate cos & tan.

    cos(x) = sin(x+90)

    tan(x) = sin(x)/cos(x)
    =sin(x)/sin(x+90)
    Alphanos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width