Appendix C

Appendix C - Teach Yourself Scheme in Fixnum Days[Go to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 , y-plane, and find the area enclosed between that curve, the x-axis, and the ordinates of f ( x ) at x = a and x = b . [t-y-scheme-Z-G-1.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 x-axis, 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 : [t-y-scheme-Z-G-2.gif] We define the procedure integrate-simpson to take four arguments: the integrand f ; the x-values at the limits a and b ; and the number of intervals n . (define integrate-simpson (lambda (f a b n) ;... file:///C|/Documents%20and%20Settings/Linda%20Grauer...tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-22.html (1 of 9) [2/6/2008 11:43:05 AM] Teach Yourself Scheme in Fixnum Days The first thing we do in integrate-simpson '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 sum-every-other-ordinate-starting-from 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.

Page1 / 9

Appendix C - Teach Yourself Scheme in Fixnum Days[Go to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online