Advertisement
skbtwiz

Untitled

May 21st, 2018
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 0.97 KB | None | 0 0
  1. ;; (list-of-areas F xmin xmax nsteps) produces the list of areas of rectangeles
  2. ;;    under the graph of the Function F
  3. ;; list-of-areas : Function Num Num Nat -> (listof Num)
  4. ;; requires : xmin less than or equal to xmax
  5. ;; Example :
  6. (check-expect (list-of-areas (lambda (x) (+ (* x x) 1)) 2 4 4)
  7.               (list 2.5 3.625 5 6.625))
  8.  
  9. (define (list-of-areas F xmin xmax nsteps)
  10.   (map (lambda (x) (* x (/ (- xmax xmin) nsteps)))
  11.        (map F (range xmin xmax (/ (- xmax xmin) nsteps)))))
  12.  
  13. ;; (riemann-sum F xmin xmax nsteps) finds the area under the graph of Function F
  14. ;;    using Riemann Sums with nsteps steps between xmin and xmax
  15. ;; riemann-sums : Function Num Num Nat -> Num
  16. ;; requires : xmin less than or equal to xmax
  17. ;; Example :
  18. (check-expect (riemann-sum (lambda (x) 3) 1 5 100) 12)
  19.  
  20. (define (riemann-sum F xmin xmax nsteps)
  21.   (foldr + 0 (list-of-areas F xmin xmax nsteps)))
  22.  
  23. ;; Tests :
  24. (check-expect (riemann-sum (lambda (x) (+ (* x x) 1)) 2 4 4) 17.75)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement