It's time for another physics example. In this case, I am going to calculate the electric field due to an electric charged rod. Of course you could do this analytically using a bit of calculus. This is a fairly standard example in most introductory physics textbooks. Here is an example where I calculate the electric field along the same axis as the rod.

But what if you want to find the electric field at any point? For instance, like this:

You can set up an integral to determine electric field at that point, but it won't be easy to evaluate. But the cool thing is that both the analytical and numerical methods in this case use the same idea. In both cases, you will break the charged rod into a whole bunch of tiny pieces. The electric field due to each of these tiny pieces is just like the electric field due to a point charge (if the pieces are small enough). Then the total electric field at the point of interest is just the same of the tiny electric fields due to the tiny pieces of the rod. Really, the only difference is that in the analytical method you take the limit as the piece size approaches zero.

Ok, let's set up a numerical method for calculating the electric field due to the rod. Here is the recipe.

- Break the rod into
*N*pieces (where you can change the value of*N*). - For each tiny little piece, calculate the charge and the position. The charge of each piece would just be
*Q/N*. - Find the vector that goes from each piece of the rod to the point where you want to find the electric field.
- Use the equation for the electric field to find the contribution to the total electric field due to each piece.
- Add up all the contributions to the electric field due to all the pieces.

That's it. It's really not too complicated. In fact, you don't even need a computer to do this. If you preak the rod into 10 pieces, you could easily calculate the field due to each of these 10 pieces. Of course if you want to break it into 100 pieces, the calculations still might not be difficult, but the process might drive you insane.

Before getting into the program, let's say that I want to find the electric field at some vector location *r*_{o}. Here is how you would calculate the electric field due to one of the pieces.