Python program to find all the roots of a given cubic equation

Python program to find all the roots of a given cubic equation

 

# Python program to find all the roots of a given cubic equation

import fractions

print("The equation is-\n \t ax^3 + bx^2 + cx + d = 0")
a = eval(input("Enter the value of a\t"))
b = eval(input("Enter the value of b\t"))
c = eval(input("Enter the value of c\t"))
d = eval(input("Enter the value of d\t"))
roots = []
coef = [a, b, c, d]

def quad(p, q, r):
    if p == 0:
        return lin1(q, r)
    else:
        a1 = [((-q + ((q ** 2 - 4 * p * r) ** 0.5)) / (2 * p)), ((-q - ((q ** 2 - 4 * p * r) ** 0.5)) / (2 * p))]
        if type(a1[0]) == complex:
            t2 = []
            for li in a1:
                if li.imag != 0:

                    x = float(fractions.Fraction(li.real).limit_denominator())
                    y1 = round(li.imag, 3)
                    z = complex(x, y1)
                    t2.append(z)
                else:
                    pass
        try:
            return t2
        except:
            return a1


def lin2(a1, b1, c1, a2, b2, c2):
    global d
    global m
    global n
    global o

    if a2 == 0:
        global y
        y = lin1(b2, c2)
        x = lin1(a1, (b1 * y) + c1)

        return [x, y]
    d = a1 / a2

    m = a1 - (d * a2)
    n = b1 - (d * b2)
    o = c1 - (d * c2)

    y = (-o / n)

    d = b1 / b2

    m = a1 - (d * a2)
    n = b1 - (d * b2)
    o = c1 - (d * c2)

    x = (-1 * (lin1(a1, (b1 * y) + c1)) / n)
    return [x, y]


def point_slope(u1, x, z):
    return lin2(u1, -1, (z - (u1 * x)), 0, 1, 0)[0]


def lin1(a2, b2):
    return -b2 / a2


def val_cube(x):
    return a * (x ** 3) + b * (x ** 2) + c * x + d


def val_quad(x):
    return m * (x ** 2) + (n * x) + o


def lin_div(r, p, args):
    global m
    q = []
    m = (-p/r)
    s = 0
    step = 0
    for pi in args:
        if s == 0:
            step = pi
            q.append(pi)
        else:
            step = step*m + pi
            q.append(step)
        s = s + 1
    return q


m = 3 * a
n = 2 * b
o = c
u = 2 * m
v = n
if a == 0:
    if b != 0:
        if c == 0:
            if d == 0:
                print("The solution of the quadratic equation is", quad(b, c, d))
            if d != 0:
                print("The roots of the quadratic equation are", quad(b, c, d))
        if c != 0:
            print("Since a = 0 therefore the equation is quadratic")
            print("The roots of the quadratic equation are", quad(b, c, d))

    if b == 0:
        if c == 0:
            if d == 0:
                print("The equation has no solution")
            if d != 0:
                print("The equation has no solution")
        if c != 0:
            print("The root of the linear equation is", -d/c)
if d == 0:
    if a != 0:
        f = quad(a, b, c)
        f.append(0)
        print("The roots of the equation are", f)
if a != 0:
    if d != 0:
        trial = 500.0
        sol = []
        num = 10
        while True:
            slope = val_quad(trial)
            y = val_cube(trial)
            if y < 10 ** (-num):
                if y > -10 ** (-num):
                    abc = round(trial, 2)
                    if a not in sol:
                        sol.append(abc)
                    break
            trial = (point_slope(slope, trial, y))
        quadr = (lin_div(1, -sol[0], coef))
        quad1 = []
        for i in quadr[0:3]:
            i = float(i)
            quad1.append(fractions.Fraction(i).limit_denominator())

        sol.append(quad(quad1[0], quad1[1], quad1[2]))
        print("The roots of the cubic equation are", sol)

 

 

Output:

The equation is-
ax^3 + bx^2 + cx + d = 0
Enter the value of a 4
Enter the value of b 4
Enter the value of c 2
Enter the value of d 3
The roots of the cubic equation are [-1.14, [(0.07+0.809j), (0.07-0.809j)]]
>>>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *