蒙特卡洛仿真-3D随机游走、积分.pdfVIP

  • 8
  • 0
  • 约2.4千字
  • 约 3页
  • 2020-08-06 发布于云南
  • 举报
蒙特卡洛方法求任意函数的积分 %Monte Carlo method clear clc disp( Please enter the function f=@(x) ); keyboard disp( Please enter the range of integration,a and b (ab) ); keyboard disp( Please enter the number n of repeat ); keyboard sum=0; for i=1:n k=rand(1); x=(b-a)*k+a; sum=sum+f(x); end fmean=sum/n; intf=(b-a)*fmean; disp( The value of this integral is ); disp(intf); syms y intr=int(f(y),y,a,b); intr=double(intr); disp( The real value of this integral is ); disp(intr); dis=abs(intf-intr); disp( The distance of two values is ); disp(dis); 蒙特卡洛方法仿真模拟三维随机游走 %The drunk walk clear clc disp( Please enter the number n of steps ); keyboard; x=0; y=0; z=0; for i=1:n x0=x; y0=y; z0=z; direction=rand(1); if (direction1/6) x=x+1; %move right elseif (direction2/6) x=x-1; %move left elseif (direction3/6) y=y+1; %move forward elseif (direction4/6) y=y-1; %move backward elseif (direction5/6) z=z+1; %move upward elseif (direction6/6) z=z-1; %move downward end distance=sqrt(x^2+y^2+z^2); if (distance5); col= g ; elseif (distance10); col= b ; elseif (distance15) col= y ; else col=

文档评论(0)

1亿VIP精品文档

相关文档