官术网_书友最值得收藏!

25. Monte Carlo integration

Integration is the process of calculating the area inside a region. Typically, this is the area under a curve defined by some function y = F(x). For example, in the following diagram, the goal is to calculate the shaded area under the curve in the domain -2 ≤ x ≤ 1.5:

You can use calculus to integrate the area under a curve, at least if you remember your calculus and the function is reasonably well-behaved. If you cannot apply calculus, then you can use Monte Carlo integration.

From the name and your experience with Monte Carlo approximation for π, you may be able to guess how this works. Pick a rectangle that includes the area of interest. Then, randomly generate points within the rectangle and see how many lie within the range and between the curve and the X axis. Finally, use the fraction of points in that area to estimate the area beneath the curve.

Write a program that performs Monte Carlo integration. As in the previous examples, pass the function into the method as a parameter so you can easily change it. Use your program to estimate the areas below the following functions within the indicated domains:

 

If you remember your calculus, make the program calculate the areas exactly and compare the calculated result and the result approximated by Monte Carlo integration.

主站蜘蛛池模板: 于都县| 烟台市| 长沙市| 东乡族自治县| 武山县| 邯郸市| 兴业县| 察雅县| 隆德县| 油尖旺区| 泗阳县| 莱阳市| 新龙县| 孙吴县| 体育| 平阳县| 行唐县| 怀安县| 绥江县| 黄石市| 潢川县| 达日县| 澄江县| 石首市| 卢湾区| 双城市| 油尖旺区| 开封县| 土默特右旗| 马龙县| 阿拉善左旗| 来安县| 湾仔区| 白山市| 大新县| 南华县| 兰坪| 巴林左旗| 永宁县| 仙桃市| 石狮市|