## Sunday, July 11, 2010

### SICP Exercise 1.36: Fixed points and Average damping

From SICP section 1.3.3 Procedures as General Methods

Exercise 1.36 asks us to modify fixed-point so that it prints the sequence of approximations it generates. We can do that with the newline and display primitives we saw in exercise 1.22.

Next we're asked to find a solution to xx = 1000 by finding a fixed point of x → log(1000) / log(x). We can use Scheme's primitive log procedure to compute natural logarithms.

Finally, we need to compare the number of steps it takes to find the fixed point with and without average damping. We'll use the simple average procedure defined in the lecture notes.
(define (average x y)  (/ (+ x y) 2))

We'll start with the fixed-point procedure we used in the last exercise. The try sub-procedure looks like a good place to print each guess.
(define tolerance 0.00001)(define (fixed-point f first-guess)  (define (close-enough? v1 v2)    (< (abs (- v1 v2)) tolerance))  (define (try guess)    (display guess)    (newline)    (let ((next (f guess)))      (if (close-enough? guess next)          next          (try next))))  (try first-guess))

Now we can find the value of x for xx = 1000 using this procedure. The book warns us not to use 1.0 as a starting value or else the procedure will attempt to divide by log(1) = 0, so let's start with an initial guess of 2.0.
> (fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0)2.09.9657842846620873.0044722098412146.2791957575071573.7598507024015395.2158437849258954.1822071924013974.82776509834459064.3875933846626774.6712500857638994.4814036168950524.60536574609294.52308496787188654.5771146820473414.5413824801514544.5649032452308334.5493726793033424.5596064919132874.5528538757882714.5573055297482634.5543690644361814.5563053115329994.5550282635735544.5558703967028514.5553150011920794.55568126354332754.5554397157368464.5555990099982914.5554939575313894.5555632372928844.5555175484176514.5555476793063984.5555278085162544.5555409129179574.555532270803653

The procedure took 35 steps to arrive at an answer of 4.555532270803653 without using average damping.

In order to use average damping we just need to modify the input function to average the input value of x with the computed value.
> (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 2.0)2.05.98289214233104354.9221687213083434.6282243181954554.5683465131362424.55773059092370054.5559098090451314.5555994116106244.55554655214736754.555537551999825

This time it took only 10 steps to come up with approximately the same answer. We can check the two answers by simply raising each result to itself using Scheme's expt primitive.
> (expt 4.555532270803653 4.555532270803653)999.9913579312362> (expt 4.555537551999825 4.555537551999825)1000.0046472054871

So in this case, the procedure using average damping not only arrived at an solution in a fewer number of steps, but the final result happened to be slightly more accurate as well. That won't always be the case, but it's good to know that we're not sacrificing accuracy by using averaging damping.

Related:

For links to all of the SICP lecture notes and exercises that I've done so far, see The SICP Challenge.