Question

Write a MATLAB script using a Monte Carlo method to estimate the volume of a 3-dimensional ball o...

Write a MATLAB script using a Monte Carlo method to estimate the volume of a 3-dimensional ball of radius R=0.5.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

%number of sample space in first quaradant
k=10^5;
%size of volume
R=0.5;


%random x,y,z co-ordinate
x=rand(k,1)*0.6;
y=rand(k,1)*0.6;
z=rand(k,1)*0.6;

%count total points inside sphere
counter=0;
for i=1:k
if ((x(i)^2+y(i)^2+z(i)^2)<=R^2)
counter=counter+1;
end
end

%volume of the cube containing the sphere
a=0.6;
vol_box=a^3;

%volume of cube calculated using monte carlo
vol_sphere=8*(counter/k)*vol_box;

%actual volume of sphere
true_val=(4/3)*pi*(R^3);

%display result and error
fprintf('Approx volume of ball %.6f\n',vol_sphere);
fprintf('True volume of ball %.6f\n',true_val);
fprintf('Error : %.6f\n',abs(vol_sphere-true_val));

output:

Command Window >>progl Approx volume of ball 0.527334 True volume of Erro 0.003735 ball 0.523599

For any queries comment down below. I will be glad to help you.


If you like this answer please hit the like button. :)

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB script using a Monte Carlo method to estimate the volume of a 3-dimensional ball o...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT