computing integrals integral by the trapezoidal method numerical * ( * (bfor in range( )numerical + ( *dtnumerical *dt lambda texp( ** exact_value (bf(aerror abs(exact_value numericalrel_error (error/exact_value)* print ' =% ferror% (nnumericalerrorunfortunatelythe two other problems remain and they are fundamental : suppose you want to compute another integralsay dx how much do we need to change in the previous code to compute the new integralnot so muchthe formula for must be replaced by new formula the limits and the anti-derivative is not easily known and can be omittedand therefore we cannot write out the error the notation should be changed to be aligned with the new problemi and dt changed to and these changes are straightforward to implementbut they are scattered around in the programa fact that requires us to be very careful so we do not introduce new programming errors while we modify the code it is also very easy to forget to make required change with the previous code in trapezoidal pywe can compute the new integral : dx without touching the mathematical algorithm in an interactive session (or in programwe can just do from trapezoidal import trapezoidal from math import exp trapezoidal(lambda xexp(- ** )- when you now look back at the two solutionsthe flat special-purpose program and the function-based program with the general-purpose function trapezoidalyou hopefully realize that implementing general mathematical algorithm in general function requires somewhat more abstract thinkingbut the resulting code can be used over and over again essentiallyif you apply the flat special-purpose styleyou have to retest the implementation of the algorithm after every change of the program you cannot integrate by handbut this particular integral is appearing so often in so many contexts that the integral is special functioncalled the error function (wiki/error_functionand written erf xin codeyou can call erf(xthe erf function is found in the math module