@@ -6,7 +6,7 @@ class Quadratic:
6
6
"""Representing a quadratic expression in the form of ax² + bx + c"""
7
7
8
8
def __init__ (self ,a : float , b : float , c : float ) -> None :
9
- # A class is created succesfully if the arguments of the class are either float or int
9
+ # A class is created succesfully if the arguments of the class are either of float type or int type
10
10
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 )):
11
11
self .a = a
12
12
self .b = b
@@ -15,25 +15,28 @@ def __init__(self,a : float, b : float, c : float) -> None:
15
15
raise ValueError ("Argument must be of type int or float" )
16
16
17
17
def __repr__ (self ) -> str :
18
+ """ Printing a quadratic class """
18
19
return "Quad( {0}x² + {1}x + {2} )" .format (self .a ,self .b ,self .c )
19
20
20
21
def solveQuad (self ) -> tuple [float ,float ]:
21
- """Solving the expression assuming it is equal to 0 returns a tuple of 2 values"""
22
- determinant = ((self .b ** 2 ) - (4 * self .a * self .c )) ** 0.5
22
+ """Solving the expression assuming it is equal to 0.
23
+ returns a tuple of 2 values"""
24
+
25
+ determinant = ((self .b ** 2 ) - (4 * self .a * self .c )) ** 0.5 # The determinant of a quadratic equation is the root of b² - 4ac
23
26
firstSolution = ((- 1 * self .b ) + determinant ) / (2 * self .a )
24
27
secondSolution = ((- 1 * self .b ) - determinant ) / (2 * self .a )
25
28
return (firstSolution ,secondSolution )
26
29
27
- def evaluate (self , x ):
28
- """Substitute x for a value and return a string to be called by the eval function """
29
- solution = ((self .a * (x ** 2 )) + (self .b * x ) + self .c )
30
+ def evaluate (self , value ):
31
+ """Evaluate the Quadratic expression. with x in ax² + bx + c as a numeric type """
32
+ solution = ((self .a * (value ** 2 )) + (self .b * value ) + self .c )
30
33
return solution
31
34
32
35
def drawFigure (self ):
33
- """Draws the quadratic graph and returns a matplotlib figure"""
36
+ """Draws a quadratic graph of the quadratic expression and returns a matplotlib figure"""
34
37
x_axis = np .linspace (- 10 ,10 ,50 )
35
38
y_axis = self .evaluate (x_axis )
36
-
39
+
37
40
figure , axes = plt .subplots ()
38
41
39
42
axes .plot (x_axis ,y_axis , linewidth = 1 )
0 commit comments