当前位置: 首页 > news >正文

sicp每日一题[2.10]

Exercise 2.11

In passing, Ben also cryptically comments: “By testing the signs of the endpoints of the intervals, it is possible to break m u l − i n t e r v a l mul-interval mulinterval into nine cases, only one of which requires more than two multiplications.” Rewrite this procedure using Ben’s suggestion.
After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as 3.5±0.15 rather than [3.35, 3.65]. Alyssa returns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors:

(define (make-center-width c w)(make-interval (- c w) (+ c w)))
(define (center i)(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)(/ (- (upper-bound i) (lower-bound i)) 2))

Unfortunately, most of Alyssa’s users are engineers. Real engineering situations usually involve measurements with only a small uncertainty, measured as the ratio of the width of the interval to the midpoint of the interval. Engineers usually specify percentage tolerances on the parameters of devices, as in the resistor specifications given earlier.


这道题目难度并不大,就是有点麻烦,要把每一种情况的上界和下界都分析出来。至于区间采用置信区间的表示方法,只要用宽度的一半除以中间值就行了。

; 把 w 改为百分比形式
(define (make-center-width center percent)(let ((tolerance (* center (/ percent 100))))(make-interval (- center tolerance) (+ center tolerance))))(define (center i)(average (lower-bound i) (upper-bound i)))(define (percent i)(* (/ (/ (- (upper-bound i) (lower-bound i)) 2)(center i))100))(define (display-interval-tolerance i) (newline)(display (center i))(display " ± ")(display (percent i))(display "%")); 记 x 的下界为 x1,上界为 x2;记 y 的下界为 y1,上界为 y2
; 将他们按 x1 x2 y1 y2 从左到右排序,并按位置记为 1, 2, 3, 4
; 则按照 x 和 y 的区间端点数字的符号,可以分成以下9种情况:
; - - - -   min: 2*4, max: 1*3
; - - - +   min: 1*4, max: 1*3
; - - + +   min: 1*4, max: 2*3
; - + - -   min: 2*3, max: 1*3
; - + - +   min: min(1*4, 2*3), max: max(1*3, 2*4)
; - + + +   min: 1*4, max: 2*4
; + + - -   min: 2*3, max: 1*4
; + + - +   min: 2*3, max: 2*4
; + + + +   min: 1*3, max: 2*4
(define (mul-interval x y)(let ((x1 (lower-bound x))(x2 (upper-bound x))(y1 (lower-bound y))(y2 (upper-bound y))(p1 (* (lower-bound x) (lower-bound y)))    ; 即 1*3(p2 (* (lower-bound x) (upper-bound y)))    ; 即 1*4(p3 (* (upper-bound x) (lower-bound y)))    ; 即 2*3(p4 (* (upper-bound x) (upper-bound y))))   ; 即 2*4(cond ((and (negative? x2) (negative? y2)) (make-interval p4 p1))((and (negative? x2) (negative? y1) (positive? y2)) (make-interval p2 p1))((and (negative? x2) (positive? y1)) (make-interval p2 p3))((and (negative? x1) (positive? x2) (negative? y2)) (make-interval p3 p1))((and (negative? x1) (positive? x2) (negative? y1) (positive? y2)) (make-interval (min p2 p3) (max p1 p4)))((and (negative? x1) (positive? x2) (positive? y1)) (make-interval p2 p4))((and (positive? x1) (negative? y2)) (make-interval p3 p2))((and (positive? x1) (negative? y1) (positive? y2)) (make-interval p3 p4))((and (positive? x1) (positive? y1)) (make-interval p1 p4)))))(define a (make-interval -6.12 7.48))
(define b (make-interval -4.465 4.935))
(display-interval (mul-interval a b))
(display-interval-tolerance (mul-interval a b)); 执行结果
[-33.3982, 36.9138]
1.7577999999999996 ± 2000.0000000000007%

http://www.mrgr.cn/news/27358.html

相关文章:

  • makefile的介绍与使用
  • C2000 F280049 Timer模块开发
  • Windows 的 docker 删除容器后 WSL2 磁盘空间不释放的问题
  • Java 21的Enhanced Deprecation的笔记
  • nacos和eureka的区别
  • 数据集 wider person 户外密集行人检测 >> DataBall
  • node.js精品课程网站—计算机毕业设计源码27724
  • JNI编程实现原理
  • 三维点云处理(C++)学习记录——PDAL
  • 技术周总结 09.09~09.15周日(C# WinForm WPF)
  • 使用Flux以文生图
  • [产品管理-16]:NPDP新产品开发 - 14 - 产品创新流程 - 产品创新流程模型比较:门径、IPD、精益生产、敏捷、系统工程、设计思维、精益创业
  • Java 全面指南:从入门到精通
  • PMP--一模--解题--61-70
  • re题(19)BUUCTF[ACTF新生赛2020]easyre
  • 【MySQL-初级】mysql基础操作(账户、数据库、表的增删查改)
  • 【C#生态园】选择最适合你的工具:C# GUI库完整比较及指南
  • 大模型大侦探-一次丝滑的探图记录
  • MyBatis系统学习(二)——MyBatis 核心配置与对象解析
  • 知识竞赛活动舞台搭建要多少钱