This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Appendix C Numerical techniques Recursion (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 rule The definite integral of a function f ( x ) within an interval of integration [ a , b ] can be viewed as the area under the curve representing f ( x ) from the lower limit x = a to the upper limit x = b . In other words, we consider the graph of the curve for f ( x ) on the x , yplane, and find the area enclosed between that curve, the xaxis, and the ordinates of f ( x ) at x = a and x = b . [tyschemeZG1.gif] According to Simpson's rule, we divide the interval of integration [ a , b ] into n evenly spaced intervals, where n is even. (The larger n is, the better the approximation.) The interval boundaries constitute n + 1 points on the xaxis, viz, x , x 1 , ... , x i , x i +1 , ... , x n , where x = a and x n = b . The length of each interval is h = ( b a )/ n , so each x i = a + ih . We then calculate the ordinates of f ( x ) at the interval boundaries. There are n + 1 such ordinates, viz, y , ... , y i , ... , y n , where y i = f ( x i ) = f ( a + ih ). Simpson's rule approximates the definite integral of f ( x ) between a and b with the value 12 : [tyschemeZG2.gif] We define the procedure integratesimpson to take four arguments: the integrand f ; the xvalues at the limits a and b ; and the number of intervals n . (define integratesimpson (lambda (f a b n) ;... file:///C/Documents%20and%20Settings/Linda%20Grauer...tes/Teach%20Yourself%20Scheme/tyschemeZH22.html (1 of 9) [2/6/2008 11:43:05 AM] Teach Yourself Scheme in Fixnum Days The first thing we do in integratesimpson 's body is ensure that n is even  if it isn't, we simply bump its value by 1. ;... (unless (even? n) (set! n (+ n 1))) ;... Next, we put in the local variable h the length of the interval. We introduce two more local variables h*2 and n/2 to store the values of twice h and half n respectively, as we expect to use these values often in the ensuing calculations. ;... (let* ((h (/ ( b a) n)) (h*2 (* h 2)) (n/2 (/ n 2)) ;... We note that the sums y 1 + y 3 + ··· + y n 1 and y 2 + y 4 + ··· + y n 2 both involve adding every other ordinate. So let's define a local procedure sumeveryotherordinatestartingfrom that captures this common iteration. By abstracting this iteration into a procedure, we avoid having to repeat the iteration textually. This not only reduces clutter, but reduces the chance of error, since we have only one textual occurrence of the iteration to debug....
View
Full
Document
This test prep was uploaded on 02/06/2008 for the course CS 4536 taught by Professor Fisler during the Winter '08 term at WPI.
 Winter '08
 Fisler
 Recursion

Click to edit the document details