can anyone help me in this simpson's 3/8 rule code, ?

shm_yli

New Member
Joined
May 21, 2021
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Sub simp38()
a = 0
b = 120
n = 3
h = (b - a) / n
I = h * (f(a) + f(b)) / 2
For m = 2 To n
I = I + f(a + h * (m - 1)) * h
Next
Range("b3").Value = I

End Sub


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

im really a new in this and dont have any background in this type of program, i hope you can help me
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I believe you're missing having the f function defined. Isn't that supposed to be the x value raised to the 4th power or something like that?

Code:
Function f(x)
 f = x ^ 4
End Function
 
Upvote 0
I believe you're missing having the f function defined. Isn't that supposed to be the x value raised to the 4th power or something like that?

Code:
Function f(x)
 f = x ^ 4
End Function
then is that code is correct ? I just see that in the internet and dont know if that is the correct code
 
Upvote 0
'Dim variables

Dim a, b, x, i, n, k As Integer
Dim Sum, Delta, Boo, inte1, Sim, Sim1 As Double
Dim inte As Variant




'Set values
a = Val(InputBox("Please enter the lower limit, a"))
b = Val(InputBox("Please enter the upper limit, b"))
inte = Val(InputBox("How many intervals would you like to use?"))
x = a
Delta = ((b - a) / inte)
i = 1
Sum = 0
inte1 = 0
'Program loop

For i = 1 To inte + 1

If inte Mod 2 = 0 Then
If i = a Or i = inte + 1 Then
Sim = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
x = x + Delta
ElseIf i Mod 2 = 0 Then:
Sim = (4 * (Application.WorksheetFunction.Ln(x) * (x ^ 2)))
x = x + Delta
Else
Sim = (2 * (Application.WorksheetFunction.Ln(x) * (x ^ 2)))
x = x + Delta
End If

ElseIf inte Mod 3 = 0 Then

If i = a Or i = inte + 1 Then
Sim = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
x = x + Delta
Else: Sim = 3 * (Application.WorksheetFunction.Ln(x) * (x * 2))
x = x + Delta
End If

Else
inte1 = inte - 3
inte = 3
For n = 1 To inte + 1

If n = a Or n = inte + 1 Then
Sim = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
x = x + Delta
ElseIf i Mod 2 = 0 Then
Sim = (4 * (Application.WorksheetFunction.Ln(x) * (x ^ 2)))
x x + Delta

Else
Sim = (2 * (Application.WorksheetFunction.Ln(x) * (x ^ 2)))
x = x + Delta
End If
Next n

Sim = Sum + Sim
Next i

Sim = (Delta / 3) * (Sim)

Sim1 = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
x = x + Delta
Sim2 = (3 * (Application.WorksheetFunction.Ln(x) * (x ^ 2)))
x = x + Delta
Sim3 = (Application.WorksheetFunction.Ln(x) * (x ^ 2))

Sum = Sim1 + Sim2 + Sim3
Sum = ((3 * Delta) / 8)

Sum = Sum + Sim
Next n


Cells(20, i + 2) = Sum


Boo = (Delta / 3) * Sum

Cells(20, 1) = "The approxomation is "
Cells(20, 2) = Boo


i found it in the internet, can i use this code ?
 
Upvote 0
you're missing having the f function defined. Isn't that supposed to be the x value raised to the 4th

Huh?! f(x) is the y value at x. We do not have a continuous function f(x). But @shm_yli provides 6 pairs of x and y, which are just the right set of values that we need to apply Simpson's 3/8 rule.

-----

Sim = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
[....]
then is that code is correct ?

Certainly not! If assumes a different f(x), unrelated to your data.

-----

i found it in the internet, can i use this code ?

No.

I am in the middle of preparing a knowledgeable and informative response for you. Please give me some time to finish.
 
Upvote 0
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

Does the assignment require a VBA solution?

I don't know why you would both with a VBA subroutine with hardcoded values. Such calculations can be done with Excel formulas directly.

Are you required to provide a general solution for any h and any n+1 data?

Even if so, I would implement a parameterized VBA function, not a subroutine.

-----

Moreover, are you sure that you should implement Simpson's 3/8 rule per se?

The data that you provide -- h=20 and n=6 -- is suspiciously exactly what we need to calculate Composite Simpson's 3/8 rule.

Moreover, the loop that you prototype in your VBA procedure applies to Composite Simpson's 3/8 rule, not to Simpson's 3/8 rule.

By coincidence, the same data is also sufficient to calculate Simpson's 3/8 rule. But Simpson's 3/8 rule has only 4 terms, not n+1 terms like your loop.

The definitions and formulas for the various Simpson's rule can be found in the Simpson's rule wikipage (click here).

For Simpon's 3/8 rule:
1624415842236.png

