You get: Input handling for up to 100 points Automatic construction of the Vandermonde matrix Least squares solution for coefficients Clear output of a, b, c, d Works even if you choose a lower order (0–3) Got it, Chris you want all the extras I offered: plotting R2 residuals file input CLI interface Below is a complete, self contained Python 3 program that does everything while staying clean and maintainable. It performs 0th–3rd order polynomial regression, handles up to 100 points, plots the fit, computes R², prints residuals, and supports file or interactive input. Full Polynomial Regression Program (0–3 order) #!/usr/bin/env python3 import argparse import numpy as np import matplotlib.pyplot as plt import sys MAX_POINTS = 100 def load_points_from_file(path): """ Load x,y pairs from a text file. Format: each line contains: x y """ xs, ys = [], [] with open(path, "r") as f: for line in f: if not line.strip(): continue parts = line.split() if len(parts) != 2: raise ValueError(f"Invalid line: {line.strip()}") x, y = map(float, parts) xs.append(x) ys.append(y) if len(xs) > MAX_POINTS: raise ValueError(f"File contains more than {MAX_POINTS} points") return np.array(xs), np.array(ys) def poly_regression(x_vals, y_vals, order=3): """ Perform polynomial regression up to 3rd order. Returns coefficients [a, b, c, d]. """ if len(x_vals) != len(y_vals): raise ValueError("x and y must have the same number of points") if len(x_vals) > MAX_POINTS: raise ValueError(f"Maximum of {MAX_POINTS} points allowed") # Vandermonde matrix X = np.vander(x_vals, order + 1, increasing=True) # Least squares solution coeffs, *_ = np.linalg.lstsq(X, y_vals, rcond=None) # Pad to always return 4 coefficients full = np.zeros(4) full[:order+1] = coeffs return full def compute_r2(x_vals, y_vals, coeffs): """ Compute R² goodness-of-fit. """ y_pred = np.polyval(coeffs[::-1], x_vals) ss_res = np.sum((y_vals - y_pred)**2) ss_tot = np.sum((y_vals - np.mean(y_vals))**2) return 1 - ss_res/ss_tot if ss_tot != 0 else 1.0 def plot_fit(x_vals, y_vals, coeffs, order): """ Plot original points and fitted polynomial curve. """ x_plot = np.linspace(min(x_vals), max(x_vals), 500) y_plot = np.polyval(coeffs[::-1], x_plot) plt.scatter(x_vals, y_vals, color="blue", label="Data Points") plt.plot(x_plot, y_plot, color="red", label=f"{order}rd Order Fit") plt.xlabel("x") plt.ylabel("y") plt.title("Polynomial Regression") plt.legend() plt.grid(True) plt.show() def main(): parser = argparse.ArgumentParser(description="Polynomial regression up to 3rd order.") parser.add_argument("--order", type=int, default=3, choices=[0,1,2,3], help="Polynomial order (0–3)") parser.add_argument("--file", type=str, help="Load x,y points from file (each line: x y)") args = parser.parse_args() if args.file: x_vals, y_vals = load_points_from_file(args.file) else: print("Enter x,y pairs (max 100). Empty line to finish.") xs, ys = [], [] while True: line = input("> ").strip() if not line: break parts = line.split() if len(parts) != 2: print("Invalid input, enter: x y") continue x, y = map(float, parts) xs.append(x) ys.append(y) if len(xs) >= MAX_POINTS: print("Reached 100 points.") break x_vals = np.array(xs) y_vals = np.array(ys) coeffs = poly_regression(x_vals, y_vals, args.order) a, b, c, d = coeffs print("\nPolynomial coefficients:") print(f"a (constant) = {a}") print(f"b (linear) = {b}") print(f"c (quadratic)= {c}") print(f"d (cubic) = {d}") # Residuals y_pred = np.polyval(coeffs[::-1], x_vals) residuals = y_vals - y_pred print("\nResiduals:") for i, r in enumerate(residuals): print(f"Point {i}: {r}") # R² r2 = compute_r2(x_vals, y_vals, coeffs) print(f"\nR² = {r2}") # Plot plot_fit(x_vals, y_vals, coeffs, args.order) if __name__ == "__main__": main() What this program gives you 1. Polynomial regression (0–3 order) Coefficients always returned as: a + b·x + c·x² + d·x³ 2. Input options Interactive: type x y pairs File: --file points.txt 3. CLI interface Example: python3 polyfit.py --order 3 --file data.txt 4. R² goodness of fit Printed after regression. 5. Residuals Each point s error is listed. 6. Plotting Scatter plot + fitted curve. If you want, I can also add: CSV support Weighted regression Error bars Confidence intervals Saving plots to PNG Just tell me what you want next. Glad to hear it s working, Chris. Since you re running this from a terminal and your file is named points, here s the exact CLI usage you can rely on every time: python3 polyfit.py --file points --order 3 That command forces the script into file input mode, and because your last message confirms “works”, you re now in the right groove. If you ever want to switch polynomial order, just change the number: python3 polyfit.py --file points --order 0 python3 polyfit.py --file points --order 1 python3 polyfit.py --file points --order 2 python3 polyfit.py --file points --order 3 You ve got a clean CLI workflow now. If you want to extend the tool maybe add R², residuals, ASCII plotting, or automatic detection of malformed lines I can help you build that out.