4
$\begingroup$

I want to eventually solve this with these three parameters being given from a list or table. It is not returning a solution, but I know that there is a 0 of this function (from looking at the plot)

NSolve[-((2 #2^2 #1 (1 - BesselK[0, r #3]/BesselK[0, #2 #3])^2)/
  r^3) + (2 (1 + #2^2/r^2) #1 #3 (1 - BesselK[0, r #3]/
    BesselK[0, #2 #3]) BesselK[1, r #3])/BesselK[0, #2 #3] == 0.0,
r] &[0.1, 5, 1.5]

What am I doing wrong?

$\endgroup$
  • 2
    $\begingroup$ I would try using FindRoot instead, which is designed for general numerical root-finding. Where NSolve works, FindRoot is still likely to be faster (I think). You have to provide a guess, however. FindRoot[yourEquation, {r, 4}] &[0.1, 5, 1.5] $\endgroup$ – march Apr 20 '17 at 22:40
5
$\begingroup$

If you ask Solve to work over a finite domain, than it can use interval techniques to find all roots in the domain:

Solve[
    -((2 #2^2 #1 (1-BesselK[0,r #3]/BesselK[0,#2 #3])^2)/r^3)+
    (2 (1+#2^2/r^2) #1 #3 (1-BesselK[0,r #3]/BesselK[0,#2 #3]) 
    BesselK[1,r #3])/BesselK[0,#2 #3]==0 && 0<r<10, r
]&[.1,5,1.5]

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

{{r -> 5.}, {r -> 7.27748}}

$\endgroup$
2
$\begingroup$

For (non-elementary) transcendental equations, you usually have to specify a finite domain, as Carl Woll advises. But NSolve suffers from the same bug in V11.1 in this case as found in Backslide in NSolve in V11.1?.

Reduce`RealTNRoots;
nonElementaryQ[f_] := Module[{x}, ! ListQ[Simplify`FunctionSingularities[f[x], x, "ELEM"]]]
Internal`InheritedBlock[{System`TRootsDump`NIntervalRoots},
 System`TRootsDump`NIntervalRoots[f_?nonElementaryQ, ii_, prec_] := $Failed;
 NSolve[-((2 #2^2 #1 (1 - BesselK[0, r #3]/BesselK[0, #2 #3])^2)/
          r^3) + (2 (1 + #2^2/r^2) #1 #3 (1 - 
            BesselK[0, r #3]/BesselK[0, #2 #3]) BesselK[1, r #3])/
        BesselK[0, #2 #3] == 0.0 && 0 < r < 10, r] &[0.1, 5, 1.5]
 ]
(*  {{r -> 5.}, {r -> 7.27748}}  *)

This fix is borrowed from this answer; the other answer works as well, but with a warning.

$\endgroup$
2
$\begingroup$
eq = -((5. (1 - 4013.201565307701 BesselK[0, 1.5 r])^2)/r^3) + 1203.9604695923106 
       (1 + 25/r^2) (1 -4013.201565307701 BesselK[0, 1.5 r]) BesselK[1, 1.5 r];

Plot[eq == 0, {r, -10, 10}, PlotRange -> {{4, 10}, {-0.1, 0.15}}]

enter image description here

NSolve[{eq == 0 && (4 <= r <= 8)}, r]

{{r -> 5.}, {r -> 7.27748}}

Plot[eq, {r, 4, 10}, Epilog -> {Red, PointSize[.03],  Point[{r, eq}] 
         /. NSolve[{eq == 0 && (4 <= r <= 8)}, r]}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Since you're using Plot[] anyway: plt = Plot[eq, {r, -10, 10}, Mesh -> {{0}}, MeshFunctions -> {#2 &}, MeshStyle -> Directive[Red, PointSize[.03]], PlotRange -> {{4, 10}, {-0.1, 0.15}}]; Cases[Normal[plt], Point[{x_, y_}] :> x, ∞] $\endgroup$ – J. M. will be back soon Apr 21 '17 at 4:10

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.