Question

In: Computer Science

A cubic polynomial is of the form: f(x) = Ax^3 + Bx^2 + Cx + D...

A cubic polynomial is of the form: f(x) = Ax^3 + Bx^2 + Cx + D   

A root of the polynomial is a value, x, such that f(x)=0.  

Write a program that takes in the coefficients of a cubic polynomial: A, B, C, and D. The program finds and reports all three roots of the polynomial.

Hint: First use bisection method to determine a single root of the polynomial. Then divide the polynomial by its factor to come up with a quadratic equation. You may use the procedure described here: https://www.purplemath.com/modules/synthdiv.htm. Subsequently, solve the quadratic equation.

The code should be written in python.

Solutions

Expert Solution

NOTE: PLEASE REFER IMAGE FOR INDENTATION

import math

import numpy as np

def solve(a, b, c, d):

    if (a == 0 and b == 0):                     # Case for handling Liner Equation

        return np.array([(-d * 1.0) / c])                 # Returning linear root as numpy array.

    elif (a == 0):                              # Case for handling Quadratic Equations

        D = c * c - 4.0 * b * d                       # Helper Temporary Variable

        if D >= 0:

            D = math.sqrt(D)

            x1 = (-c + D) / (2.0 * b)

            x2 = (-c - D) / (2.0 * b)

        else:

            D = math.sqrt(-D)

            x1 = (-c + D * 1j) / (2.0 * b)

            x2 = (-c - D * 1j) / (2.0 * b)

           

        return np.array([x1, x2])               # Returning Quadratic Roots as numpy array.

    f = findF(a, b, c)                          # Helper Temporary Variable

    g = findG(a, b, c, d)                       # Helper Temporary Variable

    h = findH(g, f)                             # Helper Temporary Variable

    if f == 0 and g == 0 and h == 0:            # All 3 Roots are Real and Equal

        if (d / a) >= 0:

            x = (d / (1.0 * a)) ** (1 / 3.0) * -1

        else:

            x = (-d / (1.0 * a)) ** (1 / 3.0)

        return np.array([x, x, x])              # Returning Equal Roots as numpy array.

    elif h <= 0:                                # All 3 roots are Real

        i = math.sqrt(((g ** 2.0) / 4.0) - h)   # Helper Temporary Variable

        j = i ** (1 / 3.0)                      # Helper Temporary Variable

        k = math.acos(-(g / (2 * i)))           # Helper Temporary Variable

        L = j * -1                              # Helper Temporary Variable

        M = math.cos(k / 3.0)                  # Helper Temporary Variable

        N = math.sqrt(3) * math.sin(k / 3.0)    # Helper Temporary Variable

        P = (b / (3.0 * a)) * -1                # Helper Temporary Variable

        x1 = 2 * j * math.cos(k / 3.0) - (b / (3.0 * a))

        x2 = L * (M + N) + P

        x3 = L * (M - N) + P

        return np.array([x1, x2, x3])           # Returning Real Roots as numpy array.

    elif h > 0:                                 # One Real Root and two Complex Roots

        R = -(g / 2.0) + math.sqrt(h)           # Helper Temporary Variable

        if R >= 0:

            S = R ** (1 / 3.0)                  # Helper Temporary Variable

        else:

            S = (-R) ** (1 / 3.0) * -1          # Helper Temporary Variable

        T = -(g / 2.0) - math.sqrt(h)

        if T >= 0:

            U = (T ** (1 / 3.0))                # Helper Temporary Variable

        else:

            U = ((-T) ** (1 / 3.0)) * -1        # Helper Temporary Variable

        x1 = (S + U) - (b / (3.0 * a))

        x2 = -(S + U) / 2 - (b / (3.0 * a)) + (S - U) * math.sqrt(3) * 0.5j

        x3 = -(S + U) / 2 - (b / (3.0 * a)) - (S - U) * math.sqrt(3) * 0.5j

        return np.array([x1, x2, x3])           # Returning One Real Root and two Complex Roots as numpy array.