Note that we require f(x) at x = (2*a+b)/3 and x = (a+2*b)/3. For a=0 and b=120, those are x=40 and x=80. And fortunately, we are given f(40) and f(80), which are the corresponding values of y.

For Composite Simpson's 3/8 rule:

1624415967307.png


It might helpful to start by charting the data that you are given.

1624416382897.png


The goal is to calculate the area under that "curve" (collection of line segments).

(Do you really want the area under a best-fit curve? An order-6 polynomial trendline can fit 7 data points exactly. I hope to provide that in a later response. But I will stick with this for now, since it might be what you want, anyway.)

The following shows one implementation, using just Excel.

simpson's rule.xlsx
ABCDEFGHI
1
2ORIGINAL DATA
3xy1209.00000000000simpson's 3/8 rule (simplified)
400.01209.00000000000simpson's 3/8 rule (general)
5207.4
64011.81432.50000000000composite simpson's 3/8 rule (simplified)
76015.2
88015.01374.00000000000check: trapezoid rule
910019.2
101200.2
line seg
Rich (BB code):
Formulas:
D3: =((A10-A4)/8) * (B4 + 3*B6 + 3*B8 + B10)
D4: =((A10-A4)/8) * (B4 + 3*VLOOKUP((2*A4+A10)/3, A4:B10, 2, 0) + 3*VLOOKUP((A4+2*A10)/3, A4:B10, 2, 0) + B10)
D6: =(3*(A10-A4)/6/8) * (B4 + 3*B5 + 3*B6 + 2*B7 + 3*B8 +3*B9 + 2*B10)
D8: =SUMPRODUCT(A5:A10-A4:A9, B5:B10+B4:B9)/2

D8 uses the trapezoid rule, another common method. It is provided a check, to demonstrate the reasonableness of the results of the Simpson's rules. It is not part of the solution, for your purposes.

D3 is a straight-forward implementation of the (regular) Simpson's 3/8 rule. It references B6 and B8 directly, which are f(40) and f(80).

D4 is a more-general form of the same formula. It uses VLOOKUP to find the values of y (f(x)) that correspond to x = (2*a+b)/3 and x=(a+2*b)/3.

D6 is a simplified implementation of the Composite Simpson's 3/8 rule. It is tailored to the 7 pairs of data that you provide.

We can provide a more general implementation for any set of n+1 data. That would be more straight-forward to implement with a VBA function.

But I will wait for you to clarify the assignment, answering my questions above, before investing more time.

I hope you find this informative and useful and progressing toward to the solution that you require.
 
Upvote 0
Solution
Minor typos, too late to edit....
[.... oh, never mind! I see I made other minor typos, too. sigh ....]
 
Upvote 0
Reposting.... In my haste to respond and dealing with this forum's design, I screwed up my previous response big time, misrepresenting quotes. This forum limits the time that we can make corrections. So I'm sorry for this incessant repost. I hope I get it right this time.


you're missing having the f function defined. Isn't that supposed to be the x value raised to the 4th power
[....]
Function f(x)
f = x ^ 4
End Function

Huh?! f(x) is the y value at x. @shm_yli does not have a continuous function f(x). Instead, @shm_yli provides 7 pairs of x and y, which are just the right set of values that we need to apply Simpson's 3/8 rule.


then is that code is correct ?

Certainly not. It assumes a different f(x) that is completely unrelated to your data.


Sim = (Application.WorksheetFunction.Ln(x) * (x ^ 2))
[....]
i found it in the internet, can i use this code ?

No. That also assumes a different f(x). In fact, it does not even look like a valid implementation of Simpson's 3/8 rule -- either one. Perhaps a different one of Simpson's rule; I don't know.
 
Upvote 0
Reposting.... In my haste to respond and dealing with this forum's design, I screwed up my previous response big time, misrepresenting quotes. This forum limits the time that we can make corrections. So I'm sorry for this incessant repost. I hope I get it right this time.




Huh?! f(x) is the y value at x. @shm_yli does not have a continuous function f(x). Instead, @shm_yli provides 7 pairs of x and y, which are just the right set of values that we need to apply Simpson's 3/8 rule.




Certainly not. It assumes a different f(x) that is completely unrelated to your data.




No. That also assumes a different f(x). In fact, it does not even look like a valid implementation of Simpson's 3/8 rule -- either one. Perhaps a different one of Simpson's rule; I don't know.
thanks for the kind reply, so the code that i found is not right?, and that the given i have can be directly be solve in excel.
 
Upvote 0
i only find simpson 1/3 rule in the youtube and just wanna ask if that can also be use as a reference in making the code for this ??
 
Upvote 0

Forum statistics

Threads
1,215,236
Messages
6,123,799
Members
449,127
Latest member
Cyko

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