PDA

View Full Version : Sine using Taylor Series



JonK
11-29-2014, 07:22 AM
Found This:
Sub Sine()

Option Explicit

Sub SinXSeriesExpansion()

Dim M As Long, N As Long, X As Double, SineX As Double

X = InputBox("What is X?")
M = InputBox("How many terms?")

If IsNumeric(M) And M > 0 Then
SineX = 0
On Error GoTo OverFlow
For N = 0 To M \ 1
SineX = SineX + ((-1) ^ (N) * X ^ (2 * N + 1)) / Evaluate("Fact(" & 2 * N + 1 & ")")
Range("A" & N + 1) = SineX
Next
MsgBox SineX
Else
MsgBox "Need the number of terms to be greater than zero!"
End If
Exit Sub

OverFlow:
MsgBox "Too many terms - try fewer terms", , "OverFlow Error!!"
End Sub

Cannot make it run
Says Option Explicit is Invalid Inside Procedure Help says remove it so I did, and it says compile error Expected End Sub I am in over my head

snb
11-29-2014, 08:03 AM
You might start with studying VBA fundamentals first.

ashleyuk1984
11-29-2014, 09:54 AM
Remove the first line. "Sub Sine()"

Paul_Hossler
11-30-2014, 01:15 PM
I suspect that the 'Sub Sine()' part might have been a label or text and was not intended to be part of the VBA

Option Explicit only goes one time at the top of a Standard Module




Option Explicit

Sub SinXSeriesExpansion()

Dim M As Long, N As Long, X As Double, SineX As Double

X = InputBox("What is X?")
M = InputBox("How many terms?")

If IsNumeric(M) And M > 0 Then
SineX = 0
On Error GoTo OverFlow

For N = 0 To M \ 1
SineX = SineX + ((-1) ^ (N) * X ^ (2 * N + 1)) / Evaluate("Fact(" & 2 * N + 1 & ")")
Range("A" & N + 1) = SineX
Next

MsgBox SineX

Else
MsgBox "Need the number of terms to be greater than zero!"
End If

Exit Sub

OverFlow:
MsgBox "Too many terms - try fewer terms", , "OverFlow Error!!"
End Sub