File size: 1,977 Bytes
fc042f4 | 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 | import matplotlib.pyplot as plt
import numpy as np
class Quadratic:
"""Representing a quadratic expression in the form of ax² + bx + c"""
def __init__(self,a : float, b : float, c : float) -> None:
# A class is created succesfully if the arguments of the class are either of float type or int type
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)):
self.a = a
self.b = b
self.c = c
else:
raise ValueError("Argument must be of type int or float")
def __repr__(self) -> str:
""" Printing a quadratic class """
return "Quad( {0}x² + {1}x + {2} )".format(self.a,self.b,self.c)
def solveQuad(self) -> tuple[float,float]:
"""Solving the expression assuming it is equal to 0.
returns a tuple of 2 values"""
determinant = ((self.b ** 2) - (4 * self.a * self.c)) ** 0.5 # The determinant of a quadratic equation is the root of b² - 4ac
firstSolution = ((-1 * self.b) + determinant) / (2 * self.a)
secondSolution = ((-1 * self.b) - determinant) / (2 * self.a)
return (firstSolution,secondSolution)
def evaluate(self, value):
"""Evaluate the Quadratic expression. with x in ax² + bx + c as a numeric type"""
solution = ((self.a * (value ** 2)) + (self.b * value) + self.c)
return solution
def drawFigure(self):
"""Draws a quadratic graph of the quadratic expression and returns a matplotlib figure"""
x_axis = np.linspace(-10,10,50)
y_axis = self.evaluate(x_axis)
figure, axes = plt.subplots()
axes.plot(x_axis,y_axis, linewidth = 1)
axes.grid(visible = True)
axes.set_title(self)
axes.xaxis.set_minor_formatter(str(x_axis))
return figure
|