Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.45 KB | None | 0 0
  1. (define (compose f g)
  2.   (lambda (x) (f(g x))))
  3.  
  4. (define (derive f dx)
  5.   (lambda (x)
  6.     (/(-(f(+ x dx)) (f x)) dx)))
  7.  
  8. (define (square x) (* x x))
  9. (define (cube x) (* x x x))
  10.  
  11. (define (repeated f N)
  12.   (cond ((= n 0) (lambda(x) x))
  13.         ((= n 1) f)
  14.         (else (compose f
  15.                        (repeated f (- N 1))))))
  16.  
  17. (define (id1 x) 1)
  18. (define (square1 x)(* 2 x))
  19. (define (qube1 x) (* 3 x x))
  20.  
  21.  
  22.  
  23.  
  24.  
  25. (define (compare f g x)
  26.   (display "(f x) = ")
  27.   (display (f x))
  28.   (display " (g x)")
  29.   (display (g x))
  30.   (display " compare: ")
  31.   (display (= (f x) (g x)))
  32.   (display " difference: ")
  33.   (display (- (f x)(g x)))
  34.   (display "\n"))
  35.  
  36.  
  37. (define (compare1 f g x)
  38.   (let (
  39.         (valueF (f x))
  40.         (valueG (g x))
  41.         )
  42.     (diaplay " (f x) = ")(display (valueF))
  43.     (diaplay " (g x) = ")(display (valueG))
  44.     (display " compare: ")(display (= valueF valueG))
  45.     (display " difference: ")(display (- valueF valueG))))
  46.  
  47.  
  48. (define (cube2 x) (* 6 x))
  49. (define (cube3 x) 6)
  50. (define (id x) x)
  51.  
  52. (define (compare-interval f g a b)
  53.   (if (> a b) (display "End.\n")
  54.       (begin (compare f g a)
  55.              (compare-interval f g (+ a 1) b))))
  56.  
  57. (display "Comparing id and id1:\n")
  58.  
  59. (compare-interval (derive id 0.00001) id1 1 10)
  60.  
  61. (compare (derive square 0.00001) square1 1.99999999999)
  62.  
  63. (define (deriveN f N dx)
  64.     ((repeated (lambda(f) derive f dx) N) f)
  65.   )
  66.  
  67. (compare-interval (deriveN cube 2 0.00001) cube2 1 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement