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.4|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp") (lib "graphing.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") (lib "graphing.ss" "teachpack" "htdp")))))
4 ;integrate-kepler : (number -> number) number number
5 ;Integrate f from left to right according to kepler's rule (split the curve into two trapezoids and evaluate the area of the trapezoids).
7 (define (integrate-kepler f left right)
8 (local ((define average (/ (- right left) 2))
9 (define midpoint (+ left average)))
10 (+ (area-of-trapezoid average (f left) (f midpoint))
11 (area-of-trapezoid average (f midpoint) (f right)))))
13 ;area-of-trapezoid : number number number -> number
14 ;Given the width, height1, and height2, compute the area of the trapezoid according to the formula A = width * (1/2) * (height1 + height2).
15 (define (area-of-trapezoid width height1 height2)
16 (* width 1/2 (+ height1 height2)))
18 #|
19 (define (y=x x)
20 x)
21 (define (y=x2 x)
22 (sqr x))
23 (define (y=x3 x)
24 (expt x 3))
25 |#
27 ;series : (N[>=0] -> number) N[>=0] -> number
28 ;Given a-term and n, determine the sum of the first n-th terms in the sequence a-term.
30 (define (series a-term n)
31 (cond
32 [(zero? n) (a-term n)]
33 [else (+ (a-term n)
34 (series a-term (sub1 n)))]))
36 ;integrate : (number -> number) number number number
37 ;Divide the interval between left and right into R rectangles and approximate the area under the curve by the sum of the area of the rectangles with their height taken at the midpoint. integrate sums the area of the rectangles, with the first rectangle given an index of 0, and the last, R-1.
39 ;area-of-rectangle : N -> number
40 ;Find the area of the i-th rectangle, with the first rectangle given an index of 0.
42 ;midpoint : N -> number
43 ;Find the midpoint of the i-th rectangle, where the first rectangle has an index of 0.
45 (define (integrate f left right R)
46 (local ((define width (/ (- right left) R))
47 (define step (/ width 2))
48 (define (area-of-rectangle i)
49 (* width (f (midpoint i))))
50 (define (midpoint i)
51 (cond
52 [(zero? i) (+ left step)]
53 [else (+ width (midpoint (sub1 i)))])))
54 (series area-of-rectangle (- R 1))))
56 (define (y1 x) (+ x 4))
57 (define (y2 x) (- 4 x))
58 (define (y3 x) (+ x 10))
59 (define (y4 x) (- 10 x))
60 (define (y5 x) 12)
62 (define listofeqns (list y1
63 y2
64 y3
65 y4
66 y5))
67 (define listofcolors '(blue
68 red
69 black
70 green
71 orange
72 purple))
74 ;binarymap : (X Y -> Z) (listof X) (listof Y) -> (listof Z)
75 ;Maps alox and aloy simultaneously, together, to aloz using operator.
76 (define (binarymap operator alox aloy)
77 (cond
78 [(empty? alox) empty]
79 [else (cons (operator (first alox) (first aloy))
80 (binarymap operator (rest alox) (rest aloy)))]))
82 ;(binarymap graph-line listofeqns listofcolors)
84 ;line-from-point+slope : posn number -> (number -> number)
85 ;Given point and slope, return a function that represents the desired line.
87 (define (line-from-point+slope point slope)
88 (local ((define (f x)
89 (+ (* slope
90 (- x (posn-x point)))
91 (posn-y point))))
92 f))
94 (define y=x+4 (line-from-point+slope (make-posn 0 4) 1))
96 #|
97 (graph-line y=x+4 'green)
98 (graph-line y1 'black)
99 |#
101 (define (y0 x)
102 (+ (sqr x) (* -4 x) 7))
104 ;secant-line : (number -> number) number number -> (number -> number)
105 ;Given f, e, and x, returns the function that corresponds to the line that passes through (x-e, f(x-e)) and (x+e, f(x+e)) (ie, the secant line).
107 (define (secant-line f e x)
108 (local ((define a-slope (/ (- (f (+ x e))
109 (f (- x e)))
110 (* 2 e)))
111 (define a-point (make-posn (- x e)
112 (f (- x e)))))
113 (line-from-point+slope a-point a-slope)))
115 ;(graph-fun y0 'purple)
116 ;(graph-line (secant-line y0 0.5 3) 'green)
118 #|
120 ;d/dx : (number -> number) -> (number -> number)
121 ;Returns f', the slope of f.
123 (define (d/dx f)
124 (local ((define (slope x) (/ (- (f (+ x e))
125 (f (- x e)))
126 (* 2 e)))
127 (define e 0.01))
128 slope))
129 |#
131 ;d/dx : (number -> number) number -> (number -> number)
132 ;Returns f', the slope of f, given epsilon e.
134 (define (d/dx f e)
135 (local ((define (slope x) (/ (- (f (+ x e))
136 (f (- x e)))
137 (* 2 e))))
138 slope))
141 ;line-from-two-points : posn posn -> (number -> number)
142 ;Given p1 and p2, return a function that represents the line.
144 (define (line-from-two-points p1 p2)
145 (cond
146 [(zero? (- (posn-x p2) (posn-x p1))) (error 'line-from-two-points "vertical line")]
147 [else (local ((define slope (/ (- (posn-y p2) (posn-y p1))
148 (- (posn-x p2) (posn-x p1))))
149 (define a-line (line-from-point+slope p1 slope))
150 (define (f x)
151 (a-line x)))
152 f)]))
154 (define y6 (line-from-two-points (make-posn 2 3)
155 (make-posn 2.3 8)))
157 (graph-line y6 'black)
159 (define (y7 x)
160 (+ (* 1/60 (* x x x))
161 (* -1/10 (* x x))
162 5))
164 (define a-slope (d/dx y7 2))
165 (define b-slope (d/dx y7 1))
166 (define c-slope (d/dx y7 0.5))
167 (list (a-slope 4)
168 (b-slope 4)
169 (c-slope 4))