Post Reply 
Compact Simpson's 3/8 Rule(??)
12-13-2015, 02:26 PM (This post was last modified: 12-13-2015 03:07 PM by Namir.)
Post: #1
Compact Simpson's 3/8 Rule(??)
Here is the pseudo-code for a compact version of Simpson's 3/8 Rule. This rule divides the integral range [A, B] into N (=3m where m>1) divisions. The area is calculated using:

area = 3*h/8*(f(A) + 3*f(x(1)) + 3*f(x(2)) + 2*f(x(3)) + ... + f(B))

The following pseudo-code is able to apply the sequence of coefficient values of 3,3, and 2 for every three points, starting with x=A+h and until X=B-h.

Code:
Give f(x), interval [A, B] and N divisions where N=3m for any m>1.

h=(B-A)/N
Sum=f(A)+f(B)
I=1
Do
  A=A+h
  C= 2 + (3-I)*I/2
  Sum=Sum + C*f(A)
  I=I MOD 3 + 1
  N=N-1
Loop Until N=1
Area = 3*h/8*Sum

The variable I cycles between 1, 2, and 3. The coefficient C is calculated using a special (and simple) quadratic equation to yield 3, 3, and 2 for I=1, 2, and 3.

Here is an alternate form that uses memory registers (suitable for calculators)

Code:
Give f(x), interval [A, B] and N divisions where N=3m for any m>1.

Mem(1)=3
Mem(2)=3
Mem(3)=2

h=(B-A)/N
Sum=f(A)+f(B)
I=1
Do
  A=A+h
  Sum=Sum + Mem(I)*f(A)
  I=I MOD 3 + 1
  N=N-1
Loop Until N=1
Area = 3*h/8*Sum

The main point in this thread and the one about a compact (basic) Simpson's Rule is to perform one summation of f(x) per loop iteration.

Enjoy!

Namir
Find all posts by this user
Quote this message in a reply
12-13-2015, 03:38 PM (This post was last modified: 12-13-2015 03:50 PM by Dieter.)
Post: #2
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 02:26 PM)Namir Wrote:  The variable I cycles between 1, 2, and 3. The coefficient C is calculated using a special (and simple) quadratic equation to yield 3, 3, and 2 for I=1, 2, and 3.

Waaaayyyyyy too complicated. ;-)
Instead of i=1, 2, 3 make it 0, 1, 2 and get c = 2 + sign(i).
This doesn't even require two separate variables i and n (cf. second code sample).

Code:
h = (b - a) / n
sum = f(a) + f(b)

For i = 1 To n - 1
  c = 2 + Sgn(i Mod 3)
  sum = sum + c * f(a + i * h)
Next

result = 3 / 8 * h * sum

Or, if you do not like for-next-loops and prefer while/repeat:

Code:
h = (b - a) / n
sum = f(a) + f(b)
n = n - 1

Do
  a = a + h
  c = 2 + Sgn(n Mod 3)
  sum = sum + c * f(a)
  n = n - 1
Loop Until n = 0

result = 3 / 8 * h * sum

Those who are a bit paranoid about floating point arithmetics and exact zero results may replace the exit condition with something like Loop Until n+4711 = 4711. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
12-13-2015, 04:05 PM
Post: #3
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 03:38 PM)Dieter Wrote:  Those who are a bit paranoid about floating point arithmetics and exact zero results may replace the exit condition with something like Loop Until n+4711 = 4711. ;-)

Dieter

OK, I just gotta ask - why 4711 ?

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
12-13-2015, 04:39 PM (This post was last modified: 12-13-2015 07:50 PM by Dieter.)
Post: #4
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 04:05 PM)rprosperi Wrote:  OK, I just gotta ask - why 4711 ?

That's why. ;-)
Click on "history" and the year 1794 where the origin of this number is explained.

Of course you may just as well use any other three- or four-digit integer.

While we're at it, here's a quick and dirty version for the HP41 series.
It implements yet another way of generating the 3–3–2 sequence.

