# Finding the roots of a polynomial that meet specified conditions

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]}]

• Thank you! But how would you adjust this to find the largest real root? – LightningStrike Apr 6 '18 at 18:18
• 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. – Artes Apr 6 '18 at 18:30
• If an approximate root is acceptable, could do In:= Max[Cases[x /. NSolve[f[x] == 0, x], _Real]] Out= 0.478778284362 and In:= Max[Re[x /. NSolve[f[x] == 0, x]]] Out= 0.959209369278. – Daniel Lichtblau Apr 6 '18 at 19:56

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

• 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 – Bob Hanlon Apr 6 '18 at 20:31

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.