首页 > 其他分享 >sicp每日一题[2.13-2.16]

sicp每日一题[2.13-2.16]

时间:2024-09-17 13:24:30浏览次数:8  
标签:r1 r2 make interval sicp 2.16 div 2.13 define

Exercise 2.13

Show that under the assumption of small percentage tolerances there is a simple formula for the approximate percentage tolerance of the product of two intervals in terms of the tolerances of the factors. You may simplify the problem by assumingthat all numbers are positive.
After considerable work, Alyssa P. Hacker delivers her finished system. Several years later, after she has forgotten all about it, she gets a frenzied call from an irate user, Lem E. Tweakit. It seems that Lem has noticed that the formula for parallel resistors can be written in two algebraically equivalent ways:

He has written the following two programs, each of which computes the parallel-resistors formula differently:

(define (par1 r1 r2)
  (div-interval (mul-interval r1 r2)
                (add-interval r1 r2)))

(define (par2 r1 r2)
  (let ((one (make-interval 1 1)))
    (div-interval
     one (add-interval (div-interval one r1)
                       (div-interval one r2)))))

Lem complains that Alyssa's program gives different answers for the two ways of computing. This is a serious complaint.


这道题一是要修改乘法函数,二是要演示两种计算并联电阻的方法计算的结果不一致。

(define (mul-interval x y)
  (let ((cx (center x))
        (cy (center y))
        (px (/ (percent x) 100))
        (py (/ (percent y) 100)))
    (make-interval (* (- cx (* cx px)) (- cy (* cy py)))
                   (* (+ cx (* cx px)) (+ cy (* cy py))))))

(define r1 (make-interval 6.12 7.48))
(define r2 (make-interval 4.465 4.935))

(display-interval-tolerance (par1 r1 r2))
(display-interval-tolerance (par2 r1 r2))

; 执行结果
2.844199964577264 ± 22.613352145193332%
2.777440701636504 ± 7.05260392723452%

Exercise 2.14

Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals A and B, and use them in computing the expressions A/A and A/B. You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see Exercise 2.12).


(define r1 (make-interval 6.12 7.48))
(define r2 (make-interval 4.465 4.935))

(display-interval (div-interval r1 r1))
(display-interval (div-interval r1 r2))
(newline)
(display-interval-tolerance (div-interval r1 r1))
(display-interval-tolerance (div-interval r1 r2))

; 执行结果
[0.8181818181818182, 1.222222222222222]
[1.2401215805471126, 1.6752519596864504]

1.02020202020202 ± 19.801980198019795%
1.4576867701167815 ± 14.925373134328357%

根据执行结果来看,Lem显然是对的,对于 A/A,答案显然应该是 1 才对,但是程序计算出来的却是一个相对很大的范围,这是因为计算区间除法时,我们并没有定义“相等”这个概念,所以计算 A/A 时,它也按照通用的方法去计算了。

Exercise 2.15

Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa's system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a "better" program for parallel resistances than par1. Is she right? Why?


Eva 说的是对的,对于练习 2.13 计算出的两个答案,par2 更准确,跟书上给出的答案是一致的,至于原因我觉得应该是区间之间的运算每次都会增加不确定性,par2 其实只有相加那一步是两个区间进行运算,其他的都可以看作确定数字与区间的运算,没有增加额外的不确定性;而 par1 进行了3次区间之间的运算————乘法、加法和除法,乘法和除法都会使结果变得不确定。

Exercise 2.16

Explain, in general, why equivalent algebraic expressions may lead to different answers. Can you devise an interval-arithmetic package that does not have this shortcoming, or is this task impossible? (Warning: This problem is very difficult.)


(define a (make-interval 2 4))
 
(define b (make-interval -2 0))
 
(define c (make-interval 3 8))
 
(define x (mul-interval a (add-interval b c)))
 
(define y (add-interval (mul-interval a b)
                        (mul-interval a c)))


