SICP Integral Procedure

102 Views Asked by At

In SICP section 3.5 the following procedure is given

(define (integral integrand initial-value dt)
  (define int
    (cons-stream 
     initial-value
     (add-streams (scale-stream integrand dt)
                  int)))
  int)

I understand how the procedure itself works but not how or why it finds the integral.

1

There are 1 best solutions below

0
Will Ness On

With some pseudocode mashup, it is

(integral ys z dt) = int
  where
  int = 
    [ z, ...[ dt * y + i | y <- ys
                         | i <- int ]
         ]
  -- i.e. --
  int[0] = z
  int[k+1] = int[k] + ys[k]*dt

;; or,
(integral [y, ...ys] z dt) =
    [z, ...(integral ys (z+y*dt) dt)]
(integral [] z dt) =
    []

which is a stream of partial sums following the definition of an integral, calculating an approximation of the area beneath the function's curve by the rectangle rule, under the assumption that the consecutive values of the function, ys, are taken at evenly spaced x values on the X axis at dt distances between them, i.e. we have

xs[k+1] - xs[k] = dt
ys[k] = foo(xs[k])

for the function foo(x), at any valid index k.

So we don't actually have the actual x coordinates, as we just assume they are evenly spaced in our representation of functions as streams of their y values.