using python Write a function that returns the two roots of a quadratic equation
ID: 3872508 • Letter: U
Question
using python
Write a function that returns the two roots of a quadratic equation
as a tuple of complex values.
Structure the program as follows so that the program when run, prints the roots for the following three cases:
Program structure
import math
class complex:
re = 0
im = 0
def __init__(self,r,i):
self.re = r
self.im = i
def format(self):
return "{}{:+f}i".format(self.re,self.im)
def solvequadratic(a,b,c):
.
return (complex( , ),complex( , ))
#____main____
a= solvequadratic(5,1,2)
print ("5x^2 + x + 2 = 0 -> x= ", a[0], " or ", a[1])
Explanation / Answer
import math
a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")
d = b**2-4*a*c # discriminant
if d < 0:
print "This equation has no real solution"
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print "This equation has one solutions: ", x
else:
x1 = (-b+math.sqrt(b**2-4*a*c))/2*a
x2 = (-b-math.sqrt(b**2-4*a*c))/2*a
print "This equation has two solutions: ", x1, " and", x2
a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))
d = b**2-4*a*c # discriminant
if d < 0:
print ("This equation has no real solution")
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print ("This equation has one solutions: "), x
else:
x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
print ("This equation has two solutions: ", x1, " or", x2)
<code>
import cmath
import math
print(" we are going to programming second grade equation in python")
print(" a^2 x + b x + c =0")
num1 = int(input(" enter A please : "))
num2 = int(input(" enter B please : "))
num3 = int(input(" enter c please : "))
v = num2*num2 - 4 *num1 * num3
print(v)
if v < 0 :
print("wrong values")
else:
print("root of delta =", v)
k= math.sqrt(v)
def two_sol(x,y) :
x_f= (-y + v)/(4*x)
x_s =(-y - v)/(4*x)
return x_f , x_s
def one_sol(x):
x_f = (-y + v) / (4 * x)
if v >0 :
print("we have two solution :" ,two_sol(num1,num2))
elif v == 0:
print( "we have one solution :" , one_sol(y))
else:
print(" there is no solution !!")
</code>
# quadraticcomplex.py
# A program that computes the complex roots of a quadratic equation.
# Illustrates use of the cmath library.
# The presence of complex numbers also requires mathematical functions that can perform operations on them.
# The imaginary component of a complex number is denoted by a suffix of ``J'' or ``j'';
import cmath # Makes the cmath library available.
def main(): # beginning of the code
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
root1 = (-b + discRoot) / (2 * a) # formula solving root1
root2 = (-b - discRoot) / (2 * a) # formula solving root2
print
print "The solutions are:", root1, root2 # display the results
main() # end of the code
disc = b**2 - 4*a*c # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(disc)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negative
def main(): # beginning of the code
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
disc = b**2 - 4*a*c # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(disc)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negative
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.