∇ProvenDecisions
Ctrl K

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.

Answer. The smallest objective value is 1, at b = 0.5, m = 1. Optimal, numerically checked.

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.

🎙️ Spoken (22 seconds)

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.”

📷 Photographed
Photo of the least squares line fit written on a note

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.

VariableTypeMeaning
breal numberintercept of the fitted line (unitless)
mreal numberslope 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.

(0.5, 1)-0.4-0.4-0.0750.050.250.50.5750.950.91.4bm

The answer at a glance

The optimal value of each decision, as the solver returned it.

b0.5m1

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 variablePricesValue

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)

Change the numbers and solve it

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

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: