Compact Simpson's 3/8 Rule(??)
12-13-2015, 03:38 PM (This post was last modified: 12-13-2015 03:50 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
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
 « Next Oldest | Next Newest »

 Messages In This Thread Compact Simpson's 3/8 Rule(??) - Namir - 12-13-2015, 02:26 PM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-13-2015 03:38 PM RE: Compact Simpson's 3/8 Rule(??) - rprosperi - 12-13-2015, 04:05 PM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-13-2015, 04:39 PM RE: Compact Simpson's 3/8 Rule(??) - toml_12953 - 09-25-2019, 10:02 PM RE: Compact Simpson's 3/8 Rule(??) - walter b - 12-13-2015, 07:32 PM RE: Compact Simpson's 3/8 Rule(??) - rprosperi - 12-13-2015, 08:09 PM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-14-2015, 08:13 PM RE: Compact Simpson's 3/8 Rule(??) - rprosperi - 12-14-2015, 08:44 PM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-18-2015, 09:19 PM RE: Compact Simpson's 3/8 Rule(??) - Namir - 12-13-2015, 05:05 PM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-13-2015, 07:45 PM RE: Compact Simpson's 3/8 Rule(??) - Csaba Tizedes - 09-25-2019, 06:47 PM RE: Compact Simpson's 3/8 Rule(??) - Thomas Klemm - 12-18-2015, 11:58 PM RE: Compact Simpson's 3/8 Rule(??) - rprosperi - 12-19-2015, 01:51 AM RE: Compact Simpson's 3/8 Rule(??) - walter b - 12-19-2015, 07:39 AM RE: Compact Simpson's 3/8 Rule(??) - Dieter - 12-19-2015, 08:53 AM RE: Compact Simpson's 3/8 Rule(??) - rprosperi - 12-19-2015, 05:24 PM RE: Compact Simpson's 3/8 Rule(??) - Albert Chan - 10-04-2019, 06:20 PM

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