Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Test

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

Thanks!

share|improve this question
    
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 Narain Apr 9 at 18:10
    
I love you! it works –  Gonzalo Apr 9 at 18:13

1 Answer 1

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.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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