To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp
Email and Skype:[email protected]
Facebook:https://www.facebook.com/yuwang.ke
CSDN Blog: http://blog.csdn.net/nomasp
Sina Weibo: http://weibo.com/nomasp
Exercise 3-5 Original
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's true for points (x, y) in the RE Gion and false for points not in the region. For example, the region contained within a circle of radius 3 centered at (5, 7) are described by the predicate that tests Whether (x-5) 2 + (y-7) 2< 32. To estimate the area's 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, ten) contains the circle above. The desired integral is the area of that portion of the rectangle. We can estimate the integral by picking, at random, points (x, y) that lie in the rectangle, and testing P (x, y) for each P Oint 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 a 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, is used above to estimate. Use your estimate-integral to produce an estimate of by measuring the area of a unit circle.
You'll find it useful to has a procedure that returns a number chosen at the random from a given range. The following Random-in-range procedure implements this on terms of the random procedure used in section 1.2.6, which Retu RNs a nonnegative number less than its input.8
(define (random-in-range low high) (let ((range (- high low))) (+ low (random range))))
Analysis
Monte Carlo method to find the principle of pi: In a length of 1 units, an area of 1 square units of the square, a square vertex as the center of the 1/4 Circle, the area of PI/4. The probability of placing n points evenly in a square within a sector (1/4 circle) = Sector area/Square area =PI/4. So as long as the n coordinates (x, y) are randomly generated, the data satisfying the x^2+y^2<1 is valid. The number of times a fan is multiplied by N and multiplied by 4 is theoretically close to pi.
We then borrow the Monte-carlo function and the Random-in-range function given in the title at the bottom of page 155th of the book. (because the final result is floating point, it is better to make the random-in-range slightly modified).
Code
(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) ) ) (+ "/ span> low (random (exact->inexact Range) ) ) ) ) (define ( Estimate-integral p? X1 y1 x2 y2 trials) (* "/ Span> 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)) )
"SICP Exercise" 105 practice 3.5-3.6