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

  • The Modern C# Challenge
  • Rod Stephens
  • 232字
  • 2021-08-13 15:24:00

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.

主站蜘蛛池模板: 诸城市| 烟台市| 关岭| 巴马| 贵州省| 桃园县| 循化| 长海县| 施甸县| 永修县| 韶山市| 富裕县| 麟游县| 陆丰市| 宁夏| 南部县| 金坛市| 城市| 延川县| 桑植县| 彭州市| 曲沃县| 紫金县| 正宁县| 沐川县| 叙永县| 罗定市| 晋宁县| 永清县| 通许县| 临高县| 乌拉特前旗| 玉环县| 温泉县| 扎兰屯市| 横峰县| 荆州市| 崇左市| 琼中| 游戏| 太白县|