# Lecture 01 The Geometry of Linear Equations


Today's lecture contains:<br/>
1. 2D Linear Equations<br/>
2. 3D Linear Equations<br/>
3. Matrix Multiple Vector<br/>

For all equations, we focus on TWO big concepts: **row picture** and **column picture.**<br/>
<br/>
<br/>

## 1. 2D Linear Equations

Suppose we have two equations with two unknown:

\begin{align}
2x-y&=0\\
-x+2y&=3
\end{align}<br/>

Such equations can be written to matrix format: 

\begin{align}
\begin{bmatrix}2&-1\\-1&2\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}
\end{align}

Usually we call the first matrix as **the coefficient matrix** denoted as $A$, the second part as the **unknown** $x$, and the third part as **the right hand side** $b$. Therefore, the system of linear equations can be denoted as $Ax=b$.</br>

### 1.1 Row Picture of 2D Linear Equations
To solve above linear system, one of the methods to tackle is called **row picture**. It is simply applied the math from high school:</br>

a. Find two points of each equation<br/>

b. Connect two points to a line<br/>

c. The joint is the answer.<br/>


```python
%gui qt
from mayavi import mlab

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D
import scipy as sp
import scipy.linalg
import sympy as sy
sy.init_printing() 
np.set_printoptions(precision=3)
np.set_printoptions(suppress=True)
```

    
    Bad key "text.kerning_factor" on line 4 in
    C:\Users\xingx\anaconda3\lib\site-packages\matplotlib\mpl-data\stylelib\_classic_test_patch.mplstyle.
    You probably need to get an updated matplotlibrc file from
    http://github.com/matplotlib/matplotlib/blob/master/matplotlibrc.template
    or from the matplotlib source distribution



```python
mlab.init_notebook(backend='x3d')
```

    Notebook initialized with x3d backend.


For the first equation,\begin{align}
2x-y=0
\end{align}<br/>

the solution will be:
\begin{align}
x=0, y=0\\
x=2, y=4\
\end{align}<br/>

For the second equation,\begin{align}
-x+2y=3
\end{align}<br/>

the solution will be:
\begin{align}
x=-3, y=0\\
x=1, y=2\
\end{align}<br/>

Hence, the intersection of two line would be:\begin{align}
(x, y)^T = (1, 2)^T
\end{align}<br/>


```python
x = np.linspace(-5, 5, 100)
y1 = 2*x
y2 = (1/2)*x + 3/2

fig, ax = plt.subplots(figsize = (12, 7))
#plot point
ax.scatter(1, 2, s = 200, zorder=5, color = 'r', alpha = .8) 
ax.scatter(0, 0, s = 100, zorder=5, color = 'k', alpha = .8) 
ax.scatter(2, 4, s = 100, zorder=5, color = 'k', alpha = .8) 
ax.scatter(-3, 0, s = 100, zorder=5, color = 'k', alpha = .8) 
#plot line
ax.plot(x, y1, x, y2, lw = 3)
ax.plot([1, 1], [0, 2], ls = '--', color = 'b', alpha = .5)
ax.plot([0, 1], [2, 2], ls = '--', color = 'b', alpha = .5)
ax.plot([-5, 5], [0, 0], ls = '-', color = 'k', linewidth=5, alpha = .8)
ax.plot([0, 0], [-5, 5], ls = '-', color = 'k', linewidth=5, alpha = .8)
#set plot range
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])

#plot text
s = '$(1,2)$'
pt1 = '$(-3,0)$'
pt2 = '$(0,0)$'
pt3 = '$(2,4)$'
ax.text(1, 2, s, fontsize = 20)
ax.text(-3, .3, pt1, fontsize = 20)
ax.text(0, .3, pt2, fontsize = 20)
ax.text(2, 4, pt3, fontsize = 20)

#
ax.set_title('Row Picture: Solution of $2x-y=0$, $-x+2y=3$', size = 22)
ax.grid()
```

### 1.2 Column Picture of 2D Linear Equations

Another perspective to see this equation is **column picture**. The above matrix

\begin{align}
\begin{bmatrix}2&-1\\-1&2\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}
\end{align}
can be converted to:

\begin{align}
x\begin{bmatrix}2\\-1\end{bmatrix}+y\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}
\end{align}
We call the first vector$\begin{bmatrix}2\\-1\end{bmatrix}$ as $col_1$，second vector$\begin{bmatrix}-1\\2\end{bmatrix}$ as $col_2$.

That said,to satisfy the output vector as $\begin{bmatrix}3\\0\end{bmatrix}$. We have to  $2×col_1$, $2×col_2$

\begin{align}
1\begin{bmatrix}2\\-1\end{bmatrix}+2\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}
\end{align}

Hence, the column picture can be illustrated as followed:


