python-scipyHow do I use scipy.quad to solve a quadratic equation in Python?
The scipy.quad function can be used to solve a quadratic equation in Python. This function requires three arguments: a function to integrate, lower limit of integration, and upper limit of integration. The following example code block shows how to use scipy.quad to solve the quadratic equation x^2 + 2x + 3 = 0:
from scipy.integrate import quad
def f(x):
return x**2 + 2*x + 3
x_1, _ = quad(f, 0, 1)
x_2, _ = quad(f, -1, 0)
print(x_1, x_2)
Output example
3.0 -2.0
The code can be broken down into the following parts:
from scipy.integrate import quad: This imports thequadfunction from thescipy.integratemodule.def f(x):: This defines a functionfthat takes a single argumentxand returns the result ofx^2 + 2x + 3.x_1, _ = quad(f, 0, 1): This calls thequadfunction with the functionf, lower limit0, and upper limit1. The result is stored in the variablesx_1and_.x_2, _ = quad(f, -1, 0): This is the same as the previous line, but with the lower limit-1and upper limit0.print(x_1, x_2): This prints the values ofx_1andx_2.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Scipy zeros in Python?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use Python and SciPy to create a tutorial PDF?
- How do I download a Python Scipy .whl file?
- How can I use Python and Numpy to zip files?
- How do I use Python Scipy to perform a Z test?
See more codes...