Matlab 版 (精华区)
发信人: ramjet (史努比狗), 信区: Matlab
标 题: 使用monte carlo方法估计pi的一个小程序
发信站: 哈工大紫丁香 (2002年01月27日16:32:23 星期天), 站内信件
%set-up uniformly random numbers for x-coor's and y-coor's
xpoints=[rand(1,4000) rand(1,4000)-1 rand(1,4000) rand(1,4000)-1];
ypoints=[rand(1,4000) rand(1,4000)-1 rand(1,4000)-1 rand(1,4000)];
%Total number of N trials
fprintf('The number of trials in the experiment is %.0f\n',length(xpoints
))
%The number of trial points in the circle
fprintf('The number of trial points in the circle is %.0f\n',sum(xpoints.
^2+ypoints.^2<=1))
%Monte Carlo estimation of Pi
fprintf('The Monte Carlo estimate of Pi is %f\n',(4*sum(xpoints.^2+ypoints
.^2<=1))/length(xpoints))
%Plot of the circle and the points
hold on
fplot('-sqrt(1-x^2)',[-1 1],'k');
fplot('sqrt(1-x^2)',[-1 1],'k');
x=xpoints;
y=ypoints;
plot(x,y,'y.');
title('Plot of Random Points over the Interval [-D,D]')
xlabel('Graphics by Michael Chamberland')
--
在这个时代
如果你要为社会做一点贡献的话
那么就想办法
让你和你爱的人
好好的活着
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.237.3]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.089毫秒