need help to code simpson 3/8 rule

shm_yli

New Member
Joined
May 21, 2021
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
i ask a for a help in this simpson 3/8 rule, in the past few days, and i cant really solve it, because in my assignment i need to show the vba code on how to compute my given, this is my given :

x | 0 | 20| 40 |60 |80 |100 |120|
y| 0 |7.4 |11.8 |15.2 |15| 9.2 |0.2|
h= 20 n= 6 segments

someone answered me but, i don't really get it, so i'm asking again for help again in this, on how i can code this in vba.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'd never heard of Simpsons 3/8 rule until I read this. However, here is code that I think is manipulating your data correctly?
I have output the various elements for illustrative purposes.

MRXLMAY21.xlsm
ABCDEFGHIJ
1
2xx020406080100120
3y = f(x)y07.411.815.2159.20.2
4
5Low Limit xa0
6High Limit xb120
7n Intervalsn6
8Increment hh20
9
10Simpson's 3/8 Approximation
11Simpson Sum Multiple
12Simpson sum161
13Simpson 3h/8 Multiplier7.5
14
15Result1206
16
BarCodes


VBA Code:
Sub Simpsons()
Dim Rngx As Range
Dim Rngy As Range
Dim a, b, c, n, h
Dim SSum, SMult, LastCol
Dim Result

'Assume data is in Rows 2 & 3 starting column D
LastCol = Cells(2, Cells.Columns.Count).End(xlToLeft).Column

'data range
Set Rngx = Range(Cells(2, 4), Cells(2, LastCol))
Set Rngy = Range(Cells(3, 4), Cells(3, LastCol))

'intervals n
n = Rngx.Columns.Count - 1
If Not n Mod 3 = 0 Then
    MsgBox "Intervals 'n' MUST be a multiple of 3.  Please check your data and try again."
    Exit Sub
End If

'a = low x   b = High x
a = Rngx(1, 1)
b = Rngx(1, LastCol - 3)

'increment h
h = (b - a) / n

'Simpson sum elements for y as F(x) 0 to n
'Low and high limit values
SSum = Rngy(1, 1) + Rngy(1, LastCol - 3)

'intermediate elements attracting multiples
For c = 1 To LastCol - 5
    If c Mod 3 = 0 Then
        SSum = SSum + 2 * Rngy(1, c + 1)   'Elements with multiple 2
    Else
        SSum = SSum + 3 * Rngy(1, c + 1)    'Elements with multiple 3
    End If

Next c

'Calculate the overall multiplier
SMult = 3 / 8 * h

Result = SMult * SSum

'Illustrate
Range("D5") = a
Range("D6") = b
Range("D7") = n
Range("D8") = h

Range("B12") = SSum
Range("B13") = SMult

Range("B15") = Result

End Sub

Hope that helps.
 
Upvote 0
I'd never heard of Simpsons 3/8 rule until I read this. However, here is code that I think is manipulating your data correctly?
I have output the various elements for illustrative purposes.

MRXLMAY21.xlsm
ABCDEFGHIJ
1
2xx020406080100120
3y = f(x)y07.411.815.2159.20.2
4
5Low Limit xa0
6High Limit xb120
7n Intervalsn6
8Increment hh20
9
10Simpson's 3/8 Approximation
11Simpson Sum Multiple
12Simpson sum161
13Simpson 3h/8 Multiplier7.5
14
15Result1206
16
BarCodes


VBA Code:
Sub Simpsons()
Dim Rngx As Range
Dim Rngy As Range
Dim a, b, c, n, h
Dim SSum, SMult, LastCol
Dim Result

'Assume data is in Rows 2 & 3 starting column D
LastCol = Cells(2, Cells.Columns.Count).End(xlToLeft).Column

'data range
Set Rngx = Range(Cells(2, 4), Cells(2, LastCol))
Set Rngy = Range(Cells(3, 4), Cells(3, LastCol))

'intervals n
n = Rngx.Columns.Count - 1
If Not n Mod 3 = 0 Then
    MsgBox "Intervals 'n' MUST be a multiple of 3.  Please check your data and try again."
    Exit Sub
End If

'a = low x   b = High x
a = Rngx(1, 1)
b = Rngx(1, LastCol - 3)

'increment h
h = (b - a) / n

'Simpson sum elements for y as F(x) 0 to n
'Low and high limit values
SSum = Rngy(1, 1) + Rngy(1, LastCol - 3)

'intermediate elements attracting multiples
For c = 1 To LastCol - 5
    If c Mod 3 = 0 Then
        SSum = SSum + 2 * Rngy(1, c + 1)   'Elements with multiple 2
    Else
        SSum = SSum + 3 * Rngy(1, c + 1)    'Elements with multiple 3
    End If

Next c

'Calculate the overall multiplier
SMult = 3 / 8 * h

Result = SMult * SSum

'Illustrate
Range("D5") = a
Range("D6") = b
Range("D7") = n
Range("D8") = h

Range("B12") = SSum
Range("B13") = SMult

Range("B15") = Result

End Sub

Hope that helps.
uhm. I copy paste this, and i get this msgbox "Intervals 'n' MUST be a multiple of 3. Please check your data and try again." i don't know if i still need to input some data or not.. and im asking if it is really smult = 3 / 8 * h or smult = 3 * h / 8 ??

thanks for answering my question.
 
Upvote 0
uhm. I copy paste this, and i get this msgbox "Intervals 'n' MUST be a multiple of 3. Please check your data and try again." i don't know if i still need to input some data or not.. and im asking if it is really smult = 3 / 8 * h or smult = 3 * h / 8 ??

thanks for answering my question.
Then I assume that you have data in row 2 to the right of column J?
The original code was looking to be able to find variable number of columns. I have edited so that LastCol is now fixed at 10 for colum J.
3/8*h or 3*h/8, mathematically the same. I have however changed it.

VBA Code:
Sub Simpsons()
Dim Rngx As Range
Dim Rngy As Range
Dim a, b, c, n, h
Dim SSum, SMult, LastCol
Dim Result

'Assume data is in Rows 2 & 3 starting column D

'In this instance data range is fixed D2:J3 so
LastCol = 10  'To limit to column J
'data range
Set Rngx = Range(Cells(2, 4), Cells(2, LastCol))
Set Rngy = Range(Cells(3, 4), Cells(3, LastCol))

'intervals n
n = Rngx.Columns.Count - 1
If Not n Mod 3 = 0 Then
    MsgBox "Intervals 'n' MUST be a multiple of 3.  Please check your data and try again."
    Exit Sub
End If

'a = low x   b = High x
a = Rngx(1, 1)
b = Rngx(1, LastCol - 3)

'increment h
h = (b - a) / n

'Simpson sum elements for y as F(x) 0 to n
'Low and high limit values
SSum = Rngy(1, 1) + Rngy(1, LastCol - 3)

'intermediate elements attracting multiples
For c = 1 To LastCol - 5
    If c Mod 3 = 0 Then
        SSum = SSum + 2 * Rngy(1, c + 1)   'Elements with multiple 2
    Else
        SSum = SSum + 3 * Rngy(1, c + 1)    'Elements with multiple 3
    End If

Next c

'Calculate the overall multiplier
SMult = 3 * h / 8

Result = SMult * SSum

'Illustrate
Range("D5") = a
Range("D6") = b
Range("D7") = n
Range("D8") = h

Range("B12") = SSum
Range("B13") = SMult

Range("B15") = Result

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top