平滑函数的公式为
他的几何意义应该是将每个点的导数,变化率变小。说简单点就是函数那块曲线弯度比较大,加上这个函数就会减小他的弯度,直到他无限接近于一条直线,也就是没弯度。
我们将dx值适当调大,一遍能较好的观察平花钱和平滑后的对比结果。
#lang racket
(define dx 0.1)
(define (cube x) (* x x x))
(define (square x) (* x x))
(define (inc x) (+ 1 x))
(define (compose f g)
(lambda (x)
(f (g x))))
(define (repeated f n)
(if (= n 1)
f
(compose f (repeated f (- n 1)))))
(define (smooth f)
(lambda (x)
(/ (+ (f (+ x dx)) (f x) (f (- x dx))) 3)))
(define (smooth-n f n)
(repeated (smooth f) n))
((repeated square 5) 2)
((repeated cube 3) 2)
((repeated inc 5) 3)
((smooth-n square 5) 2)
((smooth-n cube 3) 2)
((smooth-n inc 5) 3)
运行结果
4294967296
134217728
8
4426076645.883854
140510092.09302887
8.0