12-12-2015, 04:24 PM

Here is a (corrected, based on feedback from site members) pseudo code for what I hope to be the most compact way to implement Simpson's rule:

Many implementations use two summations--one for odd terms and one for even terms. In addition these implementations use two loops with slightly different ranges. The above pseudo-coode implements a simple version that uses one loop and a few simple computational tricks.

Namir

Code:

Given function f(x), integration interval [A, B] and N divisions, where N is even.

To calculate the integral of f(x) for X=A to x=B using Simpson's Rule

h=(B-A)/N

Sum=f(A)-f(B)

CHS=1

A=A+h

Do

Sum = Sum + (3+CHS)*f(A)

CHS=-CHS

A=A+h

N=N-1

Loop Until N=0

Area=h/3*Sum

Many implementations use two summations--one for odd terms and one for even terms. In addition these implementations use two loops with slightly different ranges. The above pseudo-coode implements a simple version that uses one loop and a few simple computational tricks.

Namir