Skip to content

Commit 4275206

Browse files
authored
Merge pull request #358 from Ezek-iel/eze-kiel
Added Quadratic-Equation-Solver to main repo
2 parents 81378ed + ed59ca3 commit 4275206

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Quadratic equation solver
2+
3+
![image](img.png)
4+
5+
## Requirements
6+
```powershell
7+
numpy : 1.24.2
8+
matplotlib : 3.6.3
9+
ttkbootstrap : 1.10.1
10+
tkinter: "inbuilt", 8.6
11+
```
12+
13+
A simple quadratic equation solver with ttkbootstrap as GUI
14+
15+
- A Quadratic class was created to ease GUI use
16+
17+
## `Class Quadratic`
18+
A quadratic class recieves 3 arguments (a,b,c) according to
19+
ax² + bx + c
20+
```python
21+
q1 = Quadratic(a = 2, b = 4, c = 5)
22+
```
23+
## Methods
24+
### The solve quad method solves a quadratic expression assuming the expression is equal to 0
25+
> returns a tuple of two numbers
26+
```python
27+
q1 = Quadratic(a = 1, b = 8, c = 16)
28+
print(q1.solveQuad())
29+
30+
# returns 4, 4
31+
```
32+
> Where the determinant is less than zero, a complex number solution is returned `python3 supports complex numbers`
33+
34+
### The evaluate method replaces the x in ax² + bx + c with an integer or float and returns the calculated value
35+
```python
36+
q1 = Quadratic(a = 1, b = 8, c = 16)
37+
print(q1.evaluate(value = 2))
38+
39+
# returns 36
40+
```
41+
### The draw figure method draws a quadratic equation graph using numpy and matplotlib
42+
> `numpy and matplotlib required` see requirements section above
43+
```python
44+
q1 = Quadratic(a = 1, b = 8, c = 16)
45+
print(q1.drawFigure())
46+
47+
# returns 4, 4
48+
```
49+
> A matplotlib figure is returned and can be added to a matplotlib graph

GUI/Quadratic-Equation-Solver/img.png

35.1 KB
Loading

GUI/Quadratic-Equation-Solver/quad.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
5+
class Quadratic:
6+
"""Representing a quadratic expression in the form of ax² + bx + c"""
7+
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 of float type or int type
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+
self.a = a
12+
self.b = b
13+
self.c = c
14+
else:
15+
raise ValueError("Argument must be of type int or float")
16+
17+
def __repr__(self) -> str:
18+
""" Printing a quadratic class """
19+
return "Quad( {0}x² + {1}x + {2} )".format(self.a,self.b,self.c)
20+
21+
def solveQuad(self) -> tuple[float,float]:
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
26+
firstSolution = ((-1 * self.b) + determinant) / (2 * self.a)
27+
secondSolution = ((-1 * self.b) - determinant) / (2 * self.a)
28+
return (firstSolution,secondSolution)
29+
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)
33+
return solution
34+
35+
def drawFigure(self):
36+
"""Draws a quadratic graph of the quadratic expression and returns a matplotlib figure"""
37+
x_axis = np.linspace(-10,10,50)
38+
y_axis = self.evaluate(x_axis)
39+
40+
figure, axes = plt.subplots()
41+
42+
axes.plot(x_axis,y_axis, linewidth = 1)
43+
axes.grid(visible = True)
44+
axes.set_title(self)
45+
axes.xaxis.set_minor_formatter(str(x_axis))
46+
return figure
47+

