; integrate : (num -> num) num num num -> num ; Numerically integrates the given function between xlo and xhi. ; Approximation accuracy is based upon threshold -- the smaller the ; threshold, the more accurate the result. ; Assumes xlo < xhi. (define (integrate f xlo xhi threshold) (local [; avg2 : num num -> num ; Returns the average of the two inputs. (define (avg2 a b) (/ (+ a b) 2)) ; area : num num -> num ; Returns the approximate area under the function's curve ; between xlo and xhi. ; Assumes xlo < xhi. (define (area xlo xhi) ; Approximate by trapezoid method. (* (avg2 (f xlo) (f xhi)) (- xhi xlo))) ; integrate-help : num num -> num ; A specialized version of integrate that uses the ; original function and threshold. (define (integrate-help xlo xhi) (cond [(< (- xhi xlo) threshold) (area xlo xhi)] [else (local [(define xmid (/ (+ xhi xlo) 2))] (+ (integrate-help xlo xmid) (integrate-help xmid xhi)))]))] (integrate-help xlo xhi)))