Skip to content

Instantly share code, notes, and snippets.

@sleibrock
Created October 11, 2016 21:48
Show Gist options
  • Save sleibrock/f2c7f9e96cfd3e336581e0ab0f7eea7a to your computer and use it in GitHub Desktop.
Save sleibrock/f2c7f9e96cfd3e336581e0ab0f7eea7a to your computer and use it in GitHub Desktop.
Area Under Curve / Volume of Solid of Revolution
#lang racket
;; Compute the area under a curve and the volume of the solid between L and R
;; first line is the bases, second is the exponents, third is the L and R limits
(define bases (map string->number (string-split (read-line) " ")))
(define expos (map string->number (string-split (read-line) " ")))
(define-values
(left right)
(apply values (map string->number (string-split (read-line) " "))))
(define (crunch x)
(define (ic x acc b e)
(if (empty? b)
acc
(ic x
(+ acc (* (car b) (expt x (car e))))
(cdr b)
(cdr e))))
(ic x 0 bases expos))
(define (riemman-sum l r f)
(define (is l r acc)
(if (>= l r)
acc
(is (+ l 0.001) r (+ acc (* 0.001 (f (crunch l)))))))
(is l r 0))
(displayln (real->decimal-string (riemman-sum left right (λ (x) x)) 1))
(displayln (real->decimal-string (riemman-sum left right (λ (x) (* pi (expt x 2)))) 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment