# Section 5.6 $\quad$ Least Squares

**Recall** An $m\times n$ linear system $A\mathbf{x}=\mathbf{b}$ is consistent if and only if <br /><br /><br /><br />

**Question** What can we do if the system $A\mathbf{x}=\mathbf{b}$ is inconsistent?<br /><br /><br /><br />

>The **least square solution** to the linear system $A\mathbf{x}=\mathbf{b}$ is the solution to the system<br /><br /><br /><br />

**Remark** If $A$ is an $m\times n$ matrix,<br /><br /><br /><br />

### Example 1

Determine the least square solution to $A\mathbf{x}=\mathbf{b}$, where
\begin{equation*}
  A =
  \left[
    \begin{array}{cc}
      2 & 1 \\
      1 & 0 \\
      0 & -1 \\
      -1 & 1 \\
    \end{array}
  \right],~~~~~~
  \mathbf{b} =
  \left[
    \begin{array}{c}
      3 \\
      1 \\
      2 \\
      -1\\
    \end{array}
  \right].
\end{equation*}


```python
from sympy import *

A = Matrix([[2, 1], [1, 0], [0, -1], [-1, 1]]);
b = Matrix([3, 1, 2, -1]);

A.LDLsolve(b)
```




    Matrix([
    [24/17],
    [-8/17]])



Least square problems often arise in constructing a mathematical model from discrete data.

### Example 2

The following data shows U.S. per capita health care expenditures

Year | Per Capita Expenditures (in \$) 
-----|------
1960    | $\qquad\qquad$ 143    
1970    | $\qquad\qquad$ 348    
1980    | $\qquad\qquad$ 1,067  
1990    | $\qquad\qquad$ 2,738
1995    | $\qquad\qquad$ 3,698 
2000    | $\qquad\qquad$ 4,560

- Determine the line of best fit to the given data.
- Predict the per capita expenditure for the year 2005, 2010, and 2015.


```python
from sympy import *
import numpy as np
import matplotlib.pyplot as plt

A = Matrix([[1960, 1], [1970, 1], [1980, 1], [1990, 1], [1995, 1], [2000, 1]]);
b = Matrix([143, 348, 1067, 2738, 3698, 4560]);
linePara = A.LDLsolve(b);

plt.xlabel('Year');
plt.ylabel('Per Capita Expenditures (in $)');
plt.title('U.S. per capita health care expenditures');
plt.plot(A[:,0], b, 'o', label = 'Data');
x = np.linspace(1950, 2030, 1000);
y = x * linePara[0] + linePara[1];
plt.plot(x, y, label = 'Prediction');
plt.legend();
plt.show()

2005*linePara[0] + linePara[1], 2010*linePara[0] + linePara[1], 2015*linePara[0] + linePara[1]
```




    (14026/3, 15748/3, 17470/3)



26d84f61393c5574b734ca30b8dcbf8e41ce0865
ipynb
Jupyter Notebook
Jupyter_Notes/Lecture29_Sec5-6_LeastSquares.ipynb
xiuquan0418/MAT341
2fb7ec4e5f0771f10719cb5e4a00a7ab07c49b59
MIT
Jupyter_Notes/Lecture29_Sec5-6_LeastSquares.ipynb
xiuquan0418/MAT341
2fb7ec4e5f0771f10719cb5e4a00a7ab07c49b59
MIT
Jupyter_Notes/Lecture29_Sec5-6_LeastSquares.ipynb
xiuquan0418/MAT341
2fb7ec4e5f0771f10719cb5e4a00a7ab07c49b59
MIT
Qwen/Qwen-72B
1. YES
2. YES