Quote:Original post by Ezbez
I'm on Exercise 1.29 right now. It's for approximating integrals with Simpson's rule. However, I haven't learned about integrals in math yet, so I'd like an alternative problem to this, if possible, that's less mathy. [smile] Thanks in advanced.
You don't really have to know about integrals to be able to solve this exercise. I'll try to help you solving this problem: first of all, don't get discouraged by the ugly formula :)
We first handle the factor before it: we compute it, and store the result in a variable, like this:
(define (simpson f a b n) (define h [expression for h]) ...)
Next, we take on the big sum, which we know we'll have to compute as
(sum [term] [a] [next] )
The values for 'a', 'b' and 'next' are easy: there are (n+1) terms, numbered from 0 till n. 'term' is the harder part.
Each term has its own y_k value, so we can define a function for that:
(define (simpson f a b n) (define h [expression for h]) (define (y k) ...) (sum [term] [a] [next] ))
So, we still need 'term': the coefficients (i.e. the constants before each y_k) are: 1 4 2 4 2 4 ... 4 2 4 2 1, meaning for k = 0 and k = n, the coefficient equals 1, and in between they alternate between 4 and 2. We put this in a separate function:
(define (simpson f a b n) (define h [expression for h]) (define (y k) ...) (define (term k) (* (y k) (cond ((or (= k 0) (= k n)) 1) ((= (modulo k 2) 1) 4) (else 2)))) (sum term [a] [next] ))
And we're done. So, this leads to the solution:
(define (simpson f a b n) (define h (/ (- b a) n)) (define (y k) (f (+ a (* k h)))) (define (term k) (* (cond ((or (= k 0) (= k n)) 1) ((= (modulo k 2) 1) 4) (else 2)) (y k))) (define (1+ x) (+ 1 x)) (* (/ h 3) (sum term 0 1+ n)))
An alternative exercise (I see 1.29 as an exercise on using sum) which I remember from high school: you build a tower by stacking an infinite amount of cubes, whose sizes are 1, 1/2, 1/3, 1/4, 1/5, 1/6, ...
Three questions:
* is it possible to build another tower of finite height which fully contains the cube-tower?
* is it possible to paint the cube-tower with an infinite
finite amount of paint? (all 6 sides of the cubes are to be painted completly)
* is it possible to have a bag of finite size which contains all the cubes (but they don't need to be stacked as in the first question, so the difference with the first question is that here you compute the total volume, not the total height)
Guess first, and then compute the three sums. The goal at school was to prove our answers formally, but for me you can just compute the sums and see if there seems to be an upper limit or not (e.g. try a tower of height 10, then 100, then 1000, ... and see if the result keeps getting larger (going to infinity) or hits a limit).
And another one (this one is more like 1.30): define me a function taking three arguments 'init-amount', 'rate' and 'goal' and computes me the number of years it takes for 'init-amount' to grow to 'goal' if every year I get intrest at rate 'rate'.
E.g. if 'init-amount' is 100, 'rate' is 10, and 'goal' is 200, it will compute:
year 0: 100
year 1: 100 + 10% = 110
year 2: 110 + 10% = 121
year 3: 121 + 10% = 133
year 4: 133 + 10% = 146
year 5: 146 + 10% = 161
year 6: 161 + 10% = 177
year 7: 177 + 10% = 194
year 8: 194 + 10% = 214
so the function should return 8. It's possible to do this with a recursive process and an iterative process. Try both.
[Edited by - SamLowry on November 29, 2006 4:51:48 AM]