Least squares line fit: a quadratic programming example, solved and checked
The least squares line fit is a classic quadratic programming problem. Here it is solved by a real solver (CLARABEL) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
Say it or snap it
You can give ProvenDecisions this problem out loud or as a photo. The app wrote down both, solved them, and got the same checked answer as the typed version.
What the app heard: “Fit the straight line y=m*x+b to 4 points by least squares. Here m is the slope and b is the intercept. The 4 points are x=1 with y=1, x=2 with y=3, x=3 with y=4, and x=4 with y=4. Choose the slope and the intercept to minimize the sum of the squared errors.”

The problem
minimize (m + b - 1)^2 + (2m + b - 3)^2 + (3m + b - 4)^2 + (4m + b - 4)^2
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
The model
Each line has a plain-language note on the right.
| make as small as possible: sum of squared residuals of the line y = m*x + b against the points (1,1), (2,3), (3,4), (4,4) |
In matrix form
With its numbers:
What is being decided
There are 2 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| b | real number | intercept of the fitted line (unitless) |
| m | real number | slope of the fitted line (unitless) |
Goal and limits
Minimize sum of squared residuals of the line y = m*x + b against the points (1,1), (2,3), (3,4), (4,4) subject to 0 constraints.
Problem type
Convex QP, complexity level 1: a small convex quadratic. Solved with CLARABEL at the Medium solver tier.
Picture
Shaded: every point that satisfies all the constraints. Dot: the optimum. Dashed: the objective's level line through it.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
b = cp.Variable(name='b') # intercept of the fitted line (unitless)
m = cp.Variable(name='m') # slope of the fitted line (unitless)
objective = cp.Minimize((b + m - 1)**2 + (b + 2*m - 3)**2 + (b + 3*m - 4)**2 + (b + 4*m - 4)**2)
constraints = [
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='CLARABEL')
print(problem.status, problem.value)
print('b =', b.value)
print('m =', m.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (1).
| Dual variable | Prices | Value |
|---|
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
minimize (m + b - 1)^2 + (2*m + b - 3)^2 + (3*m + b - 4)^2 + (4*m + b - 4)^2
subject to
m, b free
# m = slope of the fitted line (unitless)
# b = intercept of the fitted line (unitless)
# objective = sum of squared residuals of the line y = m*x + b against the points (1,1), (2,3), (3,4), (4,4)Checked: the app's answer matches the reference answer in our test set, which two solvers (CLARABEL, OSQP) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.