Skip to content

Instantly share code, notes, and snippets.

@kehiy
Last active October 30, 2024 08:06
Show Gist options
  • Save kehiy/01073f26ea91fd0164542ba1c4eeca09 to your computer and use it in GitHub Desktop.
Save kehiy/01073f26ea91fd0164542ba1c4eeca09 to your computer and use it in GitHub Desktop.
quadratic equation in python.
import math
import numpy as np
import matplotlib.pyplot as plt
a = int(input("please enter a: "))
if a == 0:
print("a can't be 0.")
exit(1)
b = int(input("please enter b: "))
c = int(input("please enter c: "))
d = math.pow(b, 2) - (4 * a * c)
if d < 0:
print("delta is less than 0.")
exit(1)
x1 = (-b + np.sqrt(d)) / (2 * a)
x2 = (-b - np.sqrt(d)) / (2 * a)
print(f"root(s) are: \nx1: {x1}\nx2: {x2}")
x = np.linspace(-14, 14, 300)
y = a * (x ** 2) + b * x + c
plt.plot(x, y, label='f(x) = ax^2 + bx + c', color='blue')
plt.plot(x1, 0, 'ro', label=f'Root 1: {x1:.2f}')
plt.plot(x2, 0, 'go', label=f'Root 2: {x2:.2f}')
plt.get_current_fig_manager().set_window_title('quadratic function')
plt.title("quadratic function")
plt.xlabel("values of x")
plt.ylabel("values of y")
plt.axhline(0, color='gray', lw=0.5, ls='--')
plt.legend()
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment