Reputation: 1157
I'm somewhat new to python, but I'm trying my best to learn. My code is
import math
a = 5
b = 5
c = 5
def quad_solve(a, b, c):
q1 = b*b
q2 = 4*a*c
q3 = 2*a
q4 = q1-q2
sqr = math.sqrt(q4)
sol1p1 = b+sqr
sol1p2 = sol1p1/2
sol2p1 = b-sqr
sol2p2 = sol2p1/2
print ("(",sol1p2,",",sol2p2,")")
quad_solve(a, b, c)
And when I run it it gives the error
Traceback (most recent call last):
File "python", line 19, in <module>
File "python", line 12, in quad_solve
ValueError: math domain error
which I don't really understand.
I'm trying to create a quadratic formula solver. I use the math module, and then define three variables, a, b, and c. Then, I define a function that takes in those variables (I call the function at the end). In the function, I define four quantities. q1
is the b squared under the square root, q2
is the 4ac also under the square root, q3
is the denominator, and q4
calculates the total under the square root (i.e., q1
- q2
). Then, I define a variable called sqr
which is equal to the square root of q4
. Then, I define four more variables, which calculate the solutions. sol1p1
takes b + sqr, and sol1p2
takes sol1p1
and divides it by two. This gives the first solution. Then, sol2p1
takes b - sqr, and sol2p2
takes sol2p1
and divides it by two. Finally, sol1p2
and sol2p2
are printed, in a set of parentheses with a comma between. I hope that makes sense; if any clarification is needed about the variable names, please let me know.
I am using the online compiler repl.it (I don't know if there's anything special to consider with that).
Thanks!
Edit:
I updated my code, per Code Apprentice's recommendations. I started by adding an if statement:
import math
a = 5
b = 5
c = 5
def quad_solve(a, b, c):
q1 = b*b
q2 = 4*a*c
q3 = 2*a
q4 = q1-q2
check = math.tan(q2)
if (q1 > check):
sqr = math.sqrt(q4)
sol1p1 = b+sqr
sol1p2 = sol1p1/2
sol2p1 = b-sqr
sol2p2 = sol2p1/2
print ("(",sol1p2,",",sol2p2,")")
else:
print "Imaginary number. There are no zeros."
quad_solve(a, b, c)
but it is continuing to return the error
Traceback (most recent call last):
File "python", line 23, in <module>
File "python", line 14, in quad_solve
ValueError: math domain error
I'm not sure why.
Upvotes: 3
Views: 7500
Reputation: 1
This is my initial try for a quadratic solver while trying to learn python. It's very basic and doesn't check for imaginary solutions. It will just fail if the solution is not real.
# Second order quadratic solver: ax^2 + bx + c
import math
def testinput(userinput):
try:
val = float(userinput)
except ValueError:
print("\n\n\nENTER REAL NUMBERS \n\n\n")
print("ax^2 + bx + c quadratic solver")
a = input("Enter a:")
testinput(a)
a = float(a)
b = input("Enter b:")
testinput(b)
b = float(b)
c = input("Enter c:")
testinput(c)
c = float(c)
x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
print("\n\nInput: %.2fx^2 + %.2fx + %.2f\n" %(a,b,c))
print("\nSolution: x1 = %.3f , x2 = %.3f\n\n" %(x1,x2))
Upvotes: 0
Reputation: 1
Try this code:
import cmath
a=float(input("Enter a: "))
b=float(input("Enter b: "))
c=float(input("Enter c: "))
sol 1=(-b-cmath.sqrt(b**2)-(4*a*c)/(2*a))
sol 2=(-b+cmath.sqrt(b**2)-(4*a*c)/(2*a))
print("x = {0}and{1}".format (sol1,sol2))
Please give me a feedback on if it worked out
Upvotes: 0
Reputation: 600
This is my version of the answer in the fewest lines of code:
import cmath
#Your Variables
a = 5
b = 5
c = 5
#The Discriminant
d = (b**2) - (4*a*c)
#The Solutions
solution1 = (-b-cmath.sqrt(d))/(2*a)
solution2 = (-b+cmath.sqrt(d))/(2*a)
print (solution1)
print (solution2)
Upvotes: 3
Reputation: 1157
There are two ways to do this (I figured this out thanks to the great hints given by Code-Apprentice, Pablo Iocco, and Tom Pitts).
import cmath
import math
a = 1
b = 3
c = 2
def quad_solve_exact(a, b, c):
d = (b*b)-(4*a*c)
solution1 = (-b-cmath.sqrt(d))/(2*a)
solution2 = (-b+cmath.sqrt(d))/(2*a)
print (solution1,solution2)
quad_solve_exact(a, b, c)
def quad_solve(a, b, c):
if (b*b > 4*a*c):
print "There is a solution!"
d = math.sqrt((b*b)-(4*a*c))
solution1 = (-b-math.sqrt(d))/(2*a)
solution2 = (-b+math.sqrt(d))/(2*a)
print (solution1,solution2)
else:
print "No solutions, imaginary number"
quad_solve(a, b, c)
The first way is the more exact. The problem is the sqrt
function used by the normal math
package can't handle negative numbers. There is a package, cmath
, however, that can handle negative numbers. So, you import both packages (the normal math
package is used in the second example) and then define a, b, and c. Then, in your function you can combine the variables much more than I did, leading to shorter/clearer code. So, the variable d
is used to denote what is under the square root. Then, for each solution, -b is added or subtracted to the square root of d
, all of which is divided by 2a. Then, the solutions are printed.
The second solution is less exact, but perfectly serviceable for my purposes. The new function also takes a, b, and c. Then, an if statement is used to make sure that the number being square rooted is not negative. If it is negative, then the else statement is ran, which prints that there is no solution. If the variables pass the if statement, it prints that there is indeed a solution, and basically uses the same code as before, except instead of cmath.sqrt
, math.sqrt
is used.
Upvotes: 0
Reputation: 27283
If you're just interested in getting a result (and not in learning how to do this), you can use sympy:
from sympy import var, solve
x = var("x")
print(solve(5*x**2 + 5*x + 5))
# prints [-1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]
Upvotes: 1
Reputation: 31
b^2 has to be greater than 4ac, So right now, that sqrt()
function is getting a negative number.
Upvotes: 1