|
-
Aug 8th, 2002, 09:53 AM
#1
Thread Starter
New Member
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
-
Aug 8th, 2002, 09:57 AM
#2
Fanatic Member
use Taylor's Series (think thats whats it called) but it is wayyyy too complex.
-
Aug 8th, 2002, 12:53 PM
#3
Hyperactive Member
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.
-
Aug 8th, 2002, 01:03 PM
#4
Addicted Member
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)
-
Aug 8th, 2002, 01:27 PM
#5
Addicted Member
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)
-
Aug 9th, 2002, 01:52 AM
#6
Hyperactive Member
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.
-
Aug 9th, 2002, 02:41 AM
#7
Addicted Member
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)
-
Aug 9th, 2002, 07:23 AM
#8
Thread Starter
New Member
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
-
Aug 9th, 2002, 11:17 PM
#9
Hyperactive Member
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|