Quadratic equation in python

by Alex
Quadratic equation in python

A program that allows you to find the roots of a quadratic equation is one example of the simple programs that you can write in Python 3. It is a great example of a program that can be written in Python 3 and is appropriate if you are new to the language.

Table of Contents

Problem Statement

The equation to be solved is as follows: a-x²+b-x+c=0. The user is prompted to enter the values of a, b, and c into the terminal. After that the program will calculate the discriminant. Based on it, find solutions to the equation – the values of x, for which the equality will be satisfied. Here is an example of the program that will be written.Problem Statement

Program

To solve quadratic equations in Python 3, let’s write the code below. Let’s break down some of the points we used in this simple program:

  • print – this function displays the information on the screen.
  • input – displays the information and prompts the user to enter the data.
  • b**2 is a power-up, in this case the b variable is squared.
  • str is a function that makes the data a string.
  • if-elif-else are conditional operators in Python. Based on the discriminant value, we determine the number of roots of the quadratic equation.
  • thediscriminant ** 0.5 is how we extract the square root. In Python, there are several ways to extract the roots, for example, using the function sqrt from the math library. How to extract roots in Python is described in a separate article.
print('Solve the equation a-x²+b-x+c=0')
a = input('Enter value of a: ')
b = input('Enter value of b: ')
c = input('Enter value c: ')
a = float(a)
b = float(b)
c = float(c)
discriminant = b**2 - 4*a*c
print('Discriminant = ' + str(discriminant))
if discriminant < 0:
    print('no roots')
elif discriminant == 0:
    x = -b / (2 * a)
    print('x = ' + str(x))
else:
    x1 = (-b + discriminant ** 0.5) / (2 * a)
    x2 = (-b - discriminant ** 0.5) / (2 * a)
    print('x₁ = ' + str(x1))
    print('x₂ = ' + str(x2))

Let’s run the program and enter the required coefficients.

Solve the equation a-x²+b-x+c=0
Enter the value of a: -4
Enter the value of b: -231
Enter the value of c: 34
Discriminant = 53905.0
x₁ = -57.89681291718352
x₂ = 0.1468129171835173

All calculated, two roots are found which will be the solution to the quadratic equation.

More

There is one more thing I would like to point out. If the discriminant is negative, there are no real roots. But there will be complex roots. If we want to handle them, we have to change the construction of conditional operators as follows:

if discriminant == 0:
    x = -b / (2 * a)
    print('x = ' + str(x))
else:
    x1 = (-b + discriminant ** 0.5) / (2 * a)
    x2 = (-b - discriminant ** 0.5) / (2 * a)
    print('x₁ = ' + str(x1))
    print('x₂ = ' + str(x2))

Then the example of solving the equation would look like this:

Solve the equation a-x²+b-x+c=0
Enter the value of a: 4
Enter the value of b: 1
Enter value of c: 2
Discriminant = -31.0
x₁ = (-0.124999999999999999996+0.6959705453537527j)
x₂ = (-0.12500000000000006-0.6959705453537527j)

As we see, we got two complex roots. This simple code, written in Python 3, can be made a little more complicated for learning to program:

  • Offer a query at the end of the program “Solve one more equation (y/n): “. And if the user enters “y”, re-request the coefficients. This should be done in a loop. You can read more about loops in Python here.
  • Make a check for correct input. After all, the user can enter any string instead of a number, which will not be processed correctly. About checking for a number is described in a separate article.

Related Posts

LEAVE A COMMENT