Code:
01  LBL"SIMP38"
02  " A↑B=?"
03  PROMPT
04  STO 02    ' save b temporarily in R02
05  STO 03
06  X<>Y
07  STO 01    ' save a in R01
08  ST- 03    ' save b - a in R03
09  XEQ E     ' f(a)
10  STO 00
11  RCL 02
12  XEQ E     ' f(b)
13  ST+ 00    ' sum = f(a) + f(b)
14  " N=?"
15  PROMPT
16  STO 02    ' store n in R02
17  ST/ 03    ' store h = (b - a) / n  in R03
18  3
19  MOD       ' validate n
20  CHS
21  SQRT      ' generate error if n mod 3 ≠ 0
22  DSE 02    ' n = n - 1
23  LBL 01    ' start of loop
24  RCL 03
25  ST+ 01    ' x = x + h
26  RCL 01
27  XEQ E     ' f(x)
28  RCL 02
29  3
30  MOD
31  X=0?      ' if i mod 3 = 0
32  DSE L     ' set c=2, else leave it at 3
33  X<> L
34  *         ' c * f(x)
35  ST+ 00    ' add to sum
36  DSE 02
37  GTO 01    ' end of loop
38  RCL 00
39  RCL 03
40  *
41  ,375
42  *         ' result = sum * h * 3/8
43  RTN

44  LBL E     ' function f(x)
45  1/x       ' place your f(x) code here
46  RTN

Dieter
Find all posts by this user
Quote this message in a reply
12-13-2015, 05:05 PM
Post: #5
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 03:38 PM)Dieter Wrote:  
(12-13-2015 02:26 PM)Namir Wrote:  The variable I cycles between 1, 2, and 3. The coefficient C is calculated using a special (and simple) quadratic equation to yield 3, 3, and 2 for I=1, 2, and 3.

Waaaayyyyyy too complicated. ;-)
Instead of i=1, 2, 3 make it 0, 1, 2 and get c = 2 + sign(i).
This doesn't even require two separate variables i and n (cf. second code sample).

Code:
h = (b - a) / n
sum = f(a) + f(b)

For i = 1 To n - 1
  c = 2 + Sgn(i Mod 3)
  sum = sum + c * f(a + i * h)
Next

result = 3 / 8 * h * sum

Or, if you do not like for-next-loops and prefer while/repeat:

Code:
h = (b - a) / n
sum = f(a) + f(b)
n = n - 1

Do
  a = a + h
  c = 2 + Sgn(n Mod 3)
  sum = sum + c * f(a)
  n = n - 1
Loop Until n = 0

result = 3 / 8 * h * sum

Those who are a bit paranoid about floating point arithmetics and exact zero results may replace the exit condition with something like Loop Until n+4711 = 4711. ;-)

Dieter

I like your FOR loop and the use of the SIGN function!!

Now we have a compact implementation that performs one function summation per iteration and with minimum calculations. Your pseudo-code version looks simple and very nice.
Find all posts by this user
Quote this message in a reply
12-13-2015, 07:32 PM
Post: #6
RE: Compact Simpson's 3/8 Rule(??)
It's quite common in Germany to use this for an arbitrary (random) number if you don't want to use letters. When this was used, you can continue with 4712 etc. Yes, we've got some special habits, too. Wink

d:-)
Find all posts by this user
Quote this message in a reply
12-13-2015, 07:45 PM
Post: #7
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 05:05 PM)Namir Wrote:  Your pseudo-code version looks simple and very nice.

Thank you. Actually it isn't pseudo-code but VBA – directly copied from a test version I tried in Excel. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
12-13-2015, 08:09 PM
Post: #8
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 04:39 PM)Dieter Wrote:  
(12-13-2015 04:05 PM)rprosperi Wrote:  OK, I just gotta ask - why 4711 ?

That's why. ;-)
Click on "history" and the year 1794 where the origin of this number is explained.
Dieter

I shall go and read that, though I imagined it was just a place-holder number to shift the round-off. But it just begged the question... and I suppose I rose to the bait!!

(12-13-2015 07:32 PM)walter b Wrote:  It's quite common in Germany to use this for an arbitrary (random) number if you don't want to use letters. When this was used, you can continue with 4712 etc. Yes, we've got some special habits, too. Wink

d:-)

Hopefully the referenced article explains the source of this 'magic' number. Typically, these explanations are nice stories, but not always the real truth behind how these things have come down though history. The real stories are probably much less interesting...

