Skip to content

Instantly share code, notes, and snippets.

@yanfeng42
Created July 7, 2021 05:56
Show Gist options
  • Save yanfeng42/f5d2db273f687b42712405e9d674025d to your computer and use it in GitHub Desktop.
Save yanfeng42/f5d2db273f687b42712405e9d674025d to your computer and use it in GitHub Desktop.
sicp 1.40 计算任意 方程的近似解
(define (newton-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)
)
)
(define dx 0.00001)
(define (cube x) (* x x x))
(define (square x) (* x x))
(define (newtons-method g guess)
(fixed-point (newton-transform g) guess)
)
(define (cubic a b c)
(lambda (x) (+ (cube x) (* a (square x)) (* b x) c))
)
(define (my-cubic a b c)
(newtons-method (cubic a b c) 1)
)
@yanfeng42
Copy link
Author

这是一个非常有价值的工具方法. 比如: 估算自己的 计算结果, 是否正确.(快速演算 -- 家长 育儿必备哈)

类似的问题, 我以前有思考过. 当时自己的思路是: 从某一个不可能的值, 逐个 遍历. 比如: -99, -88.999, -88.998.....

很明显, 我的 暴力穷举猜测 法, 明显效率更低, 兼容性也更低.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment