4
$\begingroup$

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 http://hyperphysics.phy-astr.gsu.edu/hbase/magnetic/curloo.html#c3 for a point on the axis and http://www.netdenizen.com/emagnet/offaxis/iloopcalculator.htm for a point off the axis. My program agrees with these sources for points not too close to the loop.

This is my code. http://pastebin.com/4BH9qEZz

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

$\endgroup$
4
$\begingroup$

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. :-)

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user1750289 Nov 21 '12 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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