Equazione di primo grado
print("ax + b = 0")
a = float(input("a = ")) # INPUT
b = float(input("b = ")) # INPUT
x = -b/a # ELABORAZIONE
risp = "x = " + str(x)
print(risp) # OUTPUT
Se a <> 0 …
...
if(a != 0):
x = -b/a
risp = "x = " + str(x)
print(risp)
Se a = 0 …
...
if(a != 0):
x = -b/a
risp = "x = " + str(x)
if(a == 0):
risp = "???"
...
Con tre selezioni singole …
...
if(a != 0):
x = -b/a
risp = "x = " + str(x)
if(a == 0) and (b != 0):
risp = "Impossibile"
if(a == 0) and (b == 0):
risp = "Indeterminata"
...
Equazione di secondo grado
import math
a = float(input("a = "))
b = float(input("b = "))
c = float(input("c = "))
x1 = (-b-math.sqrt(b**2-4*a*c)) / (2*a)
x2 = (-b+math.sqrt(b**2-4*a*c)) / (2*a)
print("x1 =", x1)
print("x2 =", x2)
Se il discriminante è positivo o nullo …
...
delta = b*b-4*a*c
if(delta >= 0):
x1 = (-b-math.sqrt(delta))/(2*a)
x2 = (-b+math.sqrt(delta))/(2*a)
print("x1 =", x1)
print("x2 =", x2)
...
Se il discriminante è negativo …
...
delta = b*b-4*a*c
if(delta >= 0):
x1 = (-b-math.sqrt(delta))/(2*a)
x2 = (-b+math.sqrt(delta))/(2*a)
print("x1 =", x1)
print("x2 =", x2)
if(delta < 0):
print("Discriminante negativo.")
I tre casi
...
if(delta > 0):
x1 = (-b-math.sqrt(delta))/(2*a)
x2 = (-b+math.sqrt(delta))/(2*a)
print("x1 =", x1)
print("x2 =", x2)
if(delta == 0):
x12 = -b/(2*a)
print("x1 = x2 =", x12)
if(delta < 0):
print("Discriminante negativo.")
Regola di Cramer
Sistema di 2 equazioni lineari in 2 incognite
print("ax + by = e")
print("cx + dy = f")
a = float(input("a = "))
b = float(input("b = "))
c = float(input("c = "))
d = float(input("d = "))
e = float(input("e = "))
f = float(input("f = "))
D = a*d - b*c # Determinante
Dx = d*e - b*f # Determinante con e, f in 1° colonna
Dy = a*f - c*e # Determinante con e, f in 2° colonna
x = Dx / D # SOLUZIONE x
y = Dy / D # SOLUZIONE y
print("x =", x)
print("y =", y)
Se il determinante è diverso da zero …
D = a*d - b*c # Determinante
if(D != 0):
Dx = d*e - b*f # Determinante con e, f in 1° colonna
Dy = a*f - c*e # Determinante con e, f in 2° colonna
x = Dx / D # SOLUZIONE x
y = Dy / D # SOLUZIONE y
print("x =", x)
print("y =", y)
Se il determinante è nullo?
...
if(D == 0):
print("Determinante nullo.")
if(D != 0):
...
...