GUI/Quadratic-Equation-Solver/ui.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import ttkbootstrap as tb
2+
from ttkbootstrap.constants import *
3+
from tkinter import *
4+
from ttkbootstrap.scrolled import ScrolledFrame
5+
from quad import Quadratic
6+
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
7+
from ttkbootstrap.dialogs import Messagebox
8+
9+
# Most variables are named a,b,c according to ax² + bx + c in a quadratic equation
10+
11+
def plot():
12+
13+
# Collect all inputs
14+
a_entryInput = a_entry.get()
15+
b_entryInput = b_entry.get()
16+
c_entryInput = c_entry.get()
17+
18+
try:
19+
a = float(a_entryInput)
20+
b = float(b_entryInput)
21+
c = float(c_entryInput)
22+
eqn = Quadratic(a,b,c)
23+
24+
#--------------------------------------------------------------------------
25+
# Background Graph Frame
26+
graph_frame = ScrolledFrame(root, width = 800, height = 500)
27+
graph_frame.grid(row = 1, column = 0,pady = 10)
28+
29+
fig = eqn.drawFigure()
30+
31+
canvas = FigureCanvasTkAgg(figure = fig, master = graph_frame)
32+
canvas.draw()
33+
canvas.get_tk_widget().pack()
34+
35+
toolbar = NavigationToolbar2Tk(canvas, graph_frame)
36+
toolbar.update()
37+
38+
canvas.get_tk_widget().pack()
39+
solution_label.config(text = "Solution : x₁ = {0}, x₂ = {1}".format(eqn.solveQuad()[0], eqn.solveQuad()[1]))
40+
41+
except:
42+
Messagebox.show_error(title = "Error", message = "User entered wrong value")
43+
44+
45+
46+
# Base window widget
47+
root = tb.Window(themename="vapor")
48+
root.geometry("720x720")
49+
root.title("Quadratic Equation Solver")
50+
51+
# Font data
52+
font = ("Nunito", 12)
53+
54+
# Frame containing the entry for the three arguments
55+
top_frame = tb.Frame(root)
56+
top_frame.grid(row = 0, column = 0,padx = 10, pady = 20)
57+
58+
# Entry for the three arguments
59+
a_frame = tb.Frame(top_frame)
60+
a_frame.grid(row = 0, column = 0, padx=5)
61+
62+
a_label = tb.Label(a_frame, text= "a =", font = font)
63+
a_label.grid(row = 0, column = 0)
64+
65+
a_entry = tb.Entry(a_frame, width = 30, font = font)
66+
a_entry.grid(row = 0, column = 1)
67+
68+
b_frame = tb.Frame(top_frame)
69+
b_frame.grid(row = 0, column = 1, padx=5)
70+
71+
b_label = tb.Label(b_frame, text = "b =", font = font)
72+
b_label.grid(row = 0, column = 0)
73+
74+
b_entry = tb.Entry(b_frame, width = 30, font = font)
75+
b_entry.grid(row = 0, column = 1)
76+
77+
c_frame = tb.Frame(top_frame)
78+
c_frame.grid(row = 0, column = 2, padx=5)
79+
80+
c_label = tb.Label(c_frame, text = "c =", font = font)
81+
c_label.grid(row = 0, column = 0)
82+
83+
c_entry = tb.Entry(c_frame, width = 30, font = font)
84+
c_entry.grid(row = 0, column = 1)
85+
86+
87+
# Button to plot the matplotlib graph
88+
plot_button = tb.Button(top_frame, width = 70, text = "Plot", command = plot)
89+
plot_button.grid(row = 1, column = 1, pady = 15)
90+
91+
# Label containing the solution to the equation
92+
solution_label = tb.Label(root,font = (font[0], 15), text = "")
93+
solution_label.grid(row = 2, column = 0, pady = 10)
94+
95+
root.mainloop()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@ guide [HERE](https://github.com/larymak/Python-project-Scripts/blob/main/CONTRIB
121121
| 72 | [Star pattern](https://github.com/larymak/Python-project-Scripts/tree/main/OTHERS/Star%20pattern) | [LpCodes](https://github.com/LpCodes) |
122122
| 73 | [Logging Helper](https://github.com/larymak/Python-project-Scripts/tree/main/OTHERS/add-multiprocessing-logger) | [Jerry W.](https://github.com/Jerry0420) |
123123
| 74 | [Notepad](https://github.com/larymak/Python-project-Scripts/tree/main/PYTHON%20APPS/Notepad) | [Annarhysa Albert](https://github.com/Annarhysa) |
124+
| 75 | [Quadratic Equation Solver](https://github.com/larymak/Python-project-Scripts/tree/main/GUI/Quadratic-Equation-Solver) | [Akinfenwa Ezekiel](https://github.com/Ezek-iel) |

0 commit comments

Comments
 (0)