0
$\begingroup$

Find the largest real root and the largest real part of a root of the polynomial below. Clearly indicate which is which.

$$f(x) = 5 -\sum_{k=1}^{100}k^2 x^k$$

To get all roots, solve for $f(x) = 0$.

f[x_] := 5 - Sum[k^2 x^k, {k, 1, 100}]
NSolve[f[x] == 0, x]

How can I select:

  1. The largest real root?

  2. The largest real part of a root?

My thinking is something along the lines of

Select[roots, {Element[roots, Reals]}]
$\endgroup$
3
  • $\begingroup$ Thank you! But how would you adjust this to find the largest real root? $\endgroup$ Commented Apr 6, 2018 at 18:18
  • 1
    $\begingroup$ sols = x /. Solve[5 - Sum[k^2 x^k, {k, 1, 100}] == 0, x]; Max[N@Re[sols]] and rsols = x /. Solve[5 - Sum[k^2 x^k, {k, 1, 100}] == 0, x, Reals]; Max[N@Re[rsols]] . For a wider perspecitve see e.g. this answer. $\endgroup$
    – Artes
    Commented Apr 6, 2018 at 18:30
  • 2
    $\begingroup$ If an approximate root is acceptable, could do In[24]:= Max[Cases[x /. NSolve[f[x] == 0, x], _Real]] Out[24]= 0.478778284362 and In[26]:= Max[Re[x /. NSolve[f[x] == 0, x]]] Out[26]= 0.959209369278. $\endgroup$ Commented Apr 6, 2018 at 19:56

2 Answers 2

4
$\begingroup$

Solve conveniently gives the complete list in terms of Root objects, ordered by their real parts, from least to most, with all complex roots following the real ones. So,

First[-x /. Solve[f[-x] == 0, x]] // N
(* 0.478778 *)

gets you the largest real root. It uses -x to reverse the ordering to put the largest root first. Then,

Re[Last[x /. Solve[f[x] == 0, x]]] // N
(* 0.959209 *)

yields the real part of the complex root with the largest real part. No reversal for this case.

Leave out // N for exact algebraic results (long Root expressions).

$\endgroup$
1
  • 1
    $\begingroup$ Without reversal, x /. Solve[f[x] == 0, x, Reals][[-1]] // N For the largest real part of a root, Root[f[x], 100] // N // Re $\endgroup$
    – Bob Hanlon
    Commented Apr 6, 2018 at 20:31
1
$\begingroup$

Another way to proceed is to use CountRoots[] + RootIntervals[] for finding the real roots:

CountRoots[5 - Sum[k^2 x^k, {k, 1, 100}], {x, -∞, ∞}]
   2

which tells us that there are two real roots, and

RootIntervals[5 - Sum[k^2 x^k, {k, 1, 100}]]
   {{{-1, 0}, {0, 1}}, {{1}, {1}}}

which tells us that the largest real root is within $(0,1)$; thus:

x /. First[NSolve[5 - Sum[k^2 x^k, {k, 1, 100}] == 0 && 0 < x < 1,
                  x, WorkingPrecision -> 25]]
   0.4787782843622795018143755

For the case of getting the root with the largest real part, we can again use RootIntervals[] to find isolating intervals, and then pick out the most extreme bounds:

recs = First[RootIntervals[5 - Sum[k^2 x^k, {k, 1, 100}], Complexes]];

cand = MaximalBy[Select[recs, Im[Last[#]] > 0 &], Re @* Last]
   {{7/8, 1 + I/8}, {15/16 + I/8, 1 + I/4}, {3/4 + I/2, 1 + I}}

which leaves us with three candidates, instead of a hundred. From there:

(x /. First[NSolve[5 - Sum[k^2 x^k, {k, 1, 100}] == 0 &&
                   #[[1, 1]] < Re[x] < #[[2, 1]] &&
                   #[[1, 2]] < Im[x] < #[[2, 2]], x, 
                   WorkingPrecision -> 25]]) & /@ ReIm[cand]
   {0.9592093692777315093832973 + 0.0800374424094000451900130 I, 
    0.9423637156463657775951992 + 0.1412604810949637296158256 I, 
    0.7745164783087352268927909 + 0.5155444841877413797413391 I}

and it is clear that the first root returned has the largest real part.

$\endgroup$

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.