# Can't compute the Integral

I tried to calculate an integral with the function Integrate, but it runs for a long long time without any answer. I have been checking the integrand and it seems to be fine. And even stranger, sometimes Mathematica could answer and other times it couldn't. Can somebody help me? I have been searching questions of the same type and I found plenty, but I haven't been able to do it.

f1[x_, y_, z_] =
Integrate[
Cos[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/
2), {a, 0, 2*Pi}]

f2[x_, y_, z_] =
Integrate[
Sin[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/
2), {a, 0, 2*Pi}];
f3[x_, y_, z_] =
Integrate[(r - y*Sin[a] -
x*Cos[a])/(x^2 + y^2 + z^2 + r^2 -
2*r*(x*Cos[a] + y*Sin[a]))^(3/2), {a, 0, 2*Pi}];


And to do the plot i do this

VectorPlot3D[{(μ*i*r*z/(4*Pi))*
N[f1[x, y, z]], (μ*i*r*z/(4*Pi))*
N[f2[x, y, z]], (μ*i*r/(4*Pi))*N[f3[x, y, z]]}, {x, -0.5,
0.5}, {y, -0.5, 0.5}, {z, -0.5, 0.5}]


The equation corresponds to the magnetic field at a given point created by a spiral (the component x of course).

• Are you okay with a numerical integral? Change it to: f1[x_, y_, z_, r_] := NIntegrate[ Cos[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/ 2), {a, 0, 2*Pi}] which works much better for example: f1[0.8, 0.5, 1.3, .2] returns instantly. – flinty Nov 8 '20 at 14:34
• The trapezoidal strategy is a good choice for numerical integration, if you're confident of using only good input values to f[]. It should save some time if you have to do a lot of integrals.: NIntegrate[Cos[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/2), {a, 0, 2*Pi}, Method -> {"Trapezoidal", "SymbolicProcessing" -> 0}] – Michael E2 Nov 8 '20 at 16:12
• Actualy i have tried NIntegral and it worked much faster. But i was traying to do a vector plot as well, and because after this i do VectorPlot in a certain range i tried to calculate the generic expression so that the plot then subtituts the variable values. In the polot i put N[f[x,y,z]] – Tomás Nov 8 '20 at 16:20
• I tried as well to put the entire expression in the vector plot. Like VectorPlot[{Nintegrate[f[x]]},{x,-1,1}}] (offcourse with the other components), but ii am afried that he will compute the integral i all the interactions. – Tomás Nov 8 '20 at 16:24
• @Tomás if you are trying to get a vector plot you need a derivative which is a vector. Your f1 does not produce a vector but a scalar. You need partial derivatives for each cartesian direction. – flinty Nov 8 '20 at 17:05

Use NIntegrate and pattern test the arguments to f1,f2,f3 with NumericQ to ensure NIntegrate does not do any symbolic preprocessing first. You do not need N as it has no effect here. I've given $$\mu$$ and $$i$$ values of 1, but feel free to change them:

f1[x_?NumericQ, y_?NumericQ, z_?NumericQ, r_?NumericQ] :=
NIntegrate[
Cos[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/
2), {a, 0, 2*Pi}];

f2[x_?NumericQ, y_?NumericQ, z_?NumericQ, r_?NumericQ] :=
NIntegrate[
Sin[a]/(x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(3/
2), {a, 0, 2*Pi}];

f3[x_?NumericQ, y_?NumericQ, z_?NumericQ, r_?NumericQ] :=
NIntegrate[(r - y*Sin[a] -
x*Cos[a])/(x^2 + y^2 + z^2 + r^2 -
2*r*(x*Cos[a] + y*Sin[a]))^(3/2), {a, 0, 2*Pi}];

With[{i = 1, μ = 1, r = .1},
VectorPlot3D[{
(μ*i*r*z/(4*Pi))*f1[x, y, z, r],
(μ*i*r*z/(4*Pi))*f2[x, y, z, r],
(μ*i*r/(4*Pi))*f3[x, y, z, r]
}, {x, -0.5, 0.5}, {y, -0.5, 0.5}, {z, -0.5, 0.5}]
]


• wow. that was very fast. I can´t thank you enouf. – Tomás Nov 8 '20 at 19:33

With Version 12.1.1, at least, the integrals can be obtained symbolically, and I obtained the first one in less than an hour. Unfortunately, it is an enormous conditional expression. But, there is an easier way. Beginning with the expression,

fint = (x^2 + y^2 + z^2 + r^2 - 2*r*(x*Cos[a] + y*Sin[a]))^(-1/2);


compute

f1int = Simplify[(D[fint, x] - x D[fint, z]/z)/r]
(* Cos[a]/(r^2 + x^2 + y^2 + z^2 - 2 r x Cos[a] - 2 r y Sin[a])^(3/2) *)

f2int = Simplify[(D[fint, y] - y D[fint, z]/z)/r]
(* Sin[a]/(r^2 + x^2 + y^2 + z^2 - 2 r x Cos[a] - 2 r y Sin[a])^(3/2) *)

f3int = Simplify[-D[fint, r]]
(* (r - x Cos[a] - y Sin[a])/(r^2 + x^2 + y^2 + z^2 - 2 r x Cos[a] - 2 r y Sin[a])^(3/2) *)


The terms f1int, f2int, and f3int are recognizable as the integrands of f1, f2, and f3 in the question. Therefore, the three integrals in the question are simply the corresponding combinations of derivatives of the fint integral, which can be obtained in a few minutes. (The second assumption avoids singularities in f.)

f = Integrate[f, {a, 0, 2*Pi}, Assumptions -> (x | y | z | r) ∈ Reals &&
x^2 + y^2 + z^2 + r^2 + 2 r Sqrt[x^2 + y^2] > 0]
(* (4 EllipticK[(4 r Sqrt[x^2 + y^2])/(r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2)])/
Sqrt[r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2] *)


For instance, the f3 integral, obtained in seconds, is

FullSimplify[-D[f, r]]
(* (2 (-(-r^2 + x^2 + y^2 + z^2) (r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2)
EllipticE[(4 r Sqrt[x^2 + y^2])/(r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2)] +
((-r^2 + x^2 + y^2)^2 + 2 (r^2 + x^2 + y^2) z^2 + z^4) EllipticK[(4 r Sqrt[x^2 + y^2])
/(r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2)]))/(r (r^2 + x^2 + y^2 - 2 r
Sqrt[x^2 + y^2] + z^2) (r^2 + x^2 + y^2 + 2 r Sqrt[x^2 + y^2] + z^2)^(3/2)) *)


The f1 and f2 integrals are computed similarly from

FullSimplify[(D[f, x] - x D[f, z]/z)/r]

FullSimplify[(D[f, y] - y D[f, z]/z)/r]


also in seconds.