Created
October 11, 2016 21:48
-
-
Save sleibrock/f2c7f9e96cfd3e336581e0ab0f7eea7a to your computer and use it in GitHub Desktop.
Area Under Curve / Volume of Solid of Revolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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