Thanks to you both!

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
12-14-2015, 08:13 PM
Post: #9
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 08:09 PM)rprosperi Wrote:  Hopefully the referenced article explains the source of this 'magic' number. Typically, these explanations are nice stories, but not always the real truth behind how these things have come down though history. The real stories are probably much less interesting...

Well, in this case the story is said to be true. ;-)

If you haven't read the referenced 4711 website yet, Wikipedia also knows the story behind this number in its respective arcticle. You'll find the story of one of the world's most traditional fragrances and the 4711 brand, especially for Eau de Cologne, which was named after the manufacturer's original location at Glockengasse 4711 in Cologne. The origins of this number again are explained in the section "house number 4711".

In Germany "4711" has a brand awareness of 80%, and I assume between 90 and 100% in the older generation. Over the decades this number has become a kind of synonym for any arbitrary number, which is the way it was used here.

Dieter
Find all posts by this user
Quote this message in a reply
12-14-2015, 08:44 PM
Post: #10
RE: Compact Simpson's 3/8 Rule(??)
(12-14-2015 08:13 PM)Dieter Wrote:  
(12-13-2015 08:09 PM)rprosperi Wrote:  Hopefully the referenced article explains the source of this 'magic' number. Typically, these explanations are nice stories, but not always the real truth behind how these things have come down though history. The real stories are probably much less interesting...

Well, in this case the story is said to be true. ;-)

If you haven't read the referenced 4711 website yet, Wikipedia also knows the story behind this number in its respective arcticle. You'll find the story of one of the world's most traditional fragrances and the 4711 brand, especially for Eau de Cologne, which was named after the manufacturer's original location at Glockengasse 4711 in Cologne. The origins of this number again are explained in the section "house number 4711".

In Germany "4711" has a brand awareness of 80%, and I assume between 90 and 100% in the older generation. Over the decades this number has become a kind of synonym for any arbitrary number, which is the way it was used here.

Dieter

Thanks for the additional comments. I did go the site you listed and read the history. Seems strange I've never heard of this brand of cologne; though hardly an expert or anything, I have spent many occasions having perfumes/colognes recommended and don't recall this one. Perhaps I liked other brands and only recall the ones I purchased.

Wikipedia has quite a detailed history! Astounding someone researched that so thoroughly.

Any idea how something so well recognized by almost all folks ended up becoming a synonym for an arbitrary number? Seems an odd evolution, though it is a rather odd-looking number.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
12-18-2015, 09:19 PM
Post: #11
RE: Compact Simpson's 3/8 Rule(??)
(12-14-2015 08:44 PM)rprosperi Wrote:  Any idea how something so well recognized by almost all folks ended up becoming a synonym for an arbitrary number? Seems an odd evolution, though it is a rather odd-looking number.

Since the brand is pronounced "47–11" it even sounds more odd in English than in German. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
12-18-2015, 11:58 PM
Post: #12
RE: Compact Simpson's 3/8 Rule(??)
(12-14-2015 08:44 PM)rprosperi Wrote:  Any idea how something so well recognized by almost all folks ended up becoming a synonym for an arbitrary number?

It's not the only magic number in use. Cf. 08/15.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
12-19-2015, 01:51 AM
Post: #13
RE: Compact Simpson's 3/8 Rule(??)
(12-18-2015 11:58 PM)Thomas Klemm Wrote:  
(12-14-2015 08:44 PM)rprosperi Wrote:  Any idea how something so well recognized by almost all folks ended up becoming a synonym for an arbitrary number?

It's not the only magic number in use. Cf. 08/15.

Cheers
Thomas

There doesn't spear to be an English version of this page. Oddly, when I saw your link, my thought was "the machine gun? - nah, can't be..." so I was a bit surprised upon seeing the gun when I go there.

Is "08/15" used to mean a derived or improved version, or something similar?

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
12-19-2015, 07:39 AM (This post was last modified: 12-19-2015 10:32 PM by walter b.)
Post: #14
RE: Compact Simpson's 3/8 Rule(??)
AFAIK without the net, it was the standard German rifle in WWI.

d:-/

Edit: Just checked: it was a "light machine gun" in WWI. Sorry for my ignorance.
Find all posts by this user
Quote this message in a reply
12-19-2015, 08:53 AM
Post: #15
RE: Compact Simpson's 3/8 Rule(??)
(12-19-2015 01:51 AM)rprosperi Wrote:  There doesn't spear to be an English version of this page. Oddly, when I saw your link, my thought was "the machine gun? - nah, can't be..." so I was a bit surprised upon seeing the gun when I go there.

