Consulting

Results 1 to 4 of 4

Thread: Sine using Taylor Series

  1. #1

    Sine using Taylor Series

    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

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,647
    You might start with studying VBA fundamentals first.

  3. #3
    Remove the first line. "Sub Sine()"

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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