computing integrals note the importance of checking err against exact with tolerancerounding errors from the arithmetics inside trapezoidal will not make the result exactly like the hand-computed one the size of the tolerance is here set to which is kind of all-round value for computations with numbers not deviating much from unity solving problem without numerical errors we know that the trapezoidal rule : is exact for linear integrands choosing the integral : /dx as test casethe corresponding test function for this unit test may look like def test_trapezoidal_linear()"""check that linear functions are integrated exactly "" lambda * lambda * ** * anti-derivative expected (bf(atol - for in computed trapezoidal(fabnerror abs(expected computedsuccess error tol msg ' =%derr=% (nerrorassert successmsg demonstrating correct convergence rates in the present example with integrationit is known that the approximation errors in the trapezoidal rule are proportional to being the number of subintervals used in the composite rule computing convergence rates requires somewhat more tedious programming than the previous testsbut can be applied to more general integrands the algorithm typically goes like for ni compute integral with ni intervals compute the error ei estimate ri from ( if the corresponding code may look like def convergence_rates(ffabnum_experiments= )from math import log from numpy import zeros expected (bf(an zeros(num_experimentsdtype=inte zeros(num_experimentsr zeros(num_experiments- for in range(num_experiments) [ **( + computed trapezoidal(fabn[ ] [iabs(expected computedif r_im log( [ ]/ [ - ])/log(float( [ ])/ [ - ] [ - float(' fr_im truncate to two decimals return