-
Notifications
You must be signed in to change notification settings - Fork 0
/
e.2.13.rkt
101 lines (71 loc) · 2.78 KB
/
e.2.13.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#lang racket
(require (submod "e.2.07.rkt" export))
(require (submod "e.2.12.rkt" export))
;;; if the tolerance of x and y are small, they are negligible when
;;; divided with 100*100=1e4.
(define (percentage-tolerance-product x y)
(/ (+ (percent x) (percent y))
(+ 1 (/ (* (percent x) (percent y))
1e4))))
;; parallel resistor formula
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)))
;; same formula, numerator and denominator divided by R1R2
(define (par2 r1 r2)
(let ((one (make-interval 1 1)))
(div-interval one
(add-interval (div-interval one r1)
(div-interval one r2)))))
(module+ test
(define z1 (make-center-percent 10. 2.))
(define z2 (make-center-percent 30. 5.))
(percent (mul-interval z1 z2))
(percentage-tolerance-product z1 z2)
(str-center-percent-interval
(make-center-percent (center (mul-interval z1 z2))
(percentage-tolerance-product z1 z2))
10 10)
(str-center-percent-interval
(make-center-percent (center (mul-interval z1 z2))
(percent (mul-interval z1 z2)))
10 10)
(define (test-percent-interval-mul z1 z2)
(- (percent (mul-interval z1 z2))
(percentage-tolerance-product z1 z2)))
(test-percent-interval-mul z1 z2)
;; par2 returns an interval that is included in the interval
;; returned by par1.
(str-interval (par1 z1 z2) 10 5)
(str-interval (par2 z1 z2) 10 5)
(str-interval (par1 z1 z1) 10 5)
(str-interval (par2 z1 z1) 10 5)
(str-interval (par1 z2 z2) 10 5)
(str-interval (par2 z2 z2) 10 5))
(module+ export
(provide par1 par2))
;;; How to deduce the formula
;; We have the first interval X = [c1 - w1; c1 + w1]
;; We have the second interval Y = [c2 - w2; c2 + w2]
;; Because we suppose they are all positives, we have the product so:
;; [ (c1-w1)*(c2-w2) ; (c1+w1)*(c2+w2) ]
;; This has the center:
;; c = (2*c1*c2 + 2*w1*w2)/2 = c1c2 + w1w2
;; and the width is so:
;; w = (2*c1*w2 + 2*c2*w1)/2 = c1w2 + c2w1
;; The percent tolerance is so:
;; p = 100w/c = 100(c1w2 + c2w1) / (c1c2 + w1w2) =
;; (100 c1w2 + 100 c2w1) w2 w1
;; --------------------- ---- + ----
;; c1c2 c2 c1
;; = -------------------------- = 100 ----------------
;; w1w2 w1w2
;; 1+ -------- 1+ ------
;; c1c2 c1c2
;; t1 t2
;; ------ + ------
;; 100 100 t1+t2
;; = 100 --------------------- = ----------------
;; w1w2 t1 t2
;; 1+ -------- 1 + ---------
;; c1c2 100*100