devendergarg14 commited on
Commit
fb0e940
1 Parent(s): 24cb81f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+ def linear_interpolation(x, y, x_interp):
6
+ return np.interp(x_interp, x, y)
7
+
8
+ def quadratic_interpolation(x, y, x_interp):
9
+ coeffs = np.polyfit(x, y, 2)
10
+ return np.polyval(coeffs, x_interp)
11
+
12
+ def lagrange_interpolation(x, y, x_interp):
13
+ n = len(x)
14
+ y_interp = np.zeros_like(x_interp, dtype=float)
15
+
16
+ for i in range(n):
17
+ p = y[i]
18
+ for j in range(n):
19
+ if i != j:
20
+ p = p * (x_interp - x[j]) / (x[i] - x[j])
21
+ y_interp += p
22
+
23
+ return y_interp
24
+
25
+ def interpolate_and_plot(x_input, y_input, x_predict):
26
+ x = np.array([float(val.strip()) for val in x_input.split(',')])
27
+ y = np.array([float(val.strip()) for val in y_input.split(',')])
28
+
29
+ if len(x) != len(y):
30
+ return "Error: Number of x and y values must be the same.", None
31
+
32
+ x_interp = np.linspace(min(x), max(x), 100)
33
+
34
+ if len(x) == 2:
35
+ y_interp = linear_interpolation(x, y, x_interp)
36
+ method = "Linear"
37
+ elif len(x) == 3:
38
+ y_interp = quadratic_interpolation(x, y, x_interp)
39
+ method = "Quadratic"
40
+ else:
41
+ y_interp = lagrange_interpolation(x, y, x_interp)
42
+ method = "Lagrange"
43
+
44
+ plt.figure(figsize=(10, 6))
45
+ plt.scatter(x, y, color='red', label='Input points')
46
+ plt.plot(x_interp, y_interp, label=f'{method} interpolant')
47
+ plt.xlabel('x')
48
+ plt.ylabel('y')
49
+ plt.title(f'{method} Interpolation')
50
+ plt.legend()
51
+ plt.grid(True)
52
+
53
+ # Predict y value for given x
54
+ if x_predict is not None:
55
+ if x_predict < min(x) or x_predict > max(x):
56
+ return plt, f"Error: Prediction x value must be between {min(x)} and {max(x)}."
57
+
58
+ if len(x) == 2:
59
+ y_predict = linear_interpolation(x, y, [x_predict])[0]
60
+ elif len(x) == 3:
61
+ y_predict = quadratic_interpolation(x, y, [x_predict])[0]
62
+ else:
63
+ y_predict = lagrange_interpolation(x, y, [x_predict])[0]
64
+
65
+ plt.scatter([x_predict], [y_predict], color='green', s=100, label='Predicted point')
66
+ plt.legend()
67
+
68
+ return plt, f"Predicted y value for x = {x_predict}: {y_predict:.4f}"
69
+
70
+ return plt, None
71
+
72
+ iface = gr.Interface(
73
+ fn=interpolate_and_plot,
74
+ inputs=[
75
+ gr.Textbox(label="X values (comma-separated)"),
76
+ gr.Textbox(label="Y values (comma-separated)"),
77
+ gr.Number(label="X value to predict (optional)")
78
+ ],
79
+ outputs=[
80
+ gr.Plot(label="Interpolation Plot"),
81
+ gr.Textbox(label="Predicted Y value")
82
+ ],
83
+ title="Interpolation App",
84
+ description="Enter x and y values to see the interpolation graph. The method will be chosen based on the number of points:\n2 points: Linear, 3 points: Quadratic, >3 points: Lagrange.\n Optionally, enter an x value (between min and max of input x values) to predict its corresponding y value."
85
+ )
86
+
87
+ iface.launch()