# NSolve non-analytic function

l1 = 0.167;
l2 = 0.078596;
θ = π/18;

NSolve[θ == ArcTan[(1/Sqrt[k] Sin[Sqrt[k] l1] +
l2 Cos[Sqrt[k] l1])/(-l2 Sqrt[k] Sin[Sqrt[k] l1] +
Cos[Sqrt[k] l1])], k]


How to get the solution of the following above?

• suggest you plot θ - ArcTan, you'll see you have many roots. FindRoot gives you one at a time. – george2079 Jul 14 '15 at 21:01

There may infinitely many solutions. This will find some, if you limit the range on k:

Block[{θ = π/18, l1 = 0.167, l2 = 0.078596},
NSolve[θ ==
ArcTan[(1/Sqrt[k] Sin[Sqrt[k] l1] +
l2 Cos[Sqrt[k] l1])/(-l2 Sqrt[k] Sin[Sqrt[k] l1] +
Cos[Sqrt[k] l1])] && 0 < k < 10000, k]
]


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

(* {{k -> 421.101}, {k -> 1494.26}, {k -> 3266.71}, {k -> 5745.09}, {k -> 8930.65}} *)


There is a negative root that is hard for NSolve to find. If we use FindRoot, it's simple:

Block[{θ = π/18, l1 = 0.167, l2 = 0.078596},
FindRoot[θ ==
ArcTan[(1/Sqrt[k] Sin[Sqrt[k] l1] +
l2 Cos[Sqrt[k] l1])/(-l2 Sqrt[k] Sin[Sqrt[k] l1] +
Cos[Sqrt[k] l1])], {k, -50}]
]
(*  {k -> -21.7265 + 0. I}  *)


You can see one problem is that negative values of k make the function values complex. Note that I killed the corresponding NSolve command with the restriction -50 < k < 0 after several minutes when it had reached 6+GB of memory usage.

• In FindRoot use l1 = 0.16715, l2 = 0.07859615 and WorkingPrecision->15 to remove complex result. – Bob Hanlon Jul 14 '15 at 23:48
• @BobHanlon Neat. I didn't expect that. Usually once complex numbers arise, the stick around, even if the imaginary part comes out zero. – Michael E2 Jul 14 '15 at 23:52
• I just ran your codes,but a problem occurred when I plug theta = pi/18 *10. Basically, I want to get the array of K value when theta is from pi/18*1, pi/18*2, ..., pi/18*18. – Lawerance Jul 16 '15 at 1:56
• @Lawerance Since ArcTan for real arguments is "always in the range -Pi/2 to Pi/2" (docs), there should be no solutions when theta is Pi/18 * 10, which is what I get. – Michael E2 Jul 16 '15 at 3:32