Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Finding Roots of Polynomials Explained – Python np.roots() Tutorial with Examples

NumPy – Finding Roots of Polynomials 

Finding roots of a polynomial is a fundamental concept in algebra and numerical computing.

Roots are the values of x where the polynomial becomes zero.

Using NumPy, we can easily compute polynomial roots using the built-in function np.roots().

This is widely used in:

  • Mathematics
  • Engineering
  • Physics
  • Data science
  • Machine learning
  • Control systems

What are Polynomial Roots?

A root is a value of x such that:

P(x)=0P(x)=0

For example:

x25x+6=0x^2 - 5x + 6 = 0

Roots are:

x=2,  x=3x = 2,\; x = 3

Why Are Roots Important?

Polynomial roots help in:

  • Solving equations
  • Finding intersections
  • System modeling
  • Signal processing
  • Stability analysis
  • Curve behavior understanding

Import NumPy

import numpy as np

1. Finding Roots Using np.roots()

import numpy as np

coefficients = [1, -5, 6]

roots = np.roots(coefficients)

print(roots)

Output

[3. 2.]

Explanation

The polynomial:

x25x+6x^2 - 5x + 6

Factors into:

(x3)(x2)(x - 3)(x - 2)

So roots are 3 and 2.


2. Cubic Polynomial Roots

import numpy as np

coefficients = [1, -6, 11, -6]

roots = np.roots(coefficients)

print(roots)

Output

[3. 2. 1.]

Explanation

Polynomial:

x36x2+11x6x^3 - 6x^2 + 11x - 6

Roots:

x=1,  2,  3x = 1,\; 2,\; 3

3. Complex Roots Example

Some polynomials have imaginary roots.

import numpy as np

coefficients = [1, 0, 1]

roots = np.roots(coefficients)

print(roots)

Output

[0.+1.j 0.-1.j]

Explanation

Polynomial:

x2+1=0x^2 + 1 = 0

Roots:

x=±ix = \pm i

4. Visualizing Polynomial and Roots

import numpy as np
import matplotlib.pyplot as plt

coefficients = [1, -5, 6]

p = np.poly1d(coefficients)

x = np.linspace(0, 5, 100)

y = p(x)

roots = np.roots(coefficients)

plt.plot(x, y, label="Polynomial")

plt.axhline(0, color='black')

plt.scatter(roots, [0, 0], color='red', label="Roots")

plt.legend()

plt.title("Polynomial Roots Visualization")

plt.show()

Explanation

  • Curve crosses x-axis at roots
  • Red points show root positions
  • Helps visualize solutions clearly

5. Roots from Polynomial Object

import numpy as np

p = np.poly1d([1, -5, 6])

print(p.r)

Output

[3. 2.]

Explanation

poly1d.r directly gives polynomial roots.


6. Checking Roots

Verify a root:

import numpy as np

p = np.poly1d([1, -5, 6])

print(p(2))

Output

0.0

Explanation

If result is 0, the value is a valid root.


7. Roots of High-Degree Polynomials

import numpy as np

coefficients = [1, -10, 35, -50, 24]

roots = np.roots(coefficients)

print(roots)

Explanation

NumPy handles even complex multi-degree polynomials efficiently.


Real-World Applications

1. Engineering

  • Control system stability
  • Circuit analysis
  • Mechanical vibration modeling

2. Physics

  • Motion equations
  • Wave analysis
  • Energy systems

3. Data Science

  • Curve fitting
  • Regression models
  • Optimization problems

4. Machine Learning

  • Feature transformations
  • Model tuning
  • Polynomial regression

5. Finance

  • Risk modeling
  • Growth prediction
  • Market equilibrium analysis

Key NumPy Functions for Roots

FunctionDescription
np.roots()Find polynomial roots
np.poly1d()Create polynomial object
p.rGet roots from poly1d
np.polyval()Evaluate polynomial

Why Use NumPy for Finding Roots?

Using NumPy provides:

  • Fast numerical root solving
  • Handles real and complex roots
  • Works for high-degree polynomials
  • Easy integration with scientific workflows

Combined with Python, it becomes a powerful tool for solving mathematical and engineering problems.


Summary

To find polynomial roots in NumPy:

np.roots([coefficients])

Or using polynomial object:

p = np.poly1d([...])
p.r

Conclusion

Finding roots of polynomials is essential in mathematics and scientific computing. NumPy provides a simple yet powerful function np.roots() that can solve real and complex polynomial equations efficiently. This makes it a key tool for engineers, scientists, and data analysts working with mathematical models.




Post a Comment

0 Comments