![]() |
关于matlab运行中出现的错误,进行纠错!
我在学习蒙特卡洛算法时,设计一个“投掷一枚硬币的概率求解”程序如下:
clear all clc counter=0;i=1; x=rand(10000);n=length(x); for i=1:n if x(i)<=1/2&x(i)>=0 counter=counter+1; else counter=counter; end i=i+1; end p=counter/n 问题是:(1)每次运行时,结果虽然接近0.5,但是为什么每次运行的结果都不一样呢? (2)如果把随机数生成命令x=rand(10000);改成x=rand(100000)后为什么会产生错误,而无法运行了。 |
回复: 关于matlab运行中出现的错误,进行纠错!
clear all
clc counter=0;i=1; x=rand(1,100000);n=length(x); for i=1:n if x(i)<=1/2&x(i)>=0 counter=counter+1; else counter=counter; end i=i+1; end p=counter/n 首先,所谓蒙特卡洛就是一个随机算法,随机肯定每一次结果不同,第二,你的代码中有个错误rand, |
所有时间均为北京时间。现在的时间是 06:24。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.