0

Sorry.I have a homework but I cannot do.I have to write code for between cube and ellipse'volume with Monte Marlo Method.Can you help me? Dimension for cube is 10 and radius for ellipse is 5 and height is 3. Thank you

1 Answers1

0

I wrote this codes for the intersection of a cylinder and a sphere but I cannot translate for ellipse and cube

%clear the workspace and command window
clc
clear
% find the volume of the intersection of a cylinder and a sphere 

RunLength=10000; % run length
Count=0;
%initialize the empty arrays
px=[];
py=[];
pz=[];
qx=[];
qy=[];
 qz=[];
 d=0.2
 %try 10000 points
 for i=1:RunLength
 x=2*rand()-1; %x coordinate random number between (-1,1)
 y=2*rand()-1; %y coordinate random number between (-1,1)
 z=2*rand()-1; %z coordinate random number between (-1,1)
 if(x^2+y^2+z^2<=1); %if the point is in the big sphere (x^2+y^2+z^2<=1^2)
 if (y^2+(z-(1-0.5)^2)>=(0.5)^2);
 qx(end+1)=x; %add this point's x coordinate to the qx array
 qy(end+1)=y; %add this point's y coordinate to the qy array
 qz(end+1)=z; %add this point's z coordinate to the qz array
 Count=Count+1; %increase the number of points in the shape by 1
 end
 else 
 px(end+1)=x; %add this point's x coordinate to the px array
 py(end+1)=y; %add this point's y coordinate to the py array
 pz(end+1)=z; %add this point's z coordinate to the pz array
 end
end


%ratio is the number of points in the shape/total number of points
disp(8*Count/RunLength); %multiply the ratio with 8 and display
%use plot3 command because it is a 3D plot and use ‘ro’ for showing points as
%red circles in the plot
%use plot3 command because it is a 3D plot and use ‘ro’ for showing points as
red circles in the plot
plot3(qx,qy,qz,'ro');