6.2 The Composite Trapezoidal Rule
139
function. Thus, in the present case, we put the statements (otherwise placed a main
program) inside a function named application.
To achieve flexibility, we proceed to modify trapezoidal.py, so that it has a
test block and function definitions of trapezoidal and application:
def trapezoidal(f, a, b, n):
h = (b-a)/n
f_sum = 0
for i in range(1, n, 1):
x = a + i*h
f_sum = f_sum + f(x)
return h*(0.5*f(a) + f_sum + 0.5*f(b))
def application():
from math import exp
v = lambda t: 3*(t**2)*exp(t**3)
n = int(input(’n: ’))
numerical = trapezoidal(v, 0, 1, n)
# Compare with exact result
V = lambda t: exp(t**3)
exact = V(1) - V(0)
error = abs(exact - numerical)
print(’n={:d}: {:.16f}, error: {:g}’.format(n, numerical, error))
if __name__ == ’__main__’:
application()
With our newly gained knowledge about the making of modules, we understand
that the if test becomes true when the module file, trapezoidal.py, is run as a
program, and false when the module (or part of it) is imported in another program.
Consequently, with an import like from trapezoidal import trapezoidal,
the test fails and application() is not called. On the other hand, if we run
trapezoidal.py as a program, the test condition is positive and application()
is called. A call to application implies that our special problem gets computed.
The main program now gets very small, being just a single function call to
application.
Running the program, e.g., with n = 4 gives the output
n=4: 1.9227167504675762, error: 0.204435
Clearly, with a module like the one shown here, the trapezoidal function alone
(i.e., without application) can easily be imported by other programs to compute
other integrals.
6.2.3 A Specific Implementation: What’s the Problem?
Let us illustrate the implementation implied by alternative 1 in the Programmer’s
dilemma box in Sect. 6.2.2. That is, we make a special-purpose code, where we
adapt the general formula (6.17) to the specific problem
1
0 3t 2 e t 3 dt, in which the
integrand is a velocity function v(t).
139
function. Thus, in the present case, we put the statements (otherwise placed a main
program) inside a function named application.
To achieve flexibility, we proceed to modify trapezoidal.py, so that it has a
test block and function definitions of trapezoidal and application:
def trapezoidal(f, a, b, n):
h = (b-a)/n
f_sum = 0
for i in range(1, n, 1):
x = a + i*h
f_sum = f_sum + f(x)
return h*(0.5*f(a) + f_sum + 0.5*f(b))
def application():
from math import exp
v = lambda t: 3*(t**2)*exp(t**3)
n = int(input(’n: ’))
numerical = trapezoidal(v, 0, 1, n)
# Compare with exact result
V = lambda t: exp(t**3)
exact = V(1) - V(0)
error = abs(exact - numerical)
print(’n={:d}: {:.16f}, error: {:g}’.format(n, numerical, error))
if __name__ == ’__main__’:
application()
With our newly gained knowledge about the making of modules, we understand
that the if test becomes true when the module file, trapezoidal.py, is run as a
program, and false when the module (or part of it) is imported in another program.
Consequently, with an import like from trapezoidal import trapezoidal,
the test fails and application() is not called. On the other hand, if we run
trapezoidal.py as a program, the test condition is positive and application()
is called. A call to application implies that our special problem gets computed.
The main program now gets very small, being just a single function call to
application.
Running the program, e.g., with n = 4 gives the output
n=4: 1.9227167504675762, error: 0.204435
Clearly, with a module like the one shown here, the trapezoidal function alone
(i.e., without application) can easily be imported by other programs to compute
other integrals.
6.2.3 A Specific Implementation: What’s the Problem?
Let us illustrate the implementation implied by alternative 1 in the Programmer’s
dilemma box in Sect. 6.2.2. That is, we make a special-purpose code, where we
adapt the general formula (6.17) to the specific problem
1
0 3t 2 e t 3 dt, in which the
integrand is a velocity function v(t).
