Can you include Integrate[] in a function definition?

Hi guys!

This is truly the last resort, tried everything and searched a lot, wouldn’t bother you otherwise.

When I try to use the last two functions in the included code mathematica gives me a neverending evaluation, I truly don’t know what I am doing wrong. Would really appreciate some enlightening!

PS: in case you were wondering, this is Biot-Savart law, I am trying to calculate the magnetic field caused by a infinite slab of thickness t and width w with current density J

(* just some constants*)    
w = 5*10^-6;
t = 3*10^-7;
d = 20*10^-6;
J = (30*10^-3)/(w*t);
\[Mu] = 4*Pi*10^-7;

(* some normal functions *)    
r[x_, y_, z_, a_, b_, c_] := ((a - x)^2 + (b - y)^2 + (c - z)^2)^(1/2)

\[Theta][x_, y_, z_, a_, b_, c_] := (Pi/2) - 
ArcSin[(y - b)/r[x, y, z, a, b, c]]

\[Alpha][x_, y_, z_, a_, b_, c_] := 
ArcSin[(a - x)/r[x, y, z, a, b, c]]


(* THESE NEXT TWO ARE THE ONES WITH PROBLEMS: *)

Bx[x_, y_, z_] := ((\[Mu]*J)/(4*Pi))*
Integrate[(Sin[\[Theta][x, y, z, a, b, c]]*
Cos[\[Alpha][x, y, z, a, b, c]])/(r[x, y, z, a, b, 
c])^2, {a, -w/2, w/2}, {b, -\[Infinity], \[Infinity]}, {c, -t/2,
t/2}]

Bz[x_, y_, z_] := ((\[Mu]*J)/(4*Pi))*
Integrate[(Sin[\[Theta][x, y, z, a, b, c]]*
Sin[\[Alpha][x, y, z, a, b, c]])/(r[x, y, z, a, b, 
c])^2, {a, -w/2, w/2}, {b, -\[Infinity], \[Infinity]}, {c, -t/2,
t/2}]

Bz[1, 1, 1] (* this gives neverending evaluation for example*)

It’s probably having a difficult time with the symbolic integral. You could use a numerical method with NIntegrate:

In[10]:= 
Bz[x_, y_, z_] := ((\[Mu]*J)/(4*Pi))*
  NIntegrate[(Sin[\[Theta][x, y, z, a, b, c]]*
      Sin[\[Alpha][x, y, z, a, b, c]])/(r[x, y, z, a, b, 
       c])^2, {a, -w/2, 
    w/2}, {b, -\[Infinity], \[Infinity]}, {c, -t/2, t/2}]

In[11]:= 
Bz[1, 1, 1] (*this gives neverending evaluation for example*)

Out[11]= -2.35619*10^-9

Thank you David, but when using NIntegrate I get this error.

NIntegrate::inumr: The integrand (Sqrt[1-(a-x)^2/(Plus[<<2>>]^2+Plus[<<2>>]^2+Plus[<<2>>]^2)] Sqrt[1-(-b+y)^2/(Plus[<<2>>]^2+Plus[<<2>>]^2+Plus[<<2>>]^2)])/((a-x)^2+(b-y)^2+(c-z)^2) has evaluated to non-numerical values for all sampling points in the region with boundaries {{-(1/400000),1/400000},{-\[Infinity],0.},{-(3/20000000),3/20000000}}. >>

And when I then try Bz[0, 0, 100*10^-9] ( which is a more realistic value for my work than 1,1,1) it tells me the following:

Numerical integration converging too slowly; suspect one of the \
following: singularity, value of the integration is 0, highly \
oscillatory integrand, or WorkingPrecision too small. >>

The odd integrand (Sin[\[Theta][0,0,1/10000000,a,b,c]] \
    Sin[\[Alpha][0,0,1/10000000,a,b,c]])/r[0,0,1/10000000,a,b,c]^2 is \
    being considered as zero over the specified region, but may actually \


be divergent. >>

Hi Joao, I suspect the convergence problem occurs for field points inside your slab, where there are nonzero source values. There the calculation of integral values includes cancellations of very large but opposite contributions of nearby source points. I think I see reasonable behavior outside the slab. Please see the attached notebook.

There are contributors to this forum who know a lot more about NIntegrate than I – maybe one of them will make a comment.

Best, David

You are right David, I was quite stupid actually, totally forgot the z axis starts in the middle of the slab and not on the top, so a value such as Bz[0, 0, 10010^-9] would be inside it! Will use Bz[0, 0, 10010^-9 + t/2] from now on ahah.

Still not quite sure why Integrate doesn’t work, but since NIntegrate seems to I’ll just roll with it!

Thanks for your invaluable help David!

PS: this is Joao M