+ Reply to Thread
Results 1 to 5 of 5

Simpson's rule for integration

Hybrid View

johnboy12 Simpson's rule for integration 06-14-2008, 03:27 PM
Mallycat http://www.mrexcel.com/forum/a... 06-14-2008, 05:57 PM
shg Here's another option. ... 06-14-2008, 07:21 PM
maistral Re: Simpson's rule for... 03-24-2014, 08:18 PM
shg Re: Simpson's rule for... 03-24-2014, 08:24 PM
  1. #1
    Registered User
    Join Date
    06-14-2008
    Posts
    12

    Simpson's rule for integration

    Dear all,

    Does anyone have some (relatively) simple code for calculating the Simpson's Rule for integration?

    Many thanks in advance.

    Regards,
    John

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    http://www.mrexcel.com/forum/archive...p/t-99062.html

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Here's another option.

    There are two functions. SimpsonInt is the integrator, and it can be used as a worksheet function. Func is the function to be integrated. What's there now is arbitrary; change it to whatever you need. To test with the existing function, try

    =SimpsonInt(0, PI()/2, n) for several values of n (the correct answer is 1)

    Holler back if you need help.
    Function SimpsonInt(a As Double, b As Double, ByVal n As Long) As Double
        ' Returns the integral of Func (below) from a to b _
          using Composite Simpson's Rule over n intervals
    
        Dim i       As Double  ' index
        Dim dH      As Double  ' step size
        Dim dOdd    As Double  ' sum of Func(i), i = 1, 3, 5, 7, ... n-1, i.e., n/2 values
        Dim dEvn    As Double  ' sum of Func(i), i =   2, 4, 6,  ... n-2  i.e., n/2 - 1 values
        ' 1 + (n/2) + (n/2 - 1) + 1 = n+1 function evaluations
    
        If n < 1 Then Exit Function
        If n And 1 Then n = n + 1 ' n must be even
        dH = (b - a) / n
    
        For i = 1 To n - 1 Step 2
            dOdd = dOdd + Func(a + i * dH)
        Next
    
        For i = 2 To n - 2 Step 2
            dEvn = dEvn + Func(a + i * dH)
        Next
    
        SimpsonInt = (Func(a) + 4# * dOdd + 2# * dEvn + Func(b)) * dH / 3#    ' weighted sum
    End Function
    
    Function Func(x As Double) As Double
        ' replace this function with the function to be integrated
        Func = Sin(x)
    End Function
    Last edited by shg; 06-15-2008 at 05:45 PM.

  4. #4
    Registered User
    Join Date
    02-15-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Simpson's rule for integration

    Sorry for bumping, but I really have to ask.

    Can you please tell me what's the hashtags for after the numerical values (4# and 2#)? Thanks.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Simpson's rule for integration

    Type declaration character for a Double.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1