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
Posted in

Leave a Reply


Cosmic Pathways, Lab for Kids, and many of the other research activities discussed on this website is supported by the National Science Foundation and the Physics Teacher Education Coalition (PhysTEC) under grant no. 2325980. Any opinions, findings, and conclusions, or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Discover more from Cosmic Pathways

Subscribe now to keep reading and get access to the full archive.

Continue reading