蒙特卡洛積分的一個例子

問題:一個三維區域,它由不等式xyz≤1和-5≤x≤5,-5≤y≤5,-5≤z≤5確定.γ(x,y,z)是給定密度.假設密度γ(x,y,z)=e0.5z.求出該物體的體積和質量.

解析這樣一個積分將很困難或者是不可能的,但是用蒙特卡洛積分進行近似是很容易的.

<matlab>
N = input('Enter number of sample points: ');
gamma = inline('exp(0.5 * z)'); 
volumeOfBox = 10 * 10 * 10;
vol = 0;
mass = 0; 
volsq = 0; 
masssq = 0;

for i = 1: N
    x = -5 + 10 * rand;
    y = -5 + 10 * rand;
    z = -5 + 10 * rand;
    if x * y * z <= 1
        vol = vol + 1;
        mass = mass + gamma(z);
        volsq = volsq + 1;
        masssq = masssq + gamma(z) ^ 2;
     end
end

volumeOfObject = (vol / N) * volumeOfBox
volvar = (1 / N) * ((volsq / N) - (vol / N) ^ 2)
volstd = sqrt(volvar)

massOfObject = (mass / N) * volumeOfBox
massvar = (1 / N) * ((masssq / N) - (mass / N) ^ 2)
massstd = sqrt(massvar)
</matlab>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章