(display-interval x)
(display-interval y)

; 执行结果
[2, 32]
[-2, 32]

根据上面的结果可以看到,区间运算连乘法分配律都无法保证。至于有没有无缺陷的区间运算规则,我大概想了一下,没有什么头绪,就没继续想了。。

标签:r1,r2,make,interval,sicp,2.16,div,2.13,define
From: https://www.cnblogs.com/think2times/p/18417097

相关文章

  • sicp每日一题[2.10]
    Exercise2.10BenBitdiddle,anexpertsystemsprogrammer,looksoverAlyssa’sshoulderandcommentsthatitisnotclearwhatitmeanstodividebyanintervalthatspanszero.ModifyAlyssa'scodetocheckforthisconditionandtosignalanerror......
  • sicp每日一题[2.10]
    Exercise2.11Inpassing,Benalsocrypticallycomments:“Bytestingthesignsoftheendpointsoftheintervals,itispossibletobreakmul-intervalintoninecases,onlyoneofwhichrequiresmorethantwomultiplications.”Rewritethisprocedureusin......
  • sicp每日一题[2.9]
    Exercise2.9Thewidthofanintervalishalfofthedifferencebetweenitsupperandlowerbounds.Thewidthisameasureoftheuncertaintyofthenumberspecifiedbytheinterval.Forsomearithmeticoperationsthewidthoftheresultofcombiningtwo......
  • sicp每日一题[2.8]
    Exercise2.8>UsingreasoninganalogoustoAlyssa's,describehowthedifferenceoftwointervalsmaybecomputed.Defineacorrespondingsubtractionprocedure,calledsub-interval.这道题目也比较简单,只要注意到区间之差的下界是被减区间的下界减去另一个区间的上......
  • sicp每日一题[2.7]
    2.7Alyssa’sprogramisincompletebecauseshehasnotspecifiedtheimplementationoftheintervalabstraction.Hereisadefinitionoftheintervalconstructor:(define(make-intervalab)(consab))Defineselectorsupper-boundandlower-boundtoco......
  • 2.13 已知f(x)=(|x+1|-|x-1|)/2+sinx,g(x)=(|x+3|-|x-3|)/2+cosx,求下列超定方程组的
    点击查看代码importnumpyasnpdeff(x):return(abs(x+1)-abs(x-1))/2+np.sin(x)defg(x):return(abs(x+3)-abs(x-3))/2+np.cos(x)#假设我们有一些初始猜测值(这里只是随机选择的)x1_guess=0.5x2_guess=1.0......
  • sicp每日一题[2.6]
    Exercise2.6Incaserepresentingpairsasprocedureswasn'tmind-bogglingenough,considerthat,inalanguagethatcanmanipulateprocedures,wecangetbywithoutnumbers(atleastinsofarasnonnegativeintegersareconcerned)byimplementing0a......
  • mysql 连接失败:message from server: "Host '192.168.xx.xxx' is not allowed to conn
    前言mysql连接失败:messagefromserver:"Host'192.168.xx.xxx'isnotallowedtoconnecttothisMySQLserver"解决错误信息表明你尝试从IP地址192.168.xx.xxx连接到MySQL服务器,但是该IP地址没有被授权连接权限。为了解决这个问题,你需要确保你的MySQL用户权限......
  • sicp每日一题[2.5]
    Exercise2.5Showthatwecanrepresentpairsofnonnegativeintegersusingonlynumbersandarithmeticoperationsifwerepresentthepairaandbastheintegerthatistheproduct2a3b.Givethecorrespondingdefinitionsoftheprocedurescons,car,a......
  • sicp每日一题[2.4]
    Exercise2.4Hereisanalternativeproceduralrepresentationofpairs.Forthisrepresentation,verifythat(car(consxy))yieldsxforanyobjectsxandy.(define(consxy)(lambda(m)(mxy)))(define(carz)(z(lambda(pq)p)))Whatistheco......