Post Reply 
Compact Simpson's 3/8 Rule(??)
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
Post Reply 


Messages In This Thread
Compact Simpson's 3/8 Rule(??) - Namir - 12-13-2015, 02:26 PM
RE: Compact Simpson's 3/8 Rule(??) - Namir - 12-13-2015 05:05 PM



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