;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #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"))))) ;integrate-kepler : (number -> number) number number ;Integrate f from left to right according to kepler's rule (split the curve into two trapezoids and evaluate the area of the trapezoids). (define (integrate-kepler f left right) (local ((define average (/ (- right left) 2)) (define midpoint (+ left average))) (+ (area-of-trapezoid average (f left) (f midpoint)) (area-of-trapezoid average (f midpoint) (f right))))) ;area-of-trapezoid : number number number -> number ;Given the width, height1, and height2, compute the area of the trapezoid according to the formula A = width * (1/2) * (height1 + height2). (define (area-of-trapezoid width height1 height2) (* width 1/2 (+ height1 height2))) #| (define (y=x x) x) (define (y=x2 x) (sqr x)) (define (y=x3 x) (expt x 3)) |# ;series : (N[>=0] -> number) N[>=0] -> number ;Given a-term and n, determine the sum of the first n-th terms in the sequence a-term. (define (series a-term n) (cond [(zero? n) (a-term n)] [else (+ (a-term n) (series a-term (sub1 n)))])) ;integrate : (number -> number) number number number ;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. ;area-of-rectangle : N -> number ;Find the area of the i-th rectangle, with the first rectangle given an index of 0. ;midpoint : N -> number ;Find the midpoint of the i-th rectangle, where the first rectangle has an index of 0. (define (integrate f left right R) (local ((define width (/ (- right left) R)) (define step (/ width 2)) (define (area-of-rectangle i) (* width (f (midpoint i)))) (define (midpoint i) (cond [(zero? i) (+ left step)] [else (+ width (midpoint (sub1 i)))]))) (series area-of-rectangle (- R 1)))) (define (y1 x) (+ x 4)) (define (y2 x) (- 4 x)) (define (y3 x) (+ x 10)) (define (y4 x) (- 10 x)) (define (y5 x) 12) (define listofeqns (list y1 y2 y3 y4 y5)) (define listofcolors '(blue red black green orange purple)) ;binarymap : (X Y -> Z) (listof X) (listof Y) -> (listof Z) ;Maps alox and aloy simultaneously, together, to aloz using operator. (define (binarymap operator alox aloy) (cond [(empty? alox) empty] [else (cons (operator (first alox) (first aloy)) (binarymap operator (rest alox) (rest aloy)))])) ;(binarymap graph-line listofeqns listofcolors) ;line-from-point+slope : posn number -> (number -> number) ;Given point and slope, return a function that represents the desired line. (define (line-from-point+slope point slope) (local ((define (f x) (+ (* slope (- x (posn-x point))) (posn-y point)))) f)) (define y=x+4 (line-from-point+slope (make-posn 0 4) 1)) #| (graph-line y=x+4 'green) (graph-line y1 'black) |# (define (y0 x) (+ (sqr x) (* -4 x) 7)) ;secant-line : (number -> number) number number -> (number -> number) ;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). (define (secant-line f e x) (local ((define a-slope (/ (- (f (+ x e)) (f (- x e))) (* 2 e))) (define a-point (make-posn (- x e) (f (- x e))))) (line-from-point+slope a-point a-slope))) ;(graph-fun y0 'purple) ;(graph-line (secant-line y0 0.5 3) 'green) #| ;d/dx : (number -> number) -> (number -> number) ;Returns f', the slope of f. (define (d/dx f) (local ((define (slope x) (/ (- (f (+ x e)) (f (- x e))) (* 2 e))) (define e 0.01)) slope)) |# ;d/dx : (number -> number) number -> (number -> number) ;Returns f', the slope of f, given epsilon e. (define (d/dx f e) (local ((define (slope x) (/ (- (f (+ x e)) (f (- x e))) (* 2 e)))) slope)) ;line-from-two-points : posn posn -> (number -> number) ;Given p1 and p2, return a function that represents the line. (define (line-from-two-points p1 p2) (cond [(zero? (- (posn-x p2) (posn-x p1))) (error 'line-from-two-points "vertical line")] [else (local ((define slope (/ (- (posn-y p2) (posn-y p1)) (- (posn-x p2) (posn-x p1)))) (define a-line (line-from-point+slope p1 slope)) (define (f x) (a-line x))) f)])) (define y6 (line-from-two-points (make-posn 2 3) (make-posn 2.3 8))) (graph-line y6 'black) (define (y7 x) (+ (* 1/60 (* x x x)) (* -1/10 (* x x)) 5)) (define a-slope (d/dx y7 2)) (define b-slope (d/dx y7 1)) (define c-slope (d/dx y7 0.5)) (list (a-slope 4) (b-slope 4) (c-slope 4))