Least Squares Solver using QR Decomposition

Find the best approximate solution to \( A \mathbf{x} \approx \mathbf{b} \) using QR Decomposition.
Minimizes the Euclidean norm \( \| A \mathbf{x} - \mathbf{b} \|_2 \).

When the system $A\mathbf{x} = \mathbf{b}$ is overdetermined (more equations than unknowns), we find the solution that minimizes the residual:

$ \min_{\mathbf{x}} \| A\mathbf{x} - \mathbf{b} \|_2 $

QR Method:

Factor $A = QR$ where $Q$ is orthogonal and $R$ is upper triangular. Then solve:

$ R\mathbf{x} = Q^T\mathbf{b} $

Advantages:

  • More numerically stable than normal equations
  • Handles rank-deficient matrices gracefully
  • Provides minimum-norm solution for underdetermined systems
Equations \( m \): Variables \( n \):
Final Output as:
© 2025 Shelvean Kapita: kapita@tamu.edu
All code released under the MIT License.
Last modified: December 18, 2025