0
$\begingroup$

Using NSolveValues on certain equations returns lists of solutions of the following kind:
enter image description here

For each of these rules I want to select only one value, namely the one absolutely closest to $2\pi/10$, but without selecting this manually. How would I find this?

Edit:
I have many of these equations which depend on a lot of different parameters, so I won't put all of that here. But the point is that I want to solve a large number of equations iteratively, where each equation depends on the solution of the previous one. However each equation results in a list ofinfinite solutions as above, but I only want to use the solution that is absolutely nearest to $2\pi/10$.

f[x_]:=ArcCos[0.5 (-0.00756467 + 
     0.749491 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
     0.503279 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) + 
     0.538571 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
     0.430085 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
     0.0857465 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) - 
     0.838206 (-0.423397 Cos[45. x] + 
        0.841604 Sin[45. x]))] ((0.566265 (0. + 
        0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
        0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
        0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
        0.538571 (0.227498 Cos[45. x] + 
           0.457042 Sin[45. x])))/(\[Sqrt](Abs[
         0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
          0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
          0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
        Abs[0.112051 + 
          0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
          0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
          0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
        Abs[0.050167 - 
          0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
          0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
          0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
          0.538571 (-0.423397 Cos[45. x] + 
             0.841604 Sin[45. x])]^2)) + (0.336803 (0.112051 + 
        0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
        0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
        0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
        0.0857465 (-0.423397 Cos[45. x] + 
           0.841604 Sin[45. x])))/(\[Sqrt](Abs[
         0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
          0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
          0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
        Abs[
         0.112051 + 
          0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
          0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
          0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
        Abs[0.050167 - 
          0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
          0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
          0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
          0.538571 (-0.423397 Cos[45. x] + 
             0.841604 Sin[45. x])]^2)) - (0.752268 (0.050167 - 
        0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
        0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
        0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
        0.538571 (-0.423397 Cos[45. x] + 
           0.841604 Sin[45. x])))/(\[Sqrt](Abs[
         0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
          0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
          0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
        Abs[0.112051 + 
          0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
          0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
          0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
          0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
        Abs[0.050167 - 
          0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
          0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
          0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
          0.538571 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2)))

NSolveValues[f[x] == 2 \[Pi]/10, x, Reals]
$\endgroup$
8
  • $\begingroup$ Can you put the code that produced this result? $\endgroup$
    – polfosol
    Dec 9, 2021 at 9:57
  • $\begingroup$ @polfosol Thank you for your comment, please see my edit. $\endgroup$ Dec 9, 2021 at 10:04
  • $\begingroup$ Also I was thinking about using something like NearestTo, but that doesn't work for the kind of object that I have from NSolveValues. $\endgroup$ Dec 9, 2021 at 10:08
  • $\begingroup$ What is the meaning of argument 45. x ? $\endgroup$ Dec 9, 2021 at 10:28
  • 2
    $\begingroup$ You could try to give an additional constrain like: NSolveValues[{f[x] == 2 \[Pi]/10, (x - 2 Pi/10)^2 < .005}, x] $\endgroup$ Dec 9, 2021 at 10:49

2 Answers 2

2
$\begingroup$

If I understand you right, you are looking for a solution f[x]==2Pi/10 with constraint (x-2Pi/10)^2 minimal?

Try NMinimize

 mini=NMinimize[{(x - 2 Pi/10)^2, f[x] == 2 Pi/10}, x]
 (*{0.0327761, {x -> 0.80936}}*)    
$\endgroup$
3
  • $\begingroup$ Thank you for your answer, I tried it however it as you say it give 0.80936, but there is another solution of f[x]==2Pi/10, which is 0.669734. Shouldn't it return that instead? $\endgroup$ Dec 9, 2021 at 10:33
  • 1
    $\begingroup$ Interessting, don't know why! If you restrict the solution range mini = NMinimize[{(x - 2 Pi/10)^2, f[x] == 2 Pi/10, 0.6 < x < 1 }, x ] the correct result follows $\endgroup$ Dec 9, 2021 at 10:53
  • $\begingroup$ Or FindRoot[f[x] == 2 Pi/10, {x, 2 Pi/10}] seems to be more reliable. $\endgroup$
    – Akku14
    Dec 9, 2021 at 15:48
2
$\begingroup$

Since the desired root is near 2Pi/10 use FindRoot with an initial estimate of 2Pi/10

Clear["Global`*"]

f[x_] = ArcCos[
     0.5 (-0.00756467 + 
        0.749491 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
        0.503279 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) + 
        0.538571 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
        0.430085 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
        0.0857465 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) - 
        0.838206 (-0.423397 Cos[45. x] + 
           0.841604 Sin[45. x]))] ((0.566265 (0. + 
           0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
           0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
           0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
           0.538571 (0.227498 Cos[45. x] + 
              0.457042 Sin[45. x])))/(\[Sqrt](Abs[
             0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
              0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
              0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
           Abs[
             0.112051 + 
              0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
              0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
              0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
           Abs[
             0.050167 - 
              0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
              0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
              0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
              0.538571 (-0.423397 Cos[45. x] + 
                 0.841604 Sin[45. x])]^2)) + (0.336803 (0.112051 + 
           0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
           0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
           0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
           0.0857465 (-0.423397 Cos[45. x] + 
              0.841604 Sin[45. x])))/(\[Sqrt](Abs[
             0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
              0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
              0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
           Abs[
             0.112051 + 
              0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
              0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
              0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
           Abs[
             0.050167 - 
              0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
              0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
              0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
              0.538571 (-0.423397 Cos[45. x] + 
                 0.841604 Sin[45. x])]^2)) - (0.752268 (0.050167 - 
           0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
           0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
           0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
           0.538571 (-0.423397 Cos[45. x] + 
              0.841604 Sin[45. x])))/(\[Sqrt](Abs[
             0. + 0.503279 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) - 
              0.749491 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.0857465 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) - 
              0.538571 (0.227498 Cos[45. x] + 0.457042 Sin[45. x])]^2 + 
           Abs[
             0.112051 + 
              0.430085 (0.457042 Cos[45. x] - 0.227498 Sin[45. x]) - 
              0.503279 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) - 
              0.838206 (0.227498 Cos[45. x] + 0.457042 Sin[45. x]) + 
              0.0857465 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2 + 
           Abs[
             0.050167 - 
              0.430085 (0.287778 Cos[45. x] - 0.876915 Sin[45. x]) + 
              0.838206 (0.876915 Cos[45. x] + 0.287778 Sin[45. x]) + 
              0.749491 (0.841604 Cos[45. x] + 0.423397 Sin[45. x]) + 
              0.538571 (-0.423397 Cos[45. x] + 0.841604 Sin[45. x])]^2))) // 
   Rationalize[#, 0] & // Simplify

(* -((ArcCos[(1/
      2000000000000)(-7564670000 + 1615335655046 Cos[45 x] - 
        1179276556501 Sin[45 x])] (-84197000000 + 
       1139633204749573157 Cos[45 x] + 
       1561033413550243039 Sin[45 x]))/(500000 \[Sqrt](Abs[
         224102000000 - 907979420789 Cos[45 x] - 1243722090718 Sin[45 x]]^2 + 
        Abs[790864917931 Cos[45 x] + 1083301448052 Sin[45 x]]^2 + 
        4 Abs[
          50167000000 + 1014011691237 Cos[45 x] + 
           1388960982854 Sin[45 x]]^2))) *)

Solving,

sol = FindRoot[f[x] == 2 Pi/10, {x, 2 Pi/10}]

(* {x -> 0.669734} *)

Graphically,

Plot[f[x] - 2 Pi/10, {x, 0.4, 0.9},
 PlotPoints -> 200,
 MaxRecursion -> 5,
 GridLines -> {{2 Pi/10}, None},
 GridLinesStyle -> Directive[Gray, Thick, Dashed],
 Epilog -> {Red, AbsolutePointSize[4], Point[{x /. sol, 0}]}]

enter image description here

$\endgroup$

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.