Clear Filters
Clear Filters

volumeofsphere matlab function need help

1 view (last 30 days)
gracias claude
gracias claude on 11 Apr 2021
Edited: dpb on 11 Apr 2021
I put 10^4 into the function and I should be expecting Number of points inside sphere to be 3 and the area should be 0.0024. Am I missing something if so what do I need to change ?
function volumeofsphere3(n)
ds = RandStream.getGlobalStream;
reset(ds);
x=2*rand(n,1);
y=rand(n,1);
z=rand(n,1);
count=0
for i=1:n
r=sqrt((x(i)-1/3)^2+(y(i)-1/3)^2+(z(i)-1/2)^2);
if r<=0.04
count=count+1;
end
end
fprintf(1,'Number of points inside sphere: %d\n', count)
Area_of_Sphere=8*(count/n)
end

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!