Using scheme to compute derivatives and integrals.
Scheme, a rendition of Lisp, is a great language used for numerous applications, one being artificial intelligence.
Date Created:Saturday September 06th, 2008 10:37 AM
Date Modified:Friday October 31st, 2008 10:03 PM
(define average (lambda (x y) (/ (+ x y))))
(define (average-damp f)
(lambda (x) (average x (f x)) )
)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) 0.00001)
)
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next)
)
)
)
(try first-guess)
)
(define (deriv g dx) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))
(define (newton-transform g dx) (lambda (x) (- x (/ (g x) ((deriv g dx) x)))))
(define (newtons-method g guess dx) (fixed-point (newton-transform g dx) guess))
(define pi (newtons-method sin 3.14 0.0001) )
(define (cubic a b c)
(lambda (x) (+ (* x x x) (* a x x) (* b x) c) )
)
(define test-cubic (newtons-method (cubic 1 2 4) 1 0.001))
(define summation
(lambda (func inc x b)
(if (> x b)
0
(+
(func x)
(summation func inc (inc x) b)
)
)
)
)
(define dx (lambda (x) (+ x 1) ) )
(define identity (lambda (x) x) )
(define sum
(lambda (func x b dx)
(define inc-dx (lambda (x dx) (+ x dx) ) )
(if (> x b)
0
(+
(func x)
(sum func (inc-dx x dx) b dx)
)
)
)
)
(define product
(lambda (func x b dx)
(define inc-dx (lambda (x dx) (+ x dx) ) )
(if (> x b)
1
(*
(func x)
(product func (inc-dx x dx) b dx)
)
)
)
)
(define (factorial x)
(product identity 1 x 1)
)
(define (integral f a b dx)
(*
(sum f (+ a (/ dx 2.0)) b dx)
dx
)
)
Downloads:
Download: newtons.scm 765 B
Download: summation.scm 731 B
Download: accumulate-filter.scm 871 B
Download: accumulate.scm 374 B
Download: double.scm 135 B
Download: every.scm 328 B
Download: factorial.scm 246 B
Download: pi.scm 380 B
Download: product.scm 192 B
Download: repeat-compose.scm 247 B
Please login or Click Here to register for downloads
Scheme mathematics by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
