计算机程序的构造和解释 练习 1.40 ~ 1.45
来源:互联网 发布:江苏网络卖淫 编辑:程序博客网 时间:2024/06/05 15:13
#lang racket#lang racket;: 求不动点 f(x) = x 则x为不动点(define (fixed-point f first-guess) (define tolerance 0.00001) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess));: 平均阻尼函数(define (average-damp f) (define (average a b) (/ (+ a b) 2)) (lambda (x) (average x (f x))));: 重新定义求平方根函数(define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1.0));: 求平方(define (square x) (* x x));: 求立方(define (cube-root x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1.0));: 求导数(define (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))(define dx 0.00001);: 牛顿法 中的 f(x) = x - g(x) / Dg(x)(define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x)))));: 牛顿法(define (newtons-method g guess) (fixed-point (newton-transform g) guess));;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: transform 是对函数 g 的变换函数,返回值还是丈函数(define (fixed-point-of-transform g transform guess) ;: 变换之后求不动点 (fixed-point (transform g) guess));: 重新定义 平方根函数(define (sqrt2 x) (fixed-point-of-transform (lambda (y) (/ x y)) average-damp ;: 将把上面的前一个函数进行变换 1.0))(define (sqrt3 x) (fixed-point-of-transform (lambda (y) (- (square y) x)) newton-transform 1.0));;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:练习 1.40 求方程的0点(define (cubic a b c) (lambda (x) (+ (* x x x) (* a x x) (* b x) c)));: ;:(newtons-method (cubic 1 2 3) 1);: 结果-1.2756822036498454;: 验证;:((cubic 1 2 3) -1.2756822036498454) ;: 结果4.935607478273596e-012;: 结果差不多;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: 练习 1.41 请定义一过程double(define (double f) (lambda (x) (f (f x))))(define (inc x) (+ 1 x));:(newline);:((double inc) 1);:;:(((double (double double)) inc) 5);: 21;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: 练习 1.42;: f(g(x))(define (compose f g) (lambda (x) (f (g x))));:(newline);:((compose square inc) 6);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: 练习 1.43#|(define (repeated f n) (if (= n 1) f ;: n = 1 时,直接返回原函数 (lambda (x) ;: 否则返回 (f ((repeated f (- n 1)) x) ;: 对其余的调用 n-1次 f,再对结果调用一次f.这样总共调用了n次 ))))|#;: 练习 1.43使用 compose;: 调用 f函数 n次(define (repeated f n) (if (= n 1) f (compose f (repeated f (- n 1)))));:(newline);:((repeated square 2) 5);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: 练习1.44;: 一次平滑函数(define (smooth f) (define (iter dx) (lambda (x) (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3))) (iter 0.00001));: n 次平滑函数(define (smooth-n f n) ((repeated smooth n) f)) ;:((smooth-n square 10) 5);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:练习 1.45;: 计算 4次方根,使用一次平均阻尼不好使;: y^4 = x => y|-> x/(y^3)#|;: 用一次平均阻尼做不动点 不收敛!!!!!(y + x / y^3) / 2(define (sqrt-4 x) (fixed-point (average-damp (lambda (y) (/ x (* y y y)))) 1.0))|#;: 求 x 的n次方根(define (pow x n) (if (= 0 n) 1 (* x (pow x (- n 1)))))(define (sqrt-n x n) ;: 求以2为底n的对象,并取整 (define (lg n) (cond ((> (/ n 2) 1) (+ 1 (lg (/ n 2)))) ((< (/ n 2) 1) 0) (else 1))) (define re-times (lg n)) ;: 求不动点 (fixed-point ((repeated average-damp re-times) (lambda (y) (/ x (pow y (- n 1)))) ) ;: 对此函数求 re-times次平均阻尼 1.0));: 测试(sqrt-n 100000 5)(sqrt-n 1000000 6)(sqrt-n 10000000 7)(sqrt-n 100000000 8)
0 0
- 计算机程序的构造和解释 练习 1.40 ~ 1.45
- 计算机程序的构造和解释 练习 1.8
- 计算机程序的构造和解释 练习1.9
- 计算机程序的构造和解释 练习 1.10
- 计算机程序的构造和解释 练习 1.11
- 计算机程序的构造和解释 练习 1.12
- 计算机程序的构造和解释 练习 1.13
- 计算机程序的构造和解释 练习 1.29
- 计算机程序的构造和解释 练习 1.30
- 计算机程序的构造和解释 练习 1.31
- 计算机程序的构造和解释 练习 1.32
- 计算机程序的构造和解释 练习 1.33
- 计算机程序的构造和解释 练习 1.36
- 计算机程序的构造和解释 练习 1.37
- 计算机程序的构造和解释 练习 1.38
- 计算机程序的构造和解释 练习 1.39
- 计算机程序的构造和解释 练习 1.46
- 计算机程序的构造和解释 练习 2.1
- iOS 设置tableView每个分区cell圆角
- 第6课:SparkStreaming源码解读之Job动态生成和深度思考
- 反编译插件神器 JadClipse
- ffmpeg学习(0)——什么是ffmpeg
- 多周期cpu设计(verilog)
- 计算机程序的构造和解释 练习 1.40 ~ 1.45
- SpringMVC经典必看——使用 Spring2.5注释详解(@Autowired、@Resource 、@PostConstruct、PreDestroy 和 @Component)
- javascript之BOM编程Screen(屏幕)对象
- schedule() 和 scheduleAtFixedRate() 区别
- 最强大脑唯爱水哥、ACM敬佩楼教主
- iOS 相机(需真机测试)
- 图片下载之缓存
- MongoDB创建数据库和删除数据库
- 【BZOJ4044】[Cerc2014] Virus synthesis【回文自动机】