练习3-5

原文

Exercise 3.5. Monte Carlo integration is a method of estimating definite integrals by means of Monte Carlo simulation. Consider computing the area of a region of space described by a predicate P(x, y) that is true for points (x, y) in the region and false for points not in the region. For example, the region contained within a circle of radius 3 centered at (5, 7) is described by the predicate that tests whether (x - 5)2 + (y - 7)2< 32. To estimate the area of the region described by such a predicate, begin by choosing a rectangle that contains the region. For example, a rectangle with diagonally opposite corners at (2, 4) and (8, 10) contains the circle above. The desired integral is the area of that portion of the rectangle that lies in the region. We can estimate the integral by picking, at random, points (x,y) that lie in the rectangle, and testing P(x, y) for each point to determine whether the point lies in the region. If we try this with many points, then the fraction of points that fall in the region should give an estimate of the proportion of the rectangle that lies in the region. Hence, multiplying this fraction by the area of the entire rectangle should produce an estimate of the integral.

Implement Monte Carlo integration as a procedure estimate-integral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate . Use your estimate-integral to produce an estimate of by measuring the area of a unit circle.

You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-in-range procedure implements this in terms of the random procedure used in section 1.2.6, which returns a nonnegative number less than its input.8

(define (random-in-range low high) (let ((range (- high low))) (+ low (random range))))

分析

(define (monte-carlo-pi trials) (define (monte-carlo trials experiment) (define (iter trials-remaining trials-passed) (cond ((= trials-remaining 0) (/ trials-passed trials)) ((experiment) (iter (- trials-remaining 1)(+ trials-passed 1))) (else (iter (- trials-ramaining 1) trials-passed)))) (iter trials 0)) (define (random-in-range low high) (let ((range (- high low))) (+ low (random (exact->inexact range))))) (define (estimate-integral p? x1 y1 x2 y2 trials) (* 4 (monte-carlo trials (lambda () (p? (random-in-range x1 x2) (random-in-range y1 y2)))))) (exact->inexact (estimate-integral (lambda (x y) (< (+ (square x) (square y)) 1.0)) -1.0 -1.0 1.0 1.0 trials)))

http://blog.csdn.net/nomasp

Ravi 0.18 发布，Lua 5.3 衍生编程语言

Ravi 编程语言是 Lua 5.3 的一个衍生，有限的可选静态类型，基于 LLVM 和 libgccjit 的 JIT 编译器。Ravi 的名字来自梵语的太阳。有趣的是，Lua 的前身是 Sol，它支持静态类型，Sol 是葡萄牙...

2016/12/11
1K
2

skaiser
2017/06/19
0
0
MULE新手入门

yzbty23
2016/08/09
310
0
usermod命令用法、用户密码管理文件以及mkpasswd密码生成工具

9月20日任务 3.4 usermod命令 3.5 用户密码管理 3.6 mkpasswd命令 3.4 、usermod命令 # 更改用户属性命令 [root@zgxlinux-01 ~]# usermod -u 111 username # 更改用户属性[root@zgxlinux-01...

zgxlinux
2018/09/20
17
0

nxweb 是采用 Python 和 C 编写的快速而且轻量级的 Web 服务器软件。 特性： 性能优异 可处理数千并发 内存占用小 事件驱动、多线程模型 代码量小 简单的 C API 体面的 HTTP 协议处理 支持 ...

dqzhangp
2015/11/25
1K
0

FreeRTOS列表&列表项的源码解读 第一次看列表与列表项的时候，感觉很像是链表，虽然我自己的链表也不太会，但是就是感觉很像。 在FreeRTOS中，列表与列表项使用得非常多，是FreeRTOS的一个数...

32分钟前
4
0
Java的23种设计模式，详细讲解（一）

48分钟前
5
0

https://blog.csdn.net/qq_16064871/article/details/70767949

shzwork
48分钟前
3
0
SpringCloud实现分库分表模式下，数据库实时扩容方案

49分钟前
5
0
js--时间切割装换工具类

<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> /* * 修改data原型对象Format方法 ......

zhengzhixiang
59分钟前
4
0 