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:

enter image description here


  • 1
    $\begingroup$ 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] $\endgroup$ – Rahul Apr 9 '14 at 18:10
  • $\begingroup$ I love you! it works $\endgroup$ – 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.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.