-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathQuadratic_Equation.py
172 lines (137 loc) · 4.08 KB
/
Quadratic_Equation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# This code is made by MRayan Asim
# Packages to installed:
# pip install numpy
# pip install matplotlib.pyplot
import numpy as np
import matplotlib.pyplot as plt
import math
# Get quadratic equation coefficients from user
while True:
try:
a = float(input("Enter the coefficient of x^2: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
while True:
try:
b = float(input("Enter the coefficient of x: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
while True:
try:
c = float(input("Enter the constant term: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
print(a, "x^2", "+", b, "x", "+", c, "= 0")
t = input("Are you satisfied with this equation? (yes/no) ")
if t.lower() == "no":
print("OK, enter them again:")
while True:
try:
a = float(input("Enter the coefficient of x^2: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
while True:
try:
b = float(input("Enter the coefficient of x: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
while True:
try:
c = float(input("Enter the constant term: "))
break
except (ValueError, TypeError):
print("Please enter a valid value.")
time.sleep(3)
# Solve quadratic equation
def solve_quadratic(a, b, c):
discriminant = b ** 2 - 4 * a * c
if discriminant > 0:
root1 = (-b + np.sqrt(discriminant)) / (2 * a)
root2 = (-b - np.sqrt(discriminant)) / (2 * a)
return root1, root2
elif discriminant == 0:
root = -b / (2 * a)
return root, root
else:
real_part = -b / (2 * a)
imaginary_part = np.sqrt(-discriminant) / (2 * a)
root1 = complex(real_part, imaginary_part)
root2 = complex(real_part, -imaginary_part)
return root1, root2
# Factorize quadratic equation into two brackets
def factorize_quadratic(a, b, c):
factors = []
# Convert coefficients to integers
a = int(a)
b = int(b)
c = int(c)
# Factor out common factors from the coefficients
gcd = math.gcd(a, math.gcd(b, c))
a //= gcd
b //= gcd
c //= gcd
# Factorize the coefficient of x^2
if a != 1:
factors.append(f"{a}(x^2)")
else:
factors.append("(x^2)")
# Factorize the coefficient of x
if b != 0:
if b < 0:
factors.append(f"(x{-b})")
else:
factors.append(f"(x+{b})")
# Factorize the constant term
if c != 0:
factors.append(f"({c})")
return tuple(factors)
# Get the range of x values from the user
x_values = input("Enter the range of x values (start, end): ")
x_start, x_end = map(float, x_values.split(","))
num_points = 100
# Generate x values
x = np.linspace(x_start, x_end, num_points)
# Compute y values
y = a * x ** 2 + b * x + c
# Find the vertex of the quadratic equation
vertex_x = -b / (2 * a)
vertex_y = a * vertex_x ** 2 + b * vertex_x + c
# Create the plot
plt.plot(x, y)
# Set the x and y labels
plt.xlabel("x")
plt.ylabel("y")
# Set the title
plt.title("Quadratic Equation: y = {}x^2 + {}x + {}".format(a, b, c))
# Set y-axis at the center
plt.axhline(0, color="black", linewidth=0.5)
# Mark the vertex on the graph
plt.plot(vertex_x, vertex_y, "ro")
plt.annotate(
f"({vertex_x:.2f}, {vertex_y:.2f})",
(vertex_x, vertex_y),
xytext=(vertex_x + 1, vertex_y - 10),
arrowprops=dict(facecolor="black", arrowstyle="->"),
)
# Solve quadratic equation
roots = solve_quadratic(a, b, c)
# Print the roots
print("\nRoots:")
for root in roots:
if isinstance(root, complex):
print(f"x = {root.real:.2f} + {root.imag:.2f}j")
else:
print(f"x = {root:.2f}")
# Factorize quadratic equation into two brackets
factors = factorize_quadratic(a, b, c)
# Print the factorization
print("\nFactorization:")
print("".join(factors))
time.sleep(10)
# Show the plot
plt.show()