Interval arithmetic by Robert Elton Maas: Demonstration of using Newton's method with intervals of exact rational numbers to compute square root of an interval approximation to 2 which starts out very imprecise then gets more precise when needed, i.e. "lazy evaluation" emulated manually: * (setq ndig 2) (setq powr (expt 10 ndig)) 2 * 100 * (setq nums (list (/ (+ powr powr -1) powr) (/ (+ powr powr 1) powr))) (199/100 201/100) * (show-interval nums) 1.99d0 2.01d0 1.99[0..K] NIL * (setq guess (interval-to-sqrt-firstguess+bounds nums)) 3/2 * (setq rats (interval+guess-to-sqrt-bounds nums guess)) (199/150 3/2) * (show-interval rats) 1.3266666666666667d0 1.5d0 1.3[2..K] NIL * (setq rats (interval-newton-sqrt nums rats)) 1.3266666666666667d0 1.5d0 1.3[2..K] [--------------------------------!!!!!!!-------------------------------] 1.4080188679245282d0 1.4221698113207548d0 1.4[0..3] [-------------------------------!!!!!!!--------------------------------] 1.4047058823529412d0 1.4188235294117648d0 1.40[4..J] (597/425 603/425) * (setq rats (interval-newton-sqrt nums rats)) 1.4047058823529412d0 1.4188235294117648d0 1.40[4..J] ** WARNING: New interval not included within old interval, ** so I'll have to intersect the two to get a valid interval refinement. [------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4095833333333334d0 1.4188235294117648d0 1.40[9..J] [!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4047058823529412d0 1.4188235294117648d0 1.40[4..J] Interval didn't shrink enough: Old: 1.40[4..J] New: 1.40[4..J] Refinement (i.e. shrinkage) factor = 1.0 ** RETURN to go on: (597/425 603/425) ;;Note: Newton's method couldn't get much more accuracy above, ; given imprecise interval of the input data, so now we make the ; input data more precise then before: * (setq ndig 5) (setq powr (expt 10 ndig)) 5 * 100000 * (setq nums (list (/ (+ powr powr -1) powr) (/ (+ powr powr 1) powr))) (199999/100000 200001/100000) * (show-interval nums) 1.99999d0 2.00001d0 1.99999[0..K] NIL ;;Now we just pick up the Newton's method iteration from where we left off, ; no need to re-start from the initial state. Note that because the ; previous interval was based on imprecise data, it's not nicely centered ; on sqrt(2), so the first step below will be lopsided (the midpoint of ; the old interval isn't a good guess of sqrt(2)), but the next ; step after it will be centered (the midpoint is now a good guess): * (setq rats (interval-newton-sqrt nums rats)) 1.4047058823529412d0 1.4188235294117648d0 1.40[4..J] [-----------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!----------] 1.411764705882353d0 1.41667375d0 1.41[1..7] [----------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!----------] 1.4117576470588236d0 1.4166666666666667d0 1.41[1..7] (599997/425000 17/12) * (setq rats (interval-newton-sqrt nums rats)) 1.4117576470588236d0 1.4166666666666667d0 1.41[1..7] [----------------------------------!!----------------------------------] 1.4142078968100025d0 1.4142220389596813d0 1.4142[0..3] [----------------------------------!!----------------------------------] 1.4142043674176776d0 1.4142185095320623d0 1.41420[4..J] (10199949/7212500 10200051/7212500) * (setq rats (interval-newton-sqrt nums rats)) 1.4142043674176776d0 1.4142185095320623d0 1.41420[4..J] ** WARNING: New interval not included within old interval, ** so I'll have to intersect the two to get a valid interval refinement. [---------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4142086151960784d0 1.4142185095320623d0 1.41420[8..J] [!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4142043674176776d0 1.4142185095320623d0 1.41420[4..J] Interval didn't shrink enough: Old: 1.41420[4..J] New: 1.41420[4..J] Refinement (i.e. shrinkage) factor = 1.0 ** RETURN to go on: (10199949/7212500 10200051/7212500) ;;Once again Newton's method couldn't do any better with the given ; data, so once again we make the given data more precise and then ; pick up Newton's method where we left off: * (setq ndig 25) (setq powr (expt 10 ndig)) 25 * 10000000000000000000000000 * (setq nums (list (/ (+ powr powr -1) powr) (/ (+ powr powr 1) powr))) (19999999999999999999999999/10000000000000000000000000 20000000000000000000000001/10000000000000000000000000) * (show-interval nums) 2.0d0 2.0d0 1.9999999999999999999999999[0..K] NIL * (setq rats (interval-newton-sqrt nums rats)) 1.4142043674176776d0 1.4142185095320623d0 1.41420[4..J] [-----------------------------------!!!!!!!!!!!!!!!!!!!!!!-------------] 1.41421143847487d0 1.41421568627451d0 1.41421[1..6] [----------------------------------!!!!!!!!!!!!!!!!!!!!!!!-------------] 1.4142114384748696d0 1.4142156862745099d0 1.41421[1..6] (7009386627311/4956392259753 577/408) * (setq rats (interval-newton-sqrt nums rats)) 1.4142114384748696d0 1.4142156862745099d0 1.41421[1..6] [----------------------------------!-----------------------------------] 1.4142135623715004d0 1.4142135623746896d0 1.41421356237[1..5] [----------------------------------!-----------------------------------] 1.4142135623728214d0 1.4142135623733687d0 1.414213562372[8..E] (1607521/1136689 22733780000000000000000001136689/16075210000000000000000000000000) * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623728214d0 1.4142135623733687d0 1.414213562372[8..E] [----------------------------------!-----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688[5..8] [----------------------------------!-----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[2..H] (36545028759379999999999998172748562031/25841237654415000000000000000000000000 12181676253126666666666667275750479323/8613745884805000000000000000000000000) * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[2..H] ** WARNING: New interval not included within old interval, ** so I'll have to intersect the two to get a valid interval refinement. [--------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[7..H] [!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[2..H] Interval didn't shrink enough: Old: 1.4142135623730950488016886[2..H] New: 1.4142135623730950488016886[2..H] Refinement (i.e. shrinkage) factor = 1.0 ** RETURN to go on: (36545028759379999999999998172748562031/25841237654415000000000000000000000000 12181676253126666666666667275750479323/8613745884805000000000000000000000000) ;;Again, exhausted the precision available in the input data, so we ; make the input data more precise again etc.: * (setq ndig 70) (setq powr (expt 10 ndig)) 70 * 10000000000000000000000000000000000000000000000000000000000000000000000 * (setq nums (list (/ (+ powr powr -1) powr) (/ (+ powr powr 1) powr))) (19999999999999999999999999999999999999999999999999999999999999999999999/1000000 0000000000000000000000000000000000000000000000000000000000000000 20000000000000000000000000000000000000000000000000000000000000000000001/1000000 0000000000000000000000000000000000000000000000000000000000000000) * (show-interval nums) 2.0d0 2.0d0 1.9999999999999999999999999999999999999999999999999999999999999999999999[0..K] NIL * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[2..H] [-----------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!--------] 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[9..G] [----------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!--------] 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[9..G] (3654502875937999999999999999999999999999999999999999999999999999999999817274856 2031/258412376544150000000000000000000000000000000000000000000000000000000000000 00000000 5168247530883/3654502875938) * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623730951d0 1.4142135623730951d0 1.4142135623730950488016886[9..G] [----------------------------------!-----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376947[8..E] [----------------------------------!!----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376947[8..E] (75549501860685563890373016/53421565080956452077519377 3561437672063763471834625133333333333333333333333333333333333333333333511405216 93652150692506459/25183167286895187963457672000000000000000000000000000000000000 0000000000000000000000000000000000) * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376947[8..E] [----------------------------------!-----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376948073176679737990732[3..6] [----------------------------------!!----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376948073176679737990732[3..7] (116104232852194624065131557975456296/82098090374248746619236402542311697 5473206024949916441282426836154113133333333333333333333333333333333333606993634 58082915539745467514103899/38701410950731541355043852658485432000000000000000000 0000000000000000000000000000000000000000000000000000) * (setq rats (interval-newton-sqrt nums rats)) 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376948073176679737990732[3..7] ** WARNING: New interval not included within old interval, ** so I'll have to intersect the two to get a valid interval refinement. [!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-----------------------------------] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376948073176679737990732[3..6] [!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!] 1.4142135623730951d0 1.4142135623730951d0 1.414213562373095048801688724209698078569671875376948073176679737990732[3..7] Interval didn't shrink enough: Old: 1.414213562373095048801688724209698078569671875376948073176679737990732[3.. 7] New: 1.414213562373095048801688724209698078569671875376948073176679737990732[3.. 7] Refinement (i.e. shrinkage) factor = 1.0 ** RETURN to go on: (116104232852194624065131557975456296/82098090374248746619236402542311697 5473206024949916441282426836154113133333333333333333333333333333333333606993634 58082915539745467514103899/38701410950731541355043852658485432000000000000000000 0000000000000000000000000000000000000000000000000000) * ;;I could make the input data even more precise and continue to generate ; as many digits of sqrt(2) as we want, but I think you get the idea already. Would you like me to set up this software as a Web-server-side application so that you-all can play with it? Urgent note: I'm in desperate financial circumstances. I desperately need a source of income to avoid becoming homeless. Please refer me to somebody who has money to hire me for writing software.