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.

1 Answer
1

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