This preview shows pages 1–3. Sign up to view the full content.
Teach Yourself Scheme in Fixnum Days [Go to first, previous, next page; contents; index]Appendix CNumerical techniquesRecursion (including iteration) combines well with Scheme's mathematical primitive procedures to implement various numerical techniques. As an example, let's implement Simpson's rule, a procedure for finding an approximation for a definite integral. C.1 Simpson's ruleThe definite integral of a function f(x) within an interval of integration [a,b] can be viewed as the area under the curverepresenting f(x) from the lower limit x= ato the upper limit x= b. In other words, we consider the graph of the curve for f(x) on the x,y-plane, and find the area enclosed between that curve, the x-axis, and the ordinatesof f(x) at x= aand x= b. [t-y-scheme-Z-G-1.gif]According to Simpson's rule, we divide the interval of integration [a,b] into nevenly spaced intervals, where nis even. (The larger nis, the better the approximation.) The interval boundaries constitute n+ 1 points on the x-axis, viz, x0, x1, ..., xi, xi+1, ..., xn, where x0= aand xn= b. The length of each interval is h= (b-a)/n, so each xi= a+ ih. We then calculate the ordinates of f(x) at the interval boundaries. There are n+ 1 such ordinates, viz, y0, ..., yi, ..., yn, where yi= f(xi) = f(a+ ih). Simpson's rule approximates the definite integral of f(x) between aand bwith the value12:[t-y-scheme-Z-G-2.gif]We define the procedure integrate-simpsonto take four arguments: the integrand f; the x-values at the limits aand b; and the number of intervals n. (define integrate-simpson(lambda (f a b n);... (1 of 9) [2/6/2008 11:43:05 AM]
has intentionally blurred sections.
Sign up to view the full version.