Meisai BOOM Mathematical Modeling 1-2 Monte Carlo Method

Note: The article is organized according to the mathematical modeling BOOM online course, for personal use

❑Method Introduction

❑ Random sampling, statistical testing

• When the exact solution cannot be obtained , random sampling is carried out, and an approximate solution is obtained according to the statistical test .

❑ Popular understanding

• If there are 100,000 apples, you need to pick the largest one, but you can only pick with your eyes closed, and keep at most one apple in your hand. At the beginning, you can choose randomly with your eyes closed and get an apple.

• Then, continue to pick one with your eyes closed and compare it with the one you have in hand, keeping the larger one and throwing away the smaller one

• Repeat the previous step in a loop, the more times you pick, the greater the possibility of picking the largest apple

• But human beings have limited time and energy. Unless we pick all the 100,000 apples, we cannot be sure that the one picked is the biggest. So after picking 30,000, take the apple in your hand at this time as the largest approximate solution among 100,000 apples

❑ Translation Translation

• One hundred thousand apples is the largest: the feasible region is too large, and there is no general method to find an exact solution (it is unrealistic to pick all one hundred thousand apples)

• Pick with eyes closed: random sampling

• Leave the big one after each pick: statistical test

• After picking 30,000 times, the one in hand is regarded as the largest: find an approximate solution

• The greater the number of picks, the greater the probability of being close to the maximum: it needs to constitute statistical significance

❑ Simple examples

• There is no exact value for pi, how to find its approximate value? Ideas:

• 1. When will π be used ? When calculating the area; if the approximate value of the area can be obtained, the approximate value of π can naturally be obtained;

• 2. For a circle with a radius of 1, the area of ​​the circumscribed square associated with it has an exact value of 4;

• 3. If a large number of points are randomly scattered in the square , some fall inside the circle and some fall outside the circle;

• 4. Statistically , points in a circle/total points = circle area/square area;

5. There are 3 items in Article 4 that have statistical or precise values, so the statistical significance value of the "circle area" can be obtained, that is, the approximate value

• 6. Circle area S = π*(r^2), the approximate value of π can be calculated based on the first 5 items

❑Code solving

❑Method Analysis

• In order to realize random sprinkling, the rand function that comes with matlab can be used

• The more points you sprinkle, the more likely the obtained solution is closer to the optimal solution

❑ Precautions

• The Monte Carlo method is not an "algorithm", but a method and idea , and the codes written for different problems may vary widely

• For example, in the 6-step analysis mentioned earlier in this question, the "relationship between the area of ​​the circle and the circumscribed square" is used to solve the problem.

It is necessary to redefine the solution steps, not necessarily to apply the "statistical approximation of calculating the area ratio by sprinkling points"

• Strictly speaking, different problems have different probability distributions . This problem is evenly distributed. Common ones include Poisson distribution and normal distribution.

Distribution, exponential distribution, etc., need to formulate strategies according to the problem

❑ Focus

• When an exact solution cannot be obtained, an approximate solution is obtained based on statistical tests

Randomness : The rand function in matlab, rand(n) means generating n random numbers between 0 and 1

Statistics : n must be large enough, if it is too small, it does not constitute statistical significance

Approximate solution : not strictly exact solution. If there is a way to find the exact solution, no approximation is required! ! !

蒙特卡洛法求解圆周率近似值
% 
clc;clear
% 参数初始化:投放10000个点,圆半径为1,圆心坐标(1,1)
% 初始时还未投放点,有0个点在圆内,p为一共撒的点数;n代表落在圆内的点
p = 10000;  r = 1; x0 = 1;  y0 = 1;  n = 0; 

%将p个点随机放在一个边长为2的正方形内,该正方形内有个内切圆;
%正方形的面积是4;
%因为每个点都是“随机投放”的,最终落在圆内的点数比上总点数,就近似等于圆的面积比上正方形面积;
%即pi*(r^2)/4 = (圆内点数)/(总点数);

hold on     % 保持绘图窗口,多次绘图
for i = 1:p     % 对于要投放的总共p个点
    % rand函数产生在(0, 1)之间的随机数;rand函数还有其他多种形式,可自行百度
    px = rand*2;    % 随机生成该点的横坐标 ,px为0~2 
    py = rand*2;    % 随机生成该点的纵坐标
    % 所以,
    % 若该点在圆内,则颜色设为蓝色,变量n加一;在圆外则设为红色
    if (px-1)^2 + (py-1)^2 < 1      % 横纵坐标的平方和小于半径,则在圆内
        plot(px,py,'.','Color',"b");  %plot表示绘图 “.”表示画成.这个形状;颜色为blue
        n = n+1
    else
        plot(px,py,'.','Color',"r");
    end
end
axis equal      % 绘图时横纵坐标单位长度相同,便于观察圆
s = (n/p)*4;
pi0 = s;

% 注意:matlab本身有圆周率值,在计算时直接调用pi即可
% 例:a = 2*pi

Guess you like

Origin blog.csdn.net/yangxo2002/article/details/128653845