Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname |23.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;! : N -> number
5 ;Computes the factorial of n.
7 (define (! n)
8 (cond
9 [(zero? n) 1]
10 [else (* n (! (sub1 n)))]))
12 ;series : (N[>=0] -> number) N[>=0] -> number
13 ;Given a-term and n, determine the sum of the first n-th terms in the sequence a-term.
15 (define (series a-term n)
16 (cond
17 [(zero? n) (a-term n)]
18 [else (+ (a-term n)
19 (series a-term (sub1 n)))]))
21 ;;e-taylor : N -> number
22 ;;Given i, find the i-th term of the Taylor series e^x = 1 + x + (x^2)/2! ...
24 ;e-power : number N -> number
25 ;e-power computes the x-th power of e by summing up n terms of the Taylor sequence.
27 (define (e-power x n)
28 (local ((define (e-taylor i)
29 (/ (expt x i) (! i))))
30 (series e-taylor n)))
32 #|
34 (define (inexact-e-power x n)
35 (exact->inexact (e-power x n)))
37 (inexact-e-power 1 1000)
38 (exp 1)
39 (- (inexact-e-power 1 100) (exp 1))
40 |#
42 ;ln-taylor : N -> number
43 ;Computes the n-th term of the taylor sequence for ln (x).
45 ;ln-x : number N -> number
46 ;Computes ln(x) by summing the first n-th terms of the Taylor sequence.
48 (define (ln-x x n)
49 (local ((define (ln-taylor i)
50 (* 2 (/ (expt (/ (- x 1)
51 (+ x 1))
52 (+ (* 2 i) 1))
53 (+ (* 2 i) 1)))))
54 (series ln-taylor n)))
56 ;sin-x : number N -> number
57 ;Determines sin(x) by summing the first n-th terms of the Taylor sequence.
59 ;sin-taylor : N -> number
60 ;Returns the i-th term of the Taylor sequence for sin(x).
62 (define (sin-x x n)
63 (local ((define (sin-taylor i)
64 (* (/ (expt x (+ (* 2 i) 1))
65 (! (+ (* 2 i) 1)))
66 (cond
67 [(even? i) 1]
68 [(odd? i) -1]))))
69 (series sin-taylor n)))
71 ;pi-greg : N -> number
72 ;Calculates pi by summing the first n-th terms of the Taylor sequence.
74 ;greg : N -> number
75 ;Given i, find the n-th term of the greg series.
77 (define (pi-greg n)
78 (local ((define (greg i)
79 (* 4 (* (/ 1
80 (+ (* 2 i)
81 1))
82 (cond
83 [(even? i) 1]
84 [(odd? i) -1])))))
85 (series greg n)))