Physics Stack Exchange is a question and answer site for active researchers, academics and students of physics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a program in cpp that computes the magnetic field at a point from a current carrying loop. It uses the biot savart law and the monte carlo technique to carry out the integral. The program functions as expected for points that are not too close to the loop but from looking at a plot of the vectors you can see strange errors close to the loop.

I checked my results against for a point on the axis and for a point off the axis. My program agrees with these sources for points not too close to the loop.

This is my code.

My main question is if there is anything inherent in the monte carlo technique that would cause this error?

share|cite|improve this question
up vote 4 down vote accepted

If you're very close to the loop, there is a small range of theta that provides almost all of the field. When you're randomly choosing points, a relatively small fraction of those points will happen to be in that crucial range. This will amplify the random sampling error. In other words, the closer you get to the loop, the larger the number of sample points required in order to get a reasonable accuracy.

I suggest to increase the number of sample points and see if the answer gets more accurate. If not, then probably you have a formula error or coding error.

If the number of sample points required is too large, maybe uniform random sampling is a bad strategy. You can use a modified Monte Carlo technique where there is a weighting function, making some theta's more likely to occur than others. Or just don't use Monte Carlo. :-)

share|cite|improve this answer
Thank you that I ran the simulation overnight and got better results so you are probably right. If I have time I will try a weighting function, thanks again. – user1750289 Nov 21 '12 at 8:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.