# How to solve a quadratic equation in python using numpy ?

Published: April 11, 2020

Tags: Python; Numpy; Maths;

Examples of how to solve a quadratic degree equation in python using numpy:

### Example 1

With python we can find the roots of a polynomial equation of degree 2 (\$ ax ^ 2 + bx + c \$) using the function numpy: roots.

Consider for example the following polynomial equation of degree 2 \$ x ^ 2 + 3x-0 \$ with the coefficients \$ a = 1 \$, \$ b = 3 \$ and \$ c = -4 \$, we then find:

````>>> import numpy as np`
`>>> coeff = [1,3,-4]`
`>>> np.roots(coeff)`
`array([-4.,  1.])`
```

this equation has 2 real roots: \$ x = -4 \$ and \$ x = 1 \$.

### Example 2

Another example with \$x^2+3x=0\$

````>>> coeff = [1,3,3]`
`>>> np.roots(coeff)`
`array([-1.5+0.8660254j, -1.5-0.8660254j])`
```

which admits only two complex roots: \$x=-1.5+0.8660254j\$ et \$x=-1.5-0.8660254j\$.

### Example 3

Another example with \$x^2-6x+9\$

````>>> coeff = [1,-6,9]`
`>>> np.roots(coeff)`
`array([ 3. +3.72529030e-08j,  3. -3.72529030e-08j])`
```

which admits a real root here: \$x=3\$ (car \$3.72529030e-08\$ est proche de \$0\$)