Blame


1 665c255d 2023-08-04 jrmu (defun cube (x)
2 665c255d 2023-08-04 jrmu (* x x x))
3 665c255d 2023-08-04 jrmu (defun sum (term a next b)
4 665c255d 2023-08-04 jrmu (if (> a b)
5 665c255d 2023-08-04 jrmu 0
6 665c255d 2023-08-04 jrmu (+ (funcall term a)
7 665c255d 2023-08-04 jrmu (sum term (funcall next a) next b))))
8 665c255d 2023-08-04 jrmu (defun sum-integers (a b)
9 665c255d 2023-08-04 jrmu (sum #'identity a #'1+ b))
10 665c255d 2023-08-04 jrmu (defun pi-sum (a b)
11 665c255d 2023-08-04 jrmu (defun pi-term (x)
12 665c255d 2023-08-04 jrmu (/ 1.0 (* x (+ x 2))))
13 665c255d 2023-08-04 jrmu (defun pi-next (x)
14 665c255d 2023-08-04 jrmu (+ x 4))
15 665c255d 2023-08-04 jrmu (sum #'pi-term a #'pi-next b))
16 665c255d 2023-08-04 jrmu (defun integral (f a b dx)
17 665c255d 2023-08-04 jrmu (defun add-dx (x)
18 665c255d 2023-08-04 jrmu (+ x dx))
19 665c255d 2023-08-04 jrmu (* (sum f (+ a (/ dx 2.0)) #'add-dx b) dx))
20 665c255d 2023-08-04 jrmu
21 665c255d 2023-08-04 jrmu (defun simpson-integral (f a b n)
22 665c255d 2023-08-04 jrmu (let ((h (float (/ (- b a) n))))
23 665c255d 2023-08-04 jrmu (defun simpson-term (k)
24 665c255d 2023-08-04 jrmu (* (funcall f (+ a (* k h)))
25 665c255d 2023-08-04 jrmu (cond ((or (= k 0) (= k n)) 1)
26 665c255d 2023-08-04 jrmu ((oddp k) 4)
27 665c255d 2023-08-04 jrmu (t 2))))
28 665c255d 2023-08-04 jrmu (* (/ h 3)
29 665c255d 2023-08-04 jrmu (sum #'simpson-term 0 #'1+ n))))