denominator
denominator

Reputation: 181

Finding the largest roots of a polynomial in R

Is there a way to avoid the complex numbers in

polyroot {base} ?

The help file says: "…polyroot returns the n-1 complex zeros", but I simply need the largest root of a quadratic equation and the complex numbers are too much of a good thing here. Help is much appreciated!

Upvotes: 1

Views: 657

Answers (2)

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

If you get complex solutions for a quadratic equation, then there are no real roots.

The largest root of the equation x²+px+q is

(-p+sqrt(p*p-4*q))/2

if the term inside the square root is non-negative.

Or did you mean a root of largest magnitude?

Upvotes: 1

Shawn Mehan
Shawn Mehan

Reputation: 4578

When you use this you set the return against an object, say

roots=polyroot(coefs)

and you can then return the real ones by excluding the imaginary ones via complex::Im()

which(Im(roots)==0)

Upvotes: 1

Related Questions