This is probably a simple question. I have a for loop that I use to sum the effects of charges at a test point. The test points are in one list and the charge locations are in another list. The code below illustrates a basic case.
Each item in chargeList contains the coordinate of the charge and the charge. chargeList[[j]] = {x,y,z,q}. Each item in testPointList contains the coordinates of the test point. testPointList[[i]]={x,y,z}.
For each test point, I need to sum chargeList[[j]][[4]] divided by the distance of each charge to the test point. While the procedural nested For[…] loop approach works, it can be very slow for large lists.
chargeList = Table[{-3, yPos, 1.54, 1}, {yPos, -3, 3, 0.5}]
testPointList =
Table[ {1.34 Cos[\[Theta]], 1.34 Sin[\[Theta]], 1.54}, {\[Theta], 0,
2 Pi, Pi/10}]
Clear[zField];
pointCount = Length[testPointList];
chargeCount = Length[chargeList];
zField = {};
For[i = 1, i <= pointCount, i++,
zPot = 0;
For[j = 1, j <= chargeCount, j++,
zPot +=
chargeList[[j]][[4]]/
Sqrt[(testPointList[[i]][[1]] -
chargeList[[j]][[1]])^2 + (testPointList[[i]][[2]] -
chargeList[[j]][[2]])^2 + (testPointList[[i]][[3]] -
chargeList[[j]][[3]])^2]]
AppendTo[
zField, {testPointList[[i]][[1]], testPointList[[i]][[2]],
zPot}]];