Monte Carlo(蒙特卡罗)

Monte Carlo(蒙特卡罗)

Monte Carlo 是一种基于计算机的求解方法,利用计算机在input domain上产生一组随机数,通过这一组随机数计算出一组结果。通过这样的一个反复过程最终大致得到待求解值。
具体过程:
  1. Define a domain of possible inputs
  2. Generate inputs randomly from a probability distribution over the domain
  3. Perform a deterministic computation on the inputs
  4. Aggregate the results
例子:求圆周率:
代码:
注意:1. 通过text动态的显示文本,要将上一次的text删除,通过它的句柄删除它;
            2. 画图的动态显示,需要pause函数暂停才能显示; 

i=0;
h=figure(1);
count1 = 0;
count2 = 0;

while i<100000
location = rand(1,2);

if sum((location).^2)<1
    count1 = count1 + 1;
    plot(location(1),location(2),'Color','r','Marker','.','LineWidth', 4);
    axis([0 1 0 1])
    hold all;
else
    count2 = count2 + 1;
    plot(location(1), location(2), 'Color', 'b','Marker', '.','LineWidth', 4);
end
i =i+1;
pi = double(4*count1/i);
pause(0.0000001);
if i ==1
    ha = text(0.5,1.06,['n = ', num2str(i)]);
    hb = text(0.8,1.06,['\pi = ', num2str(pi)]);
else
    delete(ha);
    delete(hb);
    ha = text(0.5,1.06,['n = ', num2str(i)]);
    hb = text(0.8,1.06,['\pi = ', num2str(pi)]);
end
end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值