PhysSandbox
Classical MechanicsWaves & SoundElectricity & MagnetismOptics & LightGravity & OrbitsLabs
🌙Astronomy & The Sky🌡️Thermodynamics🌍Biophysics, Fluids & Geoscience📐Math Visualization🔧Engineering🧪Chemistry

Related simulators

Continue with similar topics in this category — or all 85 in Math Visualization.

View category →
NewSchool

Linear Regression: OLS, Ridge, Lasso & R²

Launch Simulator

Click/drag scatter points; fit y = β₀ + β₁x with OLS, Ridge (L2 on slope), or Lasso (L1 on slope). Spike Δy on the largest |x| point to see outlier sensitivity; compare SSE and R².

NewSchool

Savitzky–Golay Smoothing

Launch Simulator

Noisy cosine vs SG(7,2) convolution — preserves peaks better than a wide boxcar.

NewSchool

K-Means Clustering (Lloyd)

Launch Simulator

Click to add points, choose k, randomize centroids, then step Lloyd iterations (assign to nearest centroid, update means). Optional Gaussian-mixture demo; watch within-cluster SSE decrease.

NewSchool

2×2 Matrix & Eigenvectors

Launch Simulator

Grid deformation under M; real λ eigen-direction arrows.

NewSchool

Lotka–Volterra

Launch Simulator

N′ = αN−βNP, P′ = δNP−γP; phase plane RK4; equilibrium dot.

NewSchool

Random Walk

Launch Simulator

1D or 2D steps; trail and running mean ⟨r²⟩ vs diffusion intuition.

PhysSandbox

Interactive physics, chemistry, and engineering simulators for students, teachers, and curious minds.

Physics

  • Classical Mechanics
  • Waves & Sound
  • Electricity & Magnetism

Science

  • Optics & Light
  • Gravity & Orbits
  • Astronomy & The Sky

More

  • Thermodynamics
  • Biophysics, Fluids & Geoscience
  • Math Visualization
  • Engineering
  • Chemistry

© 2026 PhysSandbox. Free interactive science simulators.

PrivacyTermsContact
Home/Math Visualization/Least Squares Fit

Least Squares Fit

Noisy linear data; fitted slope and intercept with residuals.

Data

18
0.35
0.85
0.4

Minimize Σ(y_i − a x_i − b)². Closed form for a and b from normal equations; residuals are vertical segments to the fitted line.

Measured values

Fit a0.8383
Fit b0.3830
SSE0.657

About this model

Least squares fitting is a fundamental statistical method for finding the best-fitting line through a set of data points. This simulator visualizes the core principle: given a collection of (x, y) data with random scatter, it calculates and displays the linear model y = mx + b that minimizes the sum of the squared vertical distances (residuals) between the observed data points and the line. The method is derived from calculus, where the optimal slope (m) and y-intercept (b) are found by taking partial derivatives of the sum of squared residuals function, setting them to zero, and solving the resulting normal equations. The simulator simplifies the real-world complexity by assuming the data follows an underlying linear trend, that all uncertainty is in the y-direction, and that the residuals are normally distributed. By interacting with the controls, students directly observe how changing the slope and intercept alters the residuals and the total squared error. They learn to interpret the fitted line as a predictive model, understand the geometric meaning of residuals as vertical offsets, and see how the least squares criterion effectively balances over- and under-predictions across the entire dataset. This builds intuition for correlation, regression analysis, and the foundational concept of optimizing a model to describe noisy experimental data.

Who it's for: High school and introductory undergraduate students in statistics, physics, or any STEM field learning data analysis and curve fitting.

Key terms

  • Least Squares Fit
  • Linear Regression
  • Residual
  • Slope
  • Y-intercept
  • Sum of Squared Errors
  • Best-Fit Line
  • Normal Equations

How it works

Ordinary least squares finds the line that minimizes the sum of squared vertical errors. The same least-squares idea appears when fitting models to noisy measurements in experiments.

Frequently asked questions

Why do we square the residuals instead of just using their absolute values?
Squaring the residuals emphasizes larger errors more heavily, which makes the fit less sensitive to a few extreme outliers compared to a simple sum. Mathematically, the squared error function is differentiable everywhere, which allows us to use calculus to derive a unique, analytic solution for the best-fit parameters. Using absolute values is valid (called least absolute deviations) but often requires more complex iterative methods to solve.
Does the least squares line always pass through the average point (x̄, ȳ) of the data?
Yes, for a simple linear least squares fit, the best-fit line always passes through the centroid of the data, the point defined by the mean of the x-values and the mean of the y-values. This is a direct mathematical consequence of the normal equations used to derive the slope and intercept.
What is a real-world example where least squares fitting is used?
Least squares is ubiquitous in science and engineering. For instance, in physics, it's used to determine the acceleration due to gravity from noisy position-time data in a free-fall experiment. In economics, it might model the relationship between consumer income and spending. Any time you see a 'line of best fit' in a scatter plot, it's likely calculated via least squares.
What is a key limitation of the simple linear model shown here?
This model assumes the relationship is strictly linear. It will produce a misleading fit if the true underlying trend is curved (e.g., quadratic or exponential). It also assumes the scatter (noise) is consistent across all x-values (homoscedasticity) and that data points are independent. Real data often violate these assumptions, requiring more advanced models.