Aroy1997 commited on
Commit
ddb99ef
Β·
verified Β·
1 Parent(s): 71575e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import gradio as gr
4
+
5
+ def plot_polynomial(degree, coeff_input):
6
+ try:
7
+ coeffs = list(map(float, coeff_input.strip().split()))
8
+ if len(coeffs) != degree + 1:
9
+ return "⚠️ Please enter exactly {} coefficients.".format(degree + 1), None
10
+
11
+ coeffs = np.array(coeffs)
12
+ roots = np.roots(coeffs)
13
+ real_roots = [r.real for r in roots if np.isclose(r.imag, 0, atol=1e-5)]
14
+
15
+ # Plotting
16
+ x_vals = np.linspace(-10, 10, 400)
17
+ y_vals = np.polyval(coeffs, x_vals)
18
+
19
+ fig, ax = plt.subplots(figsize=(6, 4))
20
+ ax.plot(x_vals, y_vals, label="Polynomial Curve", color="blue")
21
+ ax.axhline(0, color='black', linewidth=0.5)
22
+ ax.axvline(0, color='black', linewidth=0.5)
23
+
24
+ for root in real_roots:
25
+ ax.scatter(root, 0, color="red", s=100)
26
+
27
+ ax.set_title("πŸ“ˆ Graph of the Polynomial")
28
+ ax.set_xlabel("x")
29
+ ax.set_ylabel("f(x)")
30
+ ax.grid(True, linestyle='--', linewidth=0.5)
31
+ ax.legend()
32
+
33
+ return "βœ… Real roots: {}".format(real_roots if real_roots else "None found."), fig
34
+ except Exception as e:
35
+ return f"❌ Error: {e}", None
36
+
37
+ # Gradio UI
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("## πŸŽ‰ Welcome to MathWizz πŸŽ‰\nPlot and Analyze Your Polynomial")
40
+
41
+ degree = gr.Slider(label="Select Degree", minimum=1, maximum=10, value=2, step=1)
42
+ coeff_input = gr.Textbox(label="Enter Coefficients (space-separated)", placeholder="e.g. 1 -3 2")
43
+
44
+ output_text = gr.Textbox(label="Result")
45
+ output_plot = gr.Plot(label="Polynomial Curve")
46
+
47
+ submit = gr.Button("Plot Polynomial")
48
+
49
+ submit.click(fn=plot_polynomial,
50
+ inputs=[degree, coeff_input],
51
+ outputs=[output_text, output_plot])
52
+
53
+ demo.launch()