sábado, 2 de junio de 2018

Quadratic equations

Solving quadratic equations (automated)

What follows is a Python.3.7 program. It must be previously installed.

#Quadratic equation
print('For the equation ax**2 + bx + c= 0')
print('type, one at a time, a, b, c')

a=float(input())
b=float(input())
c=float(input())

d =b**2-4*a*c

print('discriminant =',b**2-4*a*c)

e1=(-b+(d**.5))*(2*a)**-1
e2=(-b-(d**.5))*(2*a)**-1
f1=-b*(2*a)**-1
f2=((-d)**.5)*(2*a)**-1

if(d>=0):
    print('real solutions')
    print('x1 =',e1)
    print('x2 =',e2)

else:
    print('complex solutions')
    print('x1 =',f1,'+',f2,'i')
    print('x2 =',f1,'-',f2,'i')

Example 1. Given x**2 - x - 20 = 0
1
-1
-20
discriminant = 81.0
real solutions
x1 = 5.0
x2 = -4.0

Example 2. Given 5x**2 - 2x + 7 = 0
5
-2
7
discriminant = -136.0
complex solutions
x1 = 0.2 + 1.1661903789690602 i
x2 = 0.2 - 1.1661903789690602 i

No hay comentarios:

Publicar un comentario