Wrie a python program to use a function that computes the roots of a quadratic equation
Question
Wrie a python program to use a function that computes the roots of a quadratic equation
Solution
Sure, here is a simple Python program that uses a function to compute the roots of a quadratic equation. The quadratic equation is in the form ax^2 + bx + c = 0. The roots of the quadratic equation can be found with the formula: (-b ± sqrt(b^2 - 4ac)) / (2a).
import cmath
def compute_roots(a, b, c):
# calculate the discriminant
discriminant = (b**2) - (4*a*c)
# calculate two solutions
root1 = (-b-cmath.sqrt(discriminant))/(2*a)
root2 = (-b+cmath.sqrt(discriminant))/(2*a)
return (root1, root2)
# test the function
a = 1
b = 5
c = 6
root1, root2 = compute_roots(a, b, c)
print("The roots are {0} and {1}".format(root1, root2))
In this program, we first import the cmath module to perform complex square root. Then we define a function compute_roots that takes three parameters a, b, and c. Inside the function, we first calculate the discriminant and then the roots. If the discriminant is negative, the roots will be complex, and if it is zero or positive, the roots will be real. We return the roots from the function. Finally, we test the function with a = 1, b = 5, and c = 6 and print the roots.
Similar Questions
Which method can be used to find out the roots of any arbitrary function?Select one:a. func()b. roots()c.arb()d.fsolve()
What does the following code print?import mathx=100while(math.sqrt(x)>0): print("Square root larger than 0")x -= 5
For which of the following quadratic equations are the roots 2 and 5?x2 + 7x - 10 = 0x2 - 11x - 10 = 0x2 - 7x + 10 = 0x2 - 3x - 10 = 0
Determine, without graphing, whether the given quadratic function has a maximum value or a minimum value and then find that value.f(x) = x2 + 2x - 7
A quadratic curve y=x^2+bx+c has roots x= -2 and x=7.5. Determine the value of c.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.