Click to See Complete Forum and Search --> : Sin and Cos without built in functions
mitchell
Aug 8th, 2002, 09:53 AM
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
bugzpodder
Aug 8th, 2002, 09:57 AM
use Taylor's Series (think thats whats it called) but it is wayyyy too complex.
DavidHooper
Aug 8th, 2002, 12:53 PM
The Macularin Series is the one you want - just a simplification of the Taylor Series.
Anyway, look here (http://www.maths.abdn.ac.uk/~igc/tch/index/eg1006/notes/node88.html), about halfway down for the equations you require.
MathImagics
Aug 8th, 2002, 01:03 PM
How's this?
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 :cool:
MathImagics
Aug 8th, 2002, 01:27 PM
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 :cool:
DavidHooper
Aug 9th, 2002, 01:52 AM
Do I remember that it must converge most quickly out of all series, due to the virtue of it being a Taylor series?
MathImagics
Aug 9th, 2002, 02:41 AM
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!" :p
Dr Memory :cool:
mitchell
Aug 9th, 2002, 07:23 AM
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 :(
Alphanos
Aug 9th, 2002, 11:17 PM
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)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.