Today we were calculating the Volume N-dimensional objects using the Monte-Carlo method in MATLAB. This plot3 is beautiful, almost artwork!

% Find the value of pi
Num = 12000;
% Find xx and yy values
rr = rand(3,Num);
% Find the radius:
radius = sqrt(rr(1,:).^2 + rr(2,:).^2 + rr(3,:).^2);
% Amount inside
mask = radius < 1;
inside = sum(double(mask));
% Volume
Vol = (2^3)*inside / Num
plot3 (rr(1,:),rr(2,:),rr(3,:),'ok','MarkerFaceColor','k', 'MarkerSize',5)
hold on
plot3 (rr(1,mask),rr(2,mask),rr(3,mask),'or','MarkerFaceColor','r')
hold off




Leave a Reply