# Help understanding this numerical surface integration technique?

I'm attempting to write a FORTRAN program that calculates the magnetic field, B, at any point outside of a bar magnet.

I'm going to use a first order euler scheme, where each side of the bar magnet is split into small cells, each with centres at (xi,yi,zi). I know I can ignore all of the sides that have any z values, and just focus on the top and bottom sides that are orientated in the x-y plane. So the method says this:

$\int f(x,y,z)dS = \Delta S \cdot \sum f(x_{i},y_{i}, z_{i})$

where the integral is over the surface S, and the summation is over i.

Delta S, each area, is given by $\hat{n}\cdot d\vec{S}$ , so if the cell is oriented in the x-y plane it's just $\Delta x\cdot \Delta y$ .

Here is a screenshot of the specific method instructions with a figure that demonstrates it

The function for the magnetic field is this: $\vec{B}(\vec{r}) = \frac{\mu _{0}}{4\pi }\cdot \int\frac{(r-{r}')\cdot M(r)\cdot \hat{n}}{|(r-{r}')|^{3}}$

Where the integral is over the surface S

I'm struggling to understand this method. I've tried to construct a flow chart, but can't get very far so I figured the problem is with the mathematics. Any help to understand it would be appreciated, and also any help with the flow chart would be fantastic. Here is my flowchart

• Cross-posting is discouraged on the StackExchange network, so people don't waste their time with an answer you already received on the other site. The usual procedure is to wait a few days, and then either raise a flag and ask the moderators for migration (if there are some answers already) or delete the old and ask a new question. Croos-post math.stackexchange – Mauro Vanzetto Feb 12 '18 at 14:27

Let us begin with the following 1D integral: $$\int_{x_0}^{x_N}{f(x)\,dx}$$ We know that since the integral is a sum, the sum of the cut integral is equal to the previous one, i.e. $$\int_{x_0}^{x_N}{f(x)\,dx}=\int_{x_1}^{x'}{f(x)\,dx}+\int_{x'}^{x_N}{f(x)\,dx}$$ Where $x'$ is a point such as $x'\in[x_0,x_N]$
Imagine, we cut the integral in $N$ intervals... if this number is big enough we are "seeing" that the function $f(x)$ along a cut is almost a straight line (e.g the Earth is a sphere but from our point of view is a plane). If we do further assumptions, we can say even that the function $f$ in this tiny interval has an almost constant value. Let us name the corresponding value of $f(x)$ in the $i$-th cut $f_i$, thus the cut integral is simplified to: $$\int_{x_0}^{x_N}{f(x)\,dx}=\sum_{i=0}^{N-1}\int_{x_i}^{x_{i+1}}{f(x)\,dx}\approx\sum_{i=0}^{N-1}\int_{x_i}^{x_{i+1}}{f_i\,dx}=\sum_{i=0}^{N-1}f_i\int_{x_i}^{x_{i+1}}{\,dx}=\sum_{i=0}^{N-1}{f_i\Delta x_i}$$ The last step is due the fact that $f_i$ is a constant.
There are some type of functions that decay really quickly. Imagine that $f(x)=\frac{1}{x^2}$ (like in your problem), the integrand will be then: $$\int_{x_0}^{x_N}{\frac{dx}{x^2}}$$ The contributions of the integral are negligible once $x$ is large enough, i.e. the error in the integration is proportional to $1/x$. Therefore you can stop once $x$ is large enough, i.e. it is not necessary to sum the $N$ intervals you have.