Simpson's Rule - Sample Implementation

Sample Implementation

An implementation of the composite Simpson's rule in Python:

def simpson(f, a, b, n): """f=function, a=initial value, b=end value, n=number of intervals of size h, n must be even""" h = float(b - a) / n S = f(a) for i in range(1, n, 2): x = a + h * i S += 4 * f(x) for i in range(2, n-1, 2): x = a + h * i S += 2 * f(x) S += f(b) F = h * S / 3 return F

Read more about this topic:  Simpson's Rule

Famous quotes containing the word sample:

    As a rule they will refuse even to sample a foreign dish, they regard such things as garlic and olive oil with disgust, life is unliveable to them unless they have tea and puddings.
    George Orwell (1903–1950)