Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmPython | Finding Solutions of a Polynomial Equation

Python | Finding Solutions of a Polynomial Equation

Given a quadratic equation, the task is to find the possible solutions to it. 
Examples: 
 

Input : 
enter the coef of x2 : 1
enter the coef of x  : 2
enter the constant    : 1
Output :
the value for x is -1.0

Input :
enter the coef of x2 : 2
enter the coef of x  : 3
enter the constant    : 2
Output :
x1 = -3+5.656854249492381i/4 and x2 = -3-5.656854249492381i/4

Algorithm :

Start.
Prompt the values for a, b, c. 
Compute i = b**2-4*a*c
If i get negative value g=square root(-i)
Else h = sqrt(i)
Compute e = -b+h/(2*a)
Compute f = -b-h/(2*a)
If condition e==f then
    Print e
Else
    Print e and f
If i is negative then
    Print -b+g/(2*a) and -b-g/(2*a)
stop

Below is the Python implementation of the above mentioned task. 
 

Python3




# Python program for solving a quadratic equation.
  
from math import sqrt  
try:    
 
    # if user gives non int values it will go to except block
    a = 1
    b = 2
    c = 1
    i = b**2-4 * a * c
 
    # magic condition for complex values
    g = sqrt(-i)
    try:
        d = sqrt(i)
        # two resultants
        e = (-b + d) / 2 *
        f = (-b-d) / 2 * a
        if e == f:
            print("the values for x is " + str(e))
        else:
            print("the value for x1 is " + str(e) +
                  " and x2 is " + str(f))
    except ValueError:
        print("the result for your equation is in complex")
         
        # to print complex resultants.
        print("x1 = " + str(-b) + "+" + str(g) + "i/" + str(2 * a) +
              " and x2 = " + str(-b) + "-" + str(g) + "i/" +
              str(2 * a))  
         
except ValueError:
    print("enter a number not a string or char")


Output : 
 

the values for x is -1.0

Explanation : 
First, this program will get three inputs from the user. The values are the coefficient of x2 , coefficient of x and constant. Then it performs the formula 
(-b + (or) - sqrt(b2 - 4 * a * c) / 2a)
For complex the value of (b2 - 4 * a * c) gets negative. Rooting negative values will throw a value error. In this case, turn the result of -(b2 - 4 * a * c) and then root it. Don’t forget to include i at last. 
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments