Introduction to Monte Carlo Methods
Monte Carlo (Monte Carlo, abbr . MC) method is a method of solving physical and mathematical problems using independent repeated statistical experiments. A simple application of the MC method is to find the area of a graph .
See the complete code
Example: Area to be solved ?
%投点次数
M = 1000;
A = rand(M,2);
r = sqrt((A(:,1) - 0.5).^2 + (A(:,2) - 0.5).^2);
total = sum(r < 0.5);
pr = total / M;
disp(pr)
%绘图
xt = @(t) 0.5*cos(t) +0.5;
yt = @(t) 0.5*sin(t) + 0.5;
fplot(xt,yt)
hold on
plot(A(:,1),A(:,2),'.');
grid on
axis equal
axis([0,1,0,1])
Calculated result: 0.7630