156
6 Computing Integrals and Testing Code
error problems explained in Sect. 6.6.3, and we must use a test with tolerance
instead:
def test_add():
expected = 0.3
computed = add(0.1, 0.2)
tol = 1E-14
diff = abs(expected - computed)
assert diff < tol, ’diff={:g}’.format(diff)
Below we shall write test functions for each of the three test procedures we
suggested: comparison with hand calculations, checking problems that can be
exactly solved, and checking convergence rates. We stick to testing the trapezoidal
integration code and collect all test functions in one common file by the name
test_trapezoidal.py.
Hand-Computed Numerical Results Our previous hand calculations for two
trapezoids can be utilized in a test function like this:
from trapezoidal import trapezoidal
def test_trapezoidal_one_exact_result():
"""Compare one hand-computed result."""
from math import exp
v = lambda t: 3*(t**2)*exp(t**3)
n = 2
computed = trapezoidal(v, 0, 1, n)
expected = 2.463642041244344
error = abs(expected - computed)
tol = 1E-14
success = error < tol
msg = ’error={:g} > tol={:g}’.format(error, tol)
assert success, msg
Note the importance of checking computed against expected with a tolerance:
rounding errors from the arithmetics inside trapezoidal will not make the result
exactly like the hand-computed one.
Solving a Problem Without Numerical Errors We know that the trapezoidal rule
is exact for linear integrands. Choosing the integral
4.4
1.2 (6x −4)dx as a test case, the
corresponding test function could, for example, check with three different n values,
and may look like
def test_trapezoidal_linear():
"""Check that linear functions are integrated exactly."""
f = lambda x: 6*x - 4
F = lambda x: 3*x**2 - 4*x # Anti-derivative
a = 1.2; b = 4.4
expected = F(b) - F(a)
tol = 1E-14
for n in 2, 20, 21:
computed = trapezoidal(f, a, b, n)
error = abs(expected - computed)
success = error < tol
msg = ’n={:d}, err={:g}’.format(n, error)
assert success, msg
6 Computing Integrals and Testing Code
error problems explained in Sect. 6.6.3, and we must use a test with tolerance
instead:
def test_add():
expected = 0.3
computed = add(0.1, 0.2)
tol = 1E-14
diff = abs(expected - computed)
assert diff < tol, ’diff={:g}’.format(diff)
Below we shall write test functions for each of the three test procedures we
suggested: comparison with hand calculations, checking problems that can be
exactly solved, and checking convergence rates. We stick to testing the trapezoidal
integration code and collect all test functions in one common file by the name
test_trapezoidal.py.
Hand-Computed Numerical Results Our previous hand calculations for two
trapezoids can be utilized in a test function like this:
from trapezoidal import trapezoidal
def test_trapezoidal_one_exact_result():
"""Compare one hand-computed result."""
from math import exp
v = lambda t: 3*(t**2)*exp(t**3)
n = 2
computed = trapezoidal(v, 0, 1, n)
expected = 2.463642041244344
error = abs(expected - computed)
tol = 1E-14
success = error < tol
msg = ’error={:g} > tol={:g}’.format(error, tol)
assert success, msg
Note the importance of checking computed against expected with a tolerance:
rounding errors from the arithmetics inside trapezoidal will not make the result
exactly like the hand-computed one.
Solving a Problem Without Numerical Errors We know that the trapezoidal rule
is exact for linear integrands. Choosing the integral
4.4
1.2 (6x −4)dx as a test case, the
corresponding test function could, for example, check with three different n values,
and may look like
def test_trapezoidal_linear():
"""Check that linear functions are integrated exactly."""
f = lambda x: 6*x - 4
F = lambda x: 3*x**2 - 4*x # Anti-derivative
a = 1.2; b = 4.4
expected = F(b) - F(a)
tol = 1E-14
for n in 2, 20, 21:
computed = trapezoidal(f, a, b, n)
error = abs(expected - computed)
success = error < tol
msg = ’n={:d}, err={:g}’.format(n, error)
assert success, msg