# Helper function to return float value of f.

def findF(a, b, c):

    return ((3.0 * c / a) - ((b ** 2.0) / (a ** 2.0))) / 3.0

# Helper function to return float value of g.

def findG(a, b, c, d):

    return (((2.0 * (b ** 3.0)) / (a ** 3.0)) - ((9.0 * b * c) / (a **2.0)) + (27.0 * d / a)) /27.0

# Helper function to return float value of h.

def findH(g, f):

    return ((g ** 2.0) / 4.0 + (f ** 3.0) / 27.0)

x=solve(1,2,3,4)

print(x)


Related Solutions

1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx +...
1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx + d where a ≠ 0. Show that f can have zero, one, or two critical numbers and give an example of each case. 2. Use Rolle's Theorem to prove that if f ′ ( x ) = 0 for all xin an interval ( a , b ), then f is constant on ( a , b ). 3.True or False. The product of...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the standard equation of the tangent line AP.
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the value of the coefficient "b" in the equation of the given cubic function.
Given a general cubic function y = ax^3 + bx^2 + cx + d prove the...
Given a general cubic function y = ax^3 + bx^2 + cx + d prove the following, a) That a cubic must change at a quadratic rate.          I believe this to be the derivative of the general cubic function, yielding dy/dx = 3ax^2 + 3bx + c b) That there are only 6 basic forms (shapes) for a cubic.         This question is where I get lost. Please help, Thanks!
Find the cubic equation: f(x) = ax^3+bx^2+cx+d for which f(-1)=3, f(1)=1, f(2)=6, and f(3)=7. Find the...
Find the cubic equation: f(x) = ax^3+bx^2+cx+d for which f(-1)=3, f(1)=1, f(2)=6, and f(3)=7. Find the value of a, b, c, and d
Find the cubic equation. F(x)=ax^3+bx^2+cx+d F(-1)=3 F(1)=1 F(2)=6 F(3)=7 What is the value of a,b,c,d
Find the cubic equation. F(x)=ax^3+bx^2+cx+d F(-1)=3 F(1)=1 F(2)=6 F(3)=7 What is the value of a,b,c,d
Find a polynomial of the form f(x) = ax3 + bx2 + cx + d such...
Find a polynomial of the form f(x) = ax3 + bx2 + cx + d such that f(0) = −3, f(1) = 2, f(3) = 5, and f(4) = 0. (A graphing calculator is recommended.) answer in fraction form.
Ax = 6m and Ay= -8m, Bx= -8m and By= 3m, Cx = 27m and Cy=...
Ax = 6m and Ay= -8m, Bx= -8m and By= 3m, Cx = 27m and Cy= 21m. Determine a and b such that aA + bB + C = 0. Include a sketch of the scenario.
(x-1) is a factor of P(x) = x^3 - ax^2 + bx -10. When P(x) is...
(x-1) is a factor of P(x) = x^3 - ax^2 + bx -10. When P(x) is divided by (x+4) there is a remainder of 10. Determine the values of a and b.
let p1(x) = x^2-3x-10 ,p2(x)=x^2-5x+1,p3(x)=x^2+2x+3 and p4(x)=x+5 a- Using standard polynomIAL ADDITION ,what polynomial ax^2+bx+c can...
let p1(x) = x^2-3x-10 ,p2(x)=x^2-5x+1,p3(x)=x^2+2x+3 and p4(x)=x+5 a- Using standard polynomIAL ADDITION ,what polynomial ax^2+bx+c can be expressed as linear combination of p1(x),p2(x),p3(X),p4(x) b- a polynomial is equal to zero if and only if all it's coefficient to zero . solve for a1,a2,a3,a4 by expanding ,written as polynomial in x,and setting each coefficient equal to zero: a1p1(x)+a2p2+a3p3(x)+a4p4(x)=0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT