Last active
December 18, 2015 04:09
-
-
Save aoeuidht/5723282 to your computer and use it in GitHub Desktop.
exericse 1.29
This file contains hidden or 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
(defun sum (term a next b) | |
(if (> a b) | |
0 | |
(+ (funcall term a) | |
(sum term (funcall next a) next b)))) | |
(defun cube (x) (* x x x)) | |
(defun integral (f a b dx) | |
(defun add-dx (x) (+ x dx)) | |
(* (sum f | |
(+ a (/ dx 2.0)) | |
#'add-dx | |
b) | |
dx)) | |
(integral #'cube 0 1 0.01) | |
(defun simp-integral (f a b n) | |
(let ((h (/ (- b a) n))) | |
(defun add-dx (x) (+ x (* 2 h))) | |
(defun simp-term (a) | |
(+ (funcall f a) | |
(* 4 (funcall f (+ a h))) | |
(funcall f (+ a (* 2 h))))) | |
(* (/ h 3.0) | |
(sum #'simp-term a #'add-dx (- b (* 2 h)))) | |
)) | |
(simp-integral #'cube 0 1 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment