 Scheme mathematics eharetea

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
)
)

``` 