Lesson 4 of 15
Least Squares via QR
Least Squares via QR
For an overdetermined system (more equations than unknowns), the least-squares solution minimises .
Why QR?
The normal equations () work but are numerically unstable — squaring doubles the condition number. QR decomposition solves the same problem with far better numerical stability.
Algorithm
Given thin QR decomposition ( is , is ):
Solve this upper-triangular system via back-substitution — done.
Example
Fit to data:
The best-fit line is .
Your Task
Implement least_squares_qr(A, b) that solves the least-squares problem using QR decomposition. Build on qr_decompose and add backward_sub for back-substitution.
Python runtime loading...
Loading...
Click "Run" to execute your code.