2
$\begingroup$

Consider the following Green Function of wire turn producing magnetic field:

Br[R_, Z_, {r_, z_}]= ((z - Z) (-(((r^2 + R^2 + (z - Z)^2) EllipticE[-((
           4 r R)/((r - R)^2 + (z - Z)^2))])/(
        r^2 + 2 r R + R^2 + (z - Z)^2)) + 
       EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r - 
        R)^2 + (z - Z)^2])

It is evaluated to Indeterminate at R=0. However Limit[Br[R,Z,{r,z}],R->0,Direction->"FromAbove"] gives 0 as it should be.

What is the best way to modify/append the definition of Br[R, Z, {r, z}] to avoid this issue. Simple

Br[0, Z_, {r_, z_}]=0;
Br[0., Z_, {r_, z_}]=0;

does not always work, e.g., in case of Br[R, Z, {r, z}]/.R->0.

In similar cases I also tryied

Br[R_, Z_, {r_, z_}]/;R>0 = ...
Br[0, Z_, {r_, z_}] = 0;
Br[0., Z_, {r_, z_}] = 0;

But I am not sure that this is recommended soluttion.

$\endgroup$

3 Answers 3

2
$\begingroup$
Clear[Br]
Br[R_ /; R == 0, Z_, {r_, z_}] = 0
Br[R_ /; R != 0, Z_, {r_, z_}] = 
 Abs[Sign[R]] ((z - 
       Z) (-(((r^2 + 
              R^2 + (z - 
                 Z)^2) EllipticE[-((4 r R)/((r - R)^2 + (z - 
                    Z)^2))])/(r^2 + 2 r R + R^2 + (z - Z)^2)) + 
       EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r - 
          R)^2 + (z - Z)^2])

Br[0, 3, {1/2, 2}]
Br[0., 3, {1/2, 2}]
Br[R, 3, {1/2, 2}] /. R -> 0
Br[R, 3, {1/2, 2}] /. R -> 0.

(* 0 *)
(* 0 *)
(* 0 *)
(* 0 *)
$\endgroup$
1
$\begingroup$

Try an IF statement like:

Br[R_, Z_, {r_, z_}] = 
 If[R == 0, 
  0, ((z - 
       Z) (-(((r^2 + 
              R^2 + (z - 
                 Z)^2) EllipticE[-((4 r R)/((r - R)^2 + (z - 
                    Z)^2))])/(r^2 + 2 r R + R^2 + (z - Z)^2)) + 
       EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r - 
          R)^2 + (z - Z)^2])]

Br[R, Z, {r, z}] /. R -> 0

0
$\endgroup$
1
  • $\begingroup$ Not sure, but If can be slower than /; . $\endgroup$ Commented Sep 2, 2023 at 7:06
1
$\begingroup$

I'd try a series expansion at $R=0$ combined with Piecewise:

BBr[R_, Z_, {r_, z_}] = 
  Piecewise[{{Series[Br[R, Z, {r, z}], {R, 0, 1}] // FullSimplify // 
 Normal, R < 10^-12}},
            Br[R, Z, {r, z}]]

enter image description here

Maybe the condition on $R$ should depend on the other parameters and not be picked absolutely: for example, R < 10^-12 * Z.

$\endgroup$
3
  • $\begingroup$ Once I tested various methods. Piecewise was the slowest solution if I remember right. $\endgroup$ Commented Sep 2, 2023 at 7:47
  • $\begingroup$ My main point here was the use of a series expansion instead of only plugging the hole at $R=0$. You can combine this with If or with pattern matching or with a Dispatch however you prefer. $\endgroup$
    – Roman
    Commented Sep 2, 2023 at 8:02
  • $\begingroup$ It really depends on your use case. If is a logical clause and may get evaluated too early; Piecewise is a mathematical clause and will evaluate much later. $\endgroup$
    – Roman
    Commented Sep 2, 2023 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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