r3hab commited on
Commit
42d27aa
·
verified ·
1 Parent(s): 30639aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -52,7 +52,7 @@ def newton_interpolation_steps_latex(x, y, x_interp):
52
  steps_latex = []
53
 
54
  # Initial step in LaTeX
55
- steps_latex.append(f"Initial value: $P(x) = f[x_0] = {y_interp:.4f}$")
56
 
57
  for i in range(1, n):
58
  term = 1.0
@@ -61,31 +61,31 @@ def newton_interpolation_steps_latex(x, y, x_interp):
61
  current_term_value = dd_table[0][i] * term
62
 
63
  # Construct divided difference notation in LaTeX: f[x_0, ..., x_i]
64
- dd_notation_latex = f"f[x_0"
65
  for k in range(1, i + 1):
66
- dd_notation_latex += f", x_{k}"
67
- dd_notation_latex += "]"
68
 
69
  # Construct polynomial term in LaTeX: (x - x_0)(x - x_1)...(x - x_{i-1})
70
  polynomial_term_latex = ""
71
  for j in range(i):
72
- polynomial_term_latex += f"(x - {x[j]})"
73
  if not polynomial_term_latex:
74
- polynomial_term_latex = "1" # For i=0 case which is handled outside loop, but for consistency
75
 
76
  # Step description in LaTeX
77
- step_latex_description = f"Add term {i}: ${dd_notation_latex} \\times {polynomial_term_latex.replace('x', str(x_interp))} = {dd_table[0][i]:.4f} \\times "
78
  if i > 0:
79
- term_values_latex = " \\times ".join([f"({x_interp} - {x[j]})" for j in range(i)])
80
  step_latex_description += term_values_latex
81
  else:
82
- step_latex_description = f"Initial value: $f[x_0] = {dd_table[0][0]:.4f}$" # Should not happen in loop, but for clarity
83
 
84
- step_latex_description += f" = {current_term_value:.4f}$"
85
  steps_latex.append(step_latex_description)
86
 
87
- steps_latex.append(f"Current $P(x) = {y_interp:.4f}$")
88
- y_interp += current_term_value
89
 
90
  return y_interp, dd_table_df, steps_latex
91
 
@@ -93,9 +93,9 @@ def newton_interpolation_steps_latex(x, y, x_interp):
93
  st.title("Newton's Divided Difference Interpolation")
94
  st.write("Enter the x and y values as comma-separated lists, and the x value you want to interpolate.")
95
 
96
- x_values_input = st.text_area("Enter x values (comma-separated):", "0, 1, 2")
97
- y_values_input = st.text_area("Enter y values (comma-separated):", "1, 3, 2")
98
- x_interpolate = st.number_input("Enter x value to interpolate:", value=1.5)
99
 
100
  if st.button("Calculate Interpolated Value with Steps (LaTeX)"):
101
  try:
 
52
  steps_latex = []
53
 
54
  # Initial step in LaTeX
55
+ steps_latex.append(r"Initial value: $P(x) = f[x_0] = {:.4f}$".format(y_interp))
56
 
57
  for i in range(1, n):
58
  term = 1.0
 
61
  current_term_value = dd_table[0][i] * term
62
 
63
  # Construct divided difference notation in LaTeX: f[x_0, ..., x_i]
64
+ dd_notation_latex = r"f[x_0"
65
  for k in range(1, i + 1):
66
+ dd_notation_latex += r", x_{" + str(k) + r"}"
67
+ dd_notation_latex += r"]"
68
 
69
  # Construct polynomial term in LaTeX: (x - x_0)(x - x_1)...(x - x_{i-1})
70
  polynomial_term_latex = ""
71
  for j in range(i):
72
+ polynomial_term_latex += r"(x - " + str(x[j]) + r")"
73
  if not polynomial_term_latex:
74
+ polynomial_term_latex = "1"
75
 
76
  # Step description in LaTeX
77
+ step_latex_description = r"Add term {}: ${} \times {} = {:.4f} \times $".format(i, dd_notation_latex, polynomial_term_latex.replace('x', str(x_interp)), dd_table[0][i])
78
  if i > 0:
79
+ term_values_latex = r" $\times$ ".join([r"({:.1f} - {})".format(x_interp, val) for val in x[:i]]) # more robust float formatting
80
  step_latex_description += term_values_latex
81
  else:
82
+ step_latex_description = r"Initial value: $f[x_0] = {:.4f}$".format(dd_table[0][0]) # Should not happen in loop, but for clarity
83
 
84
+ step_latex_description += r" = {:.4f}$".format(current_term_value)
85
  steps_latex.append(step_latex_description)
86
 
87
+ steps_latex.append(r"Current $P(x) = {:.4f}$".format(y_interp + current_term_value)) # Corrected accumulation here
88
+ y_interp += current_term_value # Accumulate interpolation value here
89
 
90
  return y_interp, dd_table_df, steps_latex
91
 
 
93
  st.title("Newton's Divided Difference Interpolation")
94
  st.write("Enter the x and y values as comma-separated lists, and the x value you want to interpolate.")
95
 
96
+ x_values_input = st.text_area("Enter x values (comma-separated):", "2000, 2005, 2010, 2015")
97
+ y_values_input = st.text_area("Enter y values (comma-separated):", "1000, 1040, 1032, 1025")
98
+ x_interpolate = st.number_input("Enter x value to interpolate:", value=2001.0)
99
 
100
  if st.button("Calculate Interpolated Value with Steps (LaTeX)"):
101
  try: