How to solve the following equation using accumulate (Scheme)
I'm trying to do the following problem (there is a formula so I print-screened and uploaded it)
Formula http://i开发者_如何转开发mg248.imageshack.us/img248/6558/problemh.jpg
(http://img248.imageshack.us/img248/6558/problemh.jpg)Using accumulate:
(define (accumulate combiner null-value term a next b)
(if (> a b) null-value
(combiner (term a) (accumulate combiner null-value term (next a) next b))))
but I have pretty much no idea how to do it. It should look something like
(define (sum-func f k)...?
I'm not sure how I should define f(x+ j*h) so that to put the changing number j (changes from 0 to the given number k) inside of it... In other words, I'm pretty lost.
Thank you for the fast respond. I'm studying for my midterms and I'm trying to do the midterms from the previous years, but I just got stuck here. I know how to use the accumulate and everything in it, but I just don't know how to do the f(x + jh). I'm trying this:
(define (sum-func term a b h x)
(define (next a) (+ a 1))
(define (term a) (term (+ x (* h a))))
(accumulate + 0 (term a) a (next a) b))
But it's not working...
In other words I don't know how to use the 'next' (changing part) inside the term..
(sorry if I'm explaining very clearly - English is not my native)
I am not sure if this is a homework problem or not but in any case I will provide you with some ideas on how to solve it. For the sake of simplicity let us assume our problem is to find the sum of squares of all numbers between j and k. You can use your accumulate function above to solve this simple problem. All you will have to do is define your combiner, term and next functions for this problem along with defining the final null-value.
Term function here should calculate the squares for each j. Therefore term will take j as a argument & will return the square of j as the result. (define (term j) (sqr j))
Next function should get the next j in the sequence. (define (next j) (+ j 1)
Combiner function should combine two terms together. (define (combiner t1 t2) (+ t1 t2))
Finally, null-value signifies the stop condition, the last value that should be passed to combiner when we have accumulated all the values from j to k. All we have to do is just define it as a zero in this case. (define null-value 0)
For your problem, most of these functions are same except for the term function. In this problem, the term function was a very simple one, it just found the square of the supplied number. However, in your case, it is not as simple as finding the square of j since you have several other constants defined.
Hope this will help you solve the problem.
If you need even more insight into this, you may want to look at the accumulate problems in SICP chapter 2.
I figured it out. I guess I was overthinking the algorythm (thinking it was much more complex than it was) and this led me to many stupid mistakes.
(define (accumulate combiner null-value term a next b)
(if (> a b) null-value
(combiner
(term a)
(accumulate combiner null-value term (next a) next b))))
(define (sum-row term x h n)
(accumulate + 0
(lambda (t) (* (if (even? (- n t)) 1 -1)(term (+ x(* h t)))))
0
(lambda (t) (+ t 1))
n))
(define (square x) (* x x))
(sum-row square 2 2 3)
; -4+16-36+64=40
; -f(2+0*2)=-4 f(2+1*2)=16 -f(2+2*2)=-36 f(2+3*6)=64
精彩评论