Create quad.py
Browse files
quad.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Quadratic:
|
| 10 |
+
|
| 11 |
+
"""Representing a quadratic expression in the form of ax² + bx + c"""
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def __init__(self,a : float, b : float, c : float) -> None:
|
| 16 |
+
|
| 17 |
+
# A class is created succesfully if the arguments of the class are either of float type or int type
|
| 18 |
+
|
| 19 |
+
if (type(a) == type(0.1) or type(a) == type(1)) and (type(b) == type(0.1) or type(b) == type(1)) and (type(c) == type(0.1) or type(c) == type(1)):
|
| 20 |
+
|
| 21 |
+
self.a = a
|
| 22 |
+
|
| 23 |
+
self.b = b
|
| 24 |
+
|
| 25 |
+
self.c = c
|
| 26 |
+
|
| 27 |
+
else:
|
| 28 |
+
|
| 29 |
+
raise ValueError("Argument must be of type int or float")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def __repr__(self) -> str:
|
| 34 |
+
|
| 35 |
+
""" Printing a quadratic class """
|
| 36 |
+
|
| 37 |
+
return "Quad( {0}x² + {1}x + {2} )".format(self.a,self.b,self.c)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def solveQuad(self) -> tuple[float,float]:
|
| 42 |
+
|
| 43 |
+
"""Solving the expression assuming it is equal to 0.
|
| 44 |
+
|
| 45 |
+
returns a tuple of 2 values"""
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
determinant = ((self.b ** 2) - (4 * self.a * self.c)) ** 0.5 # The determinant of a quadratic equation is the root of b² - 4ac
|
| 50 |
+
|
| 51 |
+
firstSolution = ((-1 * self.b) + determinant) / (2 * self.a)
|
| 52 |
+
|
| 53 |
+
secondSolution = ((-1 * self.b) - determinant) / (2 * self.a)
|
| 54 |
+
|
| 55 |
+
return (firstSolution,secondSolution)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def evaluate(self, value):
|
| 60 |
+
|
| 61 |
+
"""Evaluate the Quadratic expression. with x in ax² + bx + c as a numeric type"""
|
| 62 |
+
|
| 63 |
+
solution = ((self.a * (value ** 2)) + (self.b * value) + self.c)
|
| 64 |
+
|
| 65 |
+
return solution
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def drawFigure(self):
|
| 70 |
+
|
| 71 |
+
"""Draws a quadratic graph of the quadratic expression and returns a matplotlib figure"""
|
| 72 |
+
|
| 73 |
+
x_axis = np.linspace(-10,10,50)
|
| 74 |
+
|
| 75 |
+
y_axis = self.evaluate(x_axis)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
figure, axes = plt.subplots()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
axes.plot(x_axis,y_axis, linewidth = 1)
|
| 84 |
+
|
| 85 |
+
axes.grid(visible = True)
|
| 86 |
+
|
| 87 |
+
axes.set_title(self)
|
| 88 |
+
|
| 89 |
+
axes.xaxis.set_minor_formatter(str(x_axis))
|
| 90 |
+
|
| 91 |
+
return figure
|
| 92 |
+
|