Quadratic equations¶
Write and save the following functions into the same file named
quadratic.py.
real_roots()that takes three parametersa,bandc, corresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\) with two different real roots, and returns its roots.See the following examples:
>>> a, b, c = 1, -4, 3 >>> x1, x2 = real_roots(a, b, c) >>> a*x1**2+b*x1+c == 0 True
Note
More tests are provided in the
quadratic1.txtfile.has_real_roots()that takes three parametersa,bandc, corresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\), and retunsTrueif the quadratic equation has real roots andFalseotherwise.See the following examples:
>>> a, b, c = 1, -4, 3 >>> r = has_real_roots(a, b, c) >>> r True
Note
More tests are provided in the
quadratic2.txtfile.is_a_root()that takes five parametersa,b,ccorresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\),xcorresponding to a candidate rootx, andepscorresponding to a tolerance. This function returnsTrueifxis a root of the quadratic equation taking into account the given tolerance, i. e., whenabs(y) <= eps, andFalseotherwise.See the following examples:
(x+1)(x-2/3) = 3x²+1x-2 >>> a, b, c = 3, 1, -2 >>> is_a_root(a, b, c, -1, 0) True >>> is_a_root(a, b, c, 0.66, 0.00001) False >>> is_a_root(a, b, c, 0.6666, 0.01) True >>> is_a_root(a, b, c, 1, 0) False
Note
More tests are provided in the
quadratic3.txtfile.
Solutions
A solution of these functions is provided in the
quadratic.py file.