```python
from functools import partial

fig, ax = plt.subplots(figsize = (12, 7))
plt.axhline(y=0, c='black', linewidth=5)
plt.axvline(x=0, c='black', linewidth=5)
ax = plt.gca()

#plot vector
arrow_vector = partial(plt.arrow, width=0.01, head_width=0.1, head_length=0.2, length_includes_head=True)
arrow_vector(0, 0, 2, -1, color='r')
arrow_vector(0, 0, -1, 2, color='g')
arrow_vector(2, -1, -2, 4, color='k')
arrow_vector(0, 0, 0, 3, width=0.05, color='b')
#plot point
ax.scatter(2, -1, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(0, 0, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(0, 3, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(-1, 2, s = 200, zorder=5, color = 'k', alpha = .8) 

#set plot range
ax.set_xlim([-2, 3])
ax.set_ylim([-2, 4])

#plot text
col_1 = '$col_1$'
col_2 = '$col_2$'
col_3 = '1·$col_1$+2·$col_2$'
ax.text(1, -.5, col_1, fontsize = 20)
ax.text(-.5, 1, col_2, fontsize = 20)
ax.text(1, 1.5, col_3, fontsize = 20)

#
ax.set_title('Column Picture: Solution of $2x-y=0$, $-x+2y=3$', size = 22)
ax.grid()
```


So we have this kind of linear combination: 
\begin{align}
x\begin{bmatrix}2\\-1\end{bmatrix}+y\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}
\end{align}
That said, the linear combination of $col_1,col_2$ is vector $b$. Then what would be all the linear combination of $col_1,col_2$<br/>


**The whole plane!**



```python
x, y = np.arange(-10, 11, 5), np.arange(-10, 11, 5)
X, Y = np.meshgrid(x, y)

fig, ax = plt.subplots(figsize = (8, 8))
plt.axhline(y=0, c='black', linewidth=5)
plt.axvline(x=0, c='black', linewidth=5)
ax.scatter(X, Y, s = 200, color = 'red', zorder = 3)
ax.axis([-15, 15, -15, 15])
ax.grid()
```

## 2. 3D Linear Equations

Then we move from 2D to 3D. Suppose we have 3 equations as followed:
\begin{align}
\begin{cases}2x&-y&&=0\\-x&+2y&-z&=-1\\&-3y&+4z&=4\end{cases}
\end{align}

Such equations can be written to matrix format: 

\begin{align}
A=\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-3&4\end{bmatrix},\ b=\begin{bmatrix}0\\-1\\4\end{bmatrix}
\end{align}

### 2.1 Row Picture of 2D Linear Equations

The above linear system can be written as followed in row picture:
\begin{align}
\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-3&4\end{bmatrix}\begin{bmatrix}x\\y\\z\end{bmatrix}=\begin{bmatrix}0\\-1\\4\end{bmatrix}
\end{align}

First, we try to figure out this linear system from row picture.<br/>
In 2D space, the unknown can be illustrated as **2 lines** in a plane.<br/> 
In 3D space, the unknown can be illustrated as **3 planes** in a space.

In the end, 3 plane will be intersected in a point **if** the three equations are independent.

---
TO-DO: Visualize 3D plane intersection. (matplotlib is fake 3d)

### 2.2 Column Picture of 2D Linear Equations
Column picture is Prof. Strang's favorite which is much easier to be understood. The above linear system can be written into column picture as followed:
\begin{align}
x\begin{bmatrix}2\\-1\\0\end{bmatrix}+y\begin{bmatrix}-1\\2\\-3\end{bmatrix}+z\begin{bmatrix}0\\-1\\4\end{bmatrix}=\begin{bmatrix}0\\-1\\4\end{bmatrix}
\end{align}


```python
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import


fig = plt.figure(figsize=(5,5))
ax = fig.gca(projection='3d')

# Make the grid
x, y, z = np.zeros((3,3))

# Make the direction data for the arrows
u=np.array([2,-1,0])
w=np.array([-1,2,-3])
v=np.array([0,-1,4])

ax.quiver(x, y, z, u, v, w)
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
ax.set_zlim([-5,5])

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
```


    <IPython.core.display.Javascript object>






## 3. Matrix Multiple Vector

There are two ways of multiplication between matrix and vector. Assume we have:
\begin{align}
\begin{bmatrix}2&5\\1&3\end{bmatrix}\begin{bmatrix}1\\2\end{bmatrix}=?
\end{align}

### 3.1 Solving in row picture 
\begin{align}
\begin{bmatrix}2&5\\1&3\end{bmatrix}\begin{bmatrix}1\\2\end{bmatrix}=\begin{bmatrix}2×1&5×2\\1×1&3×2\end{bmatrix}=\begin{bmatrix}12\\7\end{bmatrix}
\end{align}

### 3.2 Solving in column picture 
This is favored by Prof. Strang.
\begin{align}
\begin{bmatrix}2&5\\1&3\end{bmatrix}\begin{bmatrix}1\\2\end{bmatrix}=1\begin{bmatrix}2\\1\end{bmatrix}+2\begin{bmatrix}5\\3\end{bmatrix}=\begin{bmatrix}12\\7\end{bmatrix}
\end{align}

355392c81b675d5ca944a20e60df5bb3e01d26ae
ipynb
Jupyter Notebook
Lecture 01 The Geometry of Linear Equations.ipynb
XingxinHE/Linear_Algebra
7d6b78699f8653ece60e07765fd485dd36b26194
MIT
2021-04-24T17:23:50.000Z
2021-11-27T11:00:04.000Z
Lecture 01 The Geometry of Linear Equations.ipynb
XingxinHE/Linear_Algebra
7d6b78699f8653ece60e07765fd485dd36b26194
MIT
Lecture 01 The Geometry of Linear Equations.ipynb
XingxinHE/Linear_Algebra
7d6b78699f8653ece60e07765fd485dd36b26194
MIT
Qwen/Qwen-72B
1. YES
2. YES