⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the_calculation_of_pi.m

📁 圆周率统计计算方法仿真(即蒙特卡罗Monte Carlo仿真) 文件 应用程序:圆周率的统计计算.exe 源代码文件:圆周率的统计计算.c
💻 M
字号:
%本程序是采用蒙特卡罗仿真计算圆周率的统计方法。
%每次采用N组数据(N个点)来计算圆周率,共计算NUM次,最后对计算结果进行统计平均。
format long;
%-------------------------------------------------------------------------------------
NUM=200;
figure(1);
for count=1:NUM
    N=500;
    A=rand(1,N);
    B=rand(1,N);
    for n=1:N
        plot(A(n),B(n),'r')
        hold on;
    end
    m(count)=0;
    for n=1:N
        C(n)=A(n)*A(n)+B(n)*B(n);
        if C(n)<=1
        m(count)=m(count)+1;
        end
    end
end

x=0:0.1:pi/2;
y=ones(1,length(x));
polar(x,y,'g');
title('圆周率的统计算法');
xlabel('x');
ylabel('y');
%------------------------------------------------------------------------------------
figure(2);
plot(1:NUM,4*m/N);
title('圆周率的统计平均');
hold on;
xlabel('Count');
ylabel('vlaue');
temp=0;
for count=1:NUM
     temp=temp+4*m(count)/N;
end
pai=temp/NUM;
plot(1:NUM,pai,'r');
%-----------------------------------------------------------------------------------
stringa='统计平均值为:';
string_pai=strcat(stringa,num2str(pai));
msgbox(string_pai,'圆周率计算');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -