# Make a “ditch” function for electric field purpose

I'm trying to make a function to simulate a finite bar and get the electric field, i did it by put 22 funnels together but is too slow and is not very accurate; does anyone have any function to replace this thing?

PD: this function is:

Total[Table[1/Sqrt[x^2 + (y + i)^2], {i, -5, 5, 0.5}]]


Final Result:

Thanks!

-
How about just replacing the summation with an integral? Integrate[1/Sqrt[x^2 + (y + t)^2], {t, -5, 5}, Assumptions -> {x, y} \[Element] Reals] – Rahul Apr 9 '14 at 18:10
I love you! it works – Gonzalo Apr 9 '14 at 18:13

By adding up lots of closely spaced contributions, you are effectively approximating an integral. Just evaluate the integral analytically instead:

f[x_, y_] := Evaluate[Integrate[1/Sqrt[x^2 + (y + t)^2], {t, -5, 5},
Assumptions -> {x, y} \[Element] Reals]]


The assumptions are helpful because otherwise Mathematica assumes $x$ and $y$ to be complex, and spends a much longer time getting to essentially the same result.

(P.S. This probably differs from your expression by roughly a factor of 2. If your expression was really an approximation of the integral, it would have the sum multiplied by the spacing between points, i.e. 0.5 in your case.)

-