# i am new. i need help. kindly answer.

1 view (last 30 days)
Rana zain on 28 Nov 2020
Commented: Rana zain on 28 Nov 2020
I want to ask is there a way to write a code to integrate the function f(x,y,z) over a sphere using Monte-Carlo method in three dimensions.

Image Analyst on 28 Nov 2020
I'd say the answer is "Yes". If you know the algorithm/steps and just need to know the MATLAB syntax, you can use these links to gain that knowledge of syntax so that you can put your ideas into actual code:
John D'Errico on 28 Nov 2020
With some effort, I can certainly think of problems where only Monte Carlo makes sense. But the number of times I have encountered such a problem over my career is zero. And that suggests this is homework.
Rana zain on 28 Nov 2020
Thanks guys i got it.

John D'Errico on 28 Nov 2020
Edited: John D'Errico on 28 Nov 2020
If this is homework? It very possibly is, because there are relatively few good reasons I can think of to solve any integration problem over a sphere using Monte Carlo. So if it is homework, then you need to do your own homework. At least make some effort.
Regardless, even if this is homework, it is still trivial. Just generate points uniformly distributed over the sphere. Then add up function values at those points. (Be careful to scale by the volume of the sphere.) WTP? I can think of several ways to generate uniformly distributed random points inside a sphere. You should even be able to use a weighted Riemann sum, if your generation scheme was not uniform.
The problem is, Monte Carlo is not very efficient as a method. You will gain far more accuracy in your result by using any of a variety of schemes for integration over a simple domain.
The only circumstance when I could see using Monte Carlo on a spherical domain is if your function is itself strongly discontinuous over the domain of the sphere, with many discontinuities, derivative singularities, etc.
So in any case, you need to convince me this is not homework. And then you need to convince me why it is that you think you really need to use Monte Carlo. I won't spend the time telling you how to solve a problem poorly.

#### 1 Comment

Rana zain on 28 Nov 2020
Thanks for your help. i solved it myself. I just needed a start and confirmation if this is possible on MATLAB.