r3hab commited on
Commit
8f655b1
·
verified ·
1 Parent(s): 6546e75

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+
5
+ def lagrange_basis(x, i, x_points):
6
+ basis = 1.0
7
+ st.latex(f"L_{{{i}}}(x) = ")
8
+ for j in range(len(x_points)):
9
+ if j != i:
10
+ basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
11
+ st.latex(f"\\cdot \\frac{{(x - x_{{{j}}})}}{{(x_{{{i}}} - x_{{{j}}})}} = \\frac{{({x} - {x_points[j]})}}{{({x_points[i]} - {x_points[j]})}}")
12
+ return basis
13
+
14
+ def lagrange_interpolation(x, x_points, y_points):
15
+ result = 0.0
16
+ st.header("Calculation Steps")
17
+
18
+ for i in range(len(x_points)):
19
+ with st.expander(f"Term for (x₀,y₀) = ({x_points[i]}, {y_points[i]})", expanded=True):
20
+ st.subheader(f"Calculating L_{i}({x}) * y_{i}")
21
+
22
+ col1, col2 = st.columns(2)
23
+ with col1:
24
+ st.markdown("**Basis Polynomial Calculation**")
25
+ basis = lagrange_basis(x, i, x_points)
26
+
27
+ with col2:
28
+ st.markdown("**Term Contribution**")
29
+ term = y_points[i] * basis
30
+ st.latex(f"y_{i} \\cdot L_{i}({x}) = {y_points[i]} \\times {basis:.4f} = {term:.4f}")
31
+
32
+ result += term
33
+ st.markdown(f"**Current Total**: {result:.4f}")
34
+
35
+ return result
36
+
37
+ # Streamlit UI
38
+ st.title("Lagrange Interpolation Visualizer")
39
+ st.markdown("Interactive calculator with step-by-step LaTeX visualization")
40
+
41
+ # Input section
42
+ st.sidebar.header("Input Parameters")
43
+ x_points = st.sidebar.text_input("X values (comma separated)", "1500,1600,1700,1900").split(',')
44
+ x_points = [float(x.strip()) for x in x_points]
45
+ y_points = st.sidebar.text_input("Y values (comma separated)", "1234,2345,4567,6789").split(',')
46
+ y_points = [float(y.strip()) for y in y_points]
47
+ x_target = st.sidebar.number_input("Target X value", value=1800.0)
48
+
49
+ # Main calculation
50
+ if len(x_points) != len(y_points):
51
+ st.error("X and Y values must have the same number of elements!")
52
+ else:
53
+ st.header("Interpolation Formula")
54
+ st.latex(r"P(x) = \sum_{i=0}^{n} y_i \cdot L_i(x)")
55
+ st.latex(r"L_i(x) = \prod_{\substack{j=0 \\ j \neq i}}^{n} \frac{x - x_j}{x_i - x_j}")
56
+
57
+ result = lagrange_interpolation(x_target, x_points, y_points)
58
+
59
+ st.success(f"**Final Interpolated Value**: P({x_target}) = {result:.2f}")
60
+
61
+ # Visualization
62
+ st.header("Visualization")
63
+ fig, ax = plt.subplots()
64
+
65
+ # Plot original points
66
+ ax.scatter(x_points, y_points, c='red', label='Original Data')
67
+
68
+ # Plot interpolated point
69
+ ax.scatter([x_target], [result], c='blue', s=100, label='Interpolated Point')
70
+
71
+ # Plot polynomial curve
72
+ x_vals = np.linspace(min(x_points)-50, max(x_points)+50, 400)
73
+ y_vals = [lagrange_interpolation(x, x_points, y_points) for x in x_vals]
74
+ ax.plot(x_vals, y_vals, '--', label='Interpolation Polynomial')
75
+
76
+ ax.set_xlabel('X')
77
+ ax.set_ylabel('Y')
78
+ ax.legend()
79
+ st.pyplot(fig)