That's exactly what this number originally referred to: it's the model number which stands for the year of construction (1908 resp. 1915).

(12-19-2015 01:51 AM)rprosperi Wrote:  Is "08/15" used to mean a derived or improved version, or something similar?

The "08/15" was a common piece of equipment with a rather modest reputation among its users. And this is what that number stands for today: if something is said to be "08/15" it is something not very sophisticated or special, but standard (or even below-standard) and widespread. As this is a calculator forum: think TI-30. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
12-19-2015, 05:24 PM
Post: #16
RE: Compact Simpson's 3/8 Rule(??)
(12-19-2015 08:53 AM)Dieter Wrote:  
(12-19-2015 01:51 AM)rprosperi Wrote:  There doesn't spear to be an English version of this page. Oddly, when I saw your link, my thought was "the machine gun? - nah, can't be..." so I was a bit surprised upon seeing the gun when I go there.

That's exactly what this number originally referred to: it's the model number which stands for the year of construction (1908 resp. 1915).

(12-19-2015 01:51 AM)rprosperi Wrote:  Is "08/15" used to mean a derived or improved version, or something similar?

The "08/15" was a common piece of equipment with a rather modest reputation among its users. And this is what that number stands for today: if something is said to be "08/15" it is something not very sophisticated or special, but standard (or even below-standard) and widespread. As this is a calculator forum: think TI-30. ;-)

Dieter

Thanks for clarifying Dieter. I've not heard this term before, but have plenty of reasons to use it now. Your example is a perfect one!

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
09-25-2019, 06:47 PM
Post: #17
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 05:05 PM)Namir Wrote:  
Code:
h = (b - a) / n
sum = f(a) + f(b)

For i = 1 To n - 1
  c = 2 + Sgn(i Mod 3)
  sum = sum + c * f(a + i * h)
Next

result = 3 / 8 * h * sum
I like your FOR loop and the use of the SIGN function!!

Now we have a compact implementation that performs one function summation per iteration and with minimum calculations. Your pseudo-code version looks simple and very nice.


Or maybe:

Code:
h = (b - a) / n
sum = f(a) + f(b)

For i = 1 To n - 3 Step 3
  sum = sum + 3 * f(a + i * h) + 3 * f(a + (i+1) * h) + 2 * f(a + (i+2) * h)
Next

result = 3 / 8 * h * sum

Csaba
Find all posts by this user
Quote this message in a reply
09-25-2019, 10:02 PM
Post: #18
RE: Compact Simpson's 3/8 Rule(??)
(12-13-2015 04:39 PM)Dieter Wrote:  
(12-13-2015 04:05 PM)rprosperi Wrote:  OK, I just gotta ask - why 4711 ?

That's why. ;-)
Click on "history" and the year 1794 where the origin of this number is explained.

I went there and still couldn't figure out the significance of that particular number. Why not 4712? 4709?

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
10-04-2019, 06:20 PM
Post: #19
RE: Compact Simpson's 3/8 Rule(??)
(09-25-2019 06:47 PM)Csaba Tizedes Wrote:  
Code:
h = (b - a) / n
sum = f(a) + f(b)

For i = 1 To n - 3 Step 3
  sum = sum + 3 * f(a + i * h) + 3 * f(a + (i+1) * h) + 2 * f(a + (i+2) * h)
Next

result = 3 / 8 * h * sum

I don't think this work.
We assumed n divisible by 3, and put a weight of 1 (3 3 2) ... (3 3 2) 3 3 1

However, above generate weight of 1 (3 3 2) ... (3 3 2) 0 0 1

You could also pull the (2*, 3*, 3*) from inside the loop, and scale it all in 1 step.
Code:
h = (b - a) / n
s2 = 0
s3 = f(a + h) + f(a + 2*h)
For i = 3 To n-3 Step 3
    s2 = s2 + f(a + i*h)
    s3 = s3 + f(a + (i+1)*h) + f(a + (i+2)*h)
Next i

result = 3/8 * h * (f(a) + f(b) + 2*s2 + 3*s3)
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)