Nate
Nate

Reputation: 21

quadratic solver with real and complex roots

I'm trying to write a program that will generate the roots given a, b, and c from the quadratic formula. I've made it so it works when the roots are real or when it's a double root, but i'm not sure how to advance for when there are complex roots. For example if i was trying to find the roots of y = 2x^2 - 5x + 17. Any suggestions? Here's what i have so far:

import math
import cmath

def quadSolver(a,b,c, tol = 1e-18):
    print('Equation: {0}x**2 + {1}x + {2}'.format(a,b,c))
    discriminant = b**2 - 4 * a * c
    if discriminant > 0:
        root1 = float(-b + math.sqrt(b**2 - 4 * a * c))/ (2 * a)
        root2 = float(-b - math.sqrt(b**2 - 4 * a * c))/ (2 * a)
        print('Has two roots:')
        print(root1)
        print(root2)
    elif discriminant == 0:
        root1 = float(-b + math.sqrt(b**2 - 4 * a * c))/ (2 * a)
        print('Has a double root:')
        print(root1)
    elif discriminant < 0:
        root1 = (-b + cmath.sqrt(b**2 - 4 * a * c))/ (2 * a)
        root2 = (-b - cmath.sqrt(b**2 - 4 * a * c))/ (2 * a)
        print('Has two complex roots:')
        print(root1)
        print(root2)

**This worked so it now produces complex roots, but now i'm having trouble when a = 0, i.e. when y = mx + b or y = a constant. So how can I edit it to encompass those as well?

Upvotes: 1

Views: 13390

Answers (3)

albany.cs
albany.cs

Reputation: 1

import math
import cmath
def main():
    a=int(input("enter a: "))
    b=int(input("enter b: "))
    c=int(input("enter c: "))
    d=((math.pow(b,2))-(4*a*c))
    if a!=0:
        if d>0:
           x1=(-b+math.sqrt(d))/(2*a)
           x2=(-b-math.sqrt(d))/(2*a)
           print("x1=",x1," and x2=",x2)
           print("real root")
        elif d==0:
           x=-b/(2*a)
           print("x=",x)
           print("equal root" )
        elif d<0:
            x1=(-b+cmath.sqrt(d))/(2*a)
            x2=(-b-cmath.sqrt(d))/(2*a)
            print("x1=",x1," and x2=",x2)
            print("complex root")
    else:
        print("this is not quadratic equation")
main()
########albany.cs######

Upvotes: 0

albany
albany

Reputation: 1

import math
import cmath
def main():
    a=int(input("enter a: "))
    b=int(input("enter b: "))
    c=int(input("enter c: "))
    d=((math.pow(b,2))-(4*a*c))
    if a!=0:
        if d>0:
           x1=(-b+math.sqrt(d))/(2*a)
           x2=(-b-math.sqrt(d))/(2*a)
           print("x1=",x1," and x2=",x2)
           print("real root")
        elif d==0:
           x=-b/(2*a)
           print("x=",x)
           print("equal root" )
        elif d<0:
            x1=(-b+cmath.sqrt(d))/(2*a)
            x2=(-b-cmath.sqrt(d))/(2*a)
            print("x1=",x1," and x2=",x2)
            print("complex root")
    else:
        print("this is not quadratic equation")
main()
########albany.cs######

Upvotes: 0

xnx
xnx

Reputation: 25538

If your discriminant is < 0, you probably want cmath.sqrt in your calculation:

>>> import cmath
>>> cmath.sqrt(-1)
1j

EDIT: To answer your second question, when a == 0 your equation is linear: bx + c = 0 and its single solution is x = -c/b. (Obviously, if a==b==0 you don't have an equation to solve!)

Not tested, but to give you the idea:

import math
import cmath

def quadSolver(a,b,c, tol = 1e-18):
    print('Equation: {0}x**2 + {1}x + {2}'.format(a,b,c))
    if a==b==0:
        if c!=0:
            print('Not a valid equation')
        else:
            print(' 0=0 is not an interesting equation')
        return

    if a==0:
        print('Single solution is x =', -c/b)
        return

    discriminant = b**2 - 4 * a * c
    if discriminant > 0:
        root1 = (-b + math.sqrt(discriminant))/ (2 * a)
        root2 = (-b - math.sqrt(discriminant))/ (2 * a)
        print('Has two roots:')
        print(root1)
        print(root2)
    elif discriminant == 0:
        root1 = float(-b + math.sqrt(discriminant))/ (2 * a)
        print('Has a double root:')
        print(root1)
    elif discriminant < 0:
        root1 = (-b + cmath.sqrt(discriminant))/ (2 * a)
        root2 = (-b - cmath.sqrt(discriminant))/ (2 * a)
        print('Has two complex roots:')
        print(root1)
        print(root2)

(NB since you define discriminant you might as well use it in your subsequent calculations).

Upvotes: 3

Related Questions