Active April 11, 2020    /    Viewed 23644    /    Comments 0    /    Edit

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

Summary

### 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\$)

numpy.roots numpy doc
Finding polynomial roots using Python — Possible Numpy Extension Bug stackoverflow  ##### Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount