summaryrefslogtreecommitdiff
path: root/tutorials/module_3/3_system_of_equations.md
blob: b30ce78e7cd2baf79f403abe66a3b564aacd4b3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Linear Equations
Let's consider an linear equation
$$
y = m x + b
$$
where $a$ and $b$ are two known constants we can solve for $x$ easily.

## Problem 1
[]

# Linear Algebra
Although this isn't a course in linear algebra we are going to use some fundamental concepts from linear algebra to solve systems of equations. If you haven't taken linear algebra before, it is the study of linear equations. These equations can be represented in the form of matrices. Matrices are rectangular arrays of numbers arranged in rows and columns. They are widely used in both engineering and computer science. Let's say we have the following system of equation.
$$
\begin{cases}
a_{11} x_1 + a_{12} x_2 + a_{13} x_3 = b_1 \\
a_{21} x_1 + a_{22} x_2 + a_{23} x_3 = b_2 \\
a_{31} x_1 + a_{32} x_2 + a_{33} x_3 = b_3
\end{cases}
\tag{1}

$$
Where both the $a_{nn}$ coefficients and the $b_n$ variables are all known. We can re-write this system into a matrix form by creating an $A$ matrix of all $a_{nn}$ values and a vector in the form of:
$$
  \left[ {\begin{array}{cc}
    a_{11} & a_{12} & a_{13}\\
    a_{21} & a_{22} & a_{23}\\
    a_{31} & a_{32} & a_{33}\\
  \end{array} } \right]
  \left[ {\begin{array}{cc}
    x_{1}\\
    x_{2}\\
    x_{3}\\
  \end{array} } \right]
=
  \left[ {\begin{array}{cc}
    b_{1}\\
    b_{2}\\
    b_{3}\\
  \end{array} } \right]
  \tag{2}
$$
where
$$
    \left[ {\begin{array}{cc}
    a_{11} & a_{12} & a_{13}\\
    a_{21} & a_{22} & a_{23}\\
    a_{31} & a_{32} & a_{33}\\
  \end{array} } \right]
  = A,
  \left[ {\begin{array}{cc}
    b_{1}\\
    b_{2}\\
    b_{3}\\
  \end{array} } \right]
    = b
$$
we can also write the equation in it's simplified form
$$
Ax=b
\tag{3}
$$

Take a close look at equation 1 and 2 and look at their similarity and differences. Notice how the pattern of the $A$ matrix and $b$ vector and follow the same patterns as however the elements of the $x$ vector ($x_1$,$x_2$,$x_3$) are flipped vertically. This is because of how we multiple matrices

## Matrix operations in python

- Matrices of the same size can be added or subtracted element-wise:
- Multiply a matrix by a scalaryields a new matrix where every element is multiplied by the scalar.
- The determinant of a matrix is 
- **Identity Matrix $I$**: Square matrix with 1’s on the diagonal and 0’s elsewhere.
- **Inverse Matrix $A^{-1}$**: Satisfies $AA^{-1} = I$. (Only square, non-singular matrices have inverses.)
### Matrix Multiplication

If $A$ is $m \times n$ and $B$ is $n \times p$, their product $C = AB$ is an $m \times p$ matrix:

cij=∑k=1naikbkjc_{ij} = \sum_{k=1}^n a_{ik} b_{kj}

**Example:**
$$
[1234][5678]=[1⋅5+2⋅71⋅6+2⋅83⋅5+4⋅73⋅6+4⋅8]=[19224350]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 1 \cdot 5 + 2 \cdot 7 & 1 \cdot 6 + 2 \cdot 8 \\ 3 \cdot 5 + 4 \cdot 7 & 3 \cdot 6 + 4 \cdot 8 \end{bmatrix} = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}
$$


## 1) Matrix Math in Python
```python
import numpy as np
np.set_printoptions(suppress=True)  # nicer printing
```

---



Let's
```python
A = np.array([[1, 3],
              [2, 4]])      # 2x2
B = np.array([[5, 6],
              [7, 8]])      # 2x2
A.shape, B.shape

b = np.array([3, 5])
b.shape

# Addition and Subtraction of same shapes
A + B
A - B

# Scalar Multiplication
3*A

5*B

# Matrix Multiplication
mat_mult = A @ B


# Transpose
B.T

# Identity and Inverse matrix

I = np.eye(2)
A_inv = np.linalg.inv(A)

A @ A_inv, A_inv @ A

# Determinants

detA = np.linalg.det(A)
detB = np.linalg.det(B)
detA, detB



```



---

## 7) Determinant

For $2\times2$, 

$\det\begin{bmatrix}a&b\\ c&d\end{bmatrix}=ad-bc$.

```python
detA = np.linalg.det(A)
detB = np.linalg.det(B)
detA, detB
```


## 8) Solve Linear Systems $Ax=b$

```python
A = np.array([[3., 2., -1.],
              [2., -2., 4.],
              [-1., 0.5, -1.]])
b = np.array([1., -2., 0.])

x = np.linalg.solve(A, b)     # preferred over inv(A)@b
x, np.allclose(A @ x, b)
```

---

## 9) Eigenvalues & Eigenvectors

Solve $A\mathbf{v}=\lambda \mathbf{v}$ (square $A$).

```python
A = np.array([[4., 2.],
              [1., 3.]])
eigvals, eigvecs = np.linalg.eig(A)
eigvals, eigvecs    # columns of eigvecs are eigenvectors
```

**Verify one pair $(\lambda, v)$**

```python
lam, v = eigvals[0], eigvecs[:,0]
np.allclose(A @ v, lam * v)
```

---

## 10) Norms (size/length measures)

```python
x = np.array([3., -4., 12.])
vec_L2  = np.linalg.norm(x, ord=2)   # Euclidean
vec_L1  = np.linalg.norm(x, ord=1)
vec_Linf= np.linalg.norm(x, ord=np.inf)

M = np.array([[1., 2.],
              [3., 4.]])
fro = np.linalg.norm(M, 'fro')       # Frobenius (matrix)
vec_L2, vec_L1, vec_Linf, fro
```

---

## 11) Orthogonality & Projections (quick demo)

```python
u = np.array([1., 2., -2.])
v = np.array([2., -1., 0.])

dot = u @ v
is_orthogonal = np.isclose(dot, 0.0)

# projection of u onto v
proj_u_on_v = (u @ v) / (v @ v) * v
dot, is_orthogonal, proj_u_on_v
```

---

## 12) Example Mini-Lab

1. Compute $2A - B$ for
    

A=[1324],B=[5678].A=\begin{bmatrix}1&3\\2&4\end{bmatrix},\quad B=\begin{bmatrix}5&6\\7&8\end{bmatrix}.

```python
A = np.array([[1,3],[2,4]])
B = np.array([[5,6],[7,8]])
result = 2*A - B
result
```

2. Verify $A(A^{-1})\approx I$ and report the determinant of $A$.
    

```python
A_inv = np.linalg.inv(A)
check = np.allclose(A @ A_inv, np.eye(2))
detA  = np.linalg.det(A)
check, detA
```

3. Solve the system $Ax=b$ with
    

A=[4123],b=[10].A=\begin{bmatrix}4&1\\2&3\end{bmatrix},\quad b=\begin{bmatrix}1\\0\end{bmatrix}.

```python
A = np.array([[4.,1.],
              [2.,3.]])
b = np.array([1.,0.])
x = np.linalg.solve(A,b)
x, A @ x
```

---

### Tips for Students

- Prefer `np.linalg.solve(A,b)` over `np.linalg.inv(A) @ b` for **speed and numerical stability**.
- Watch out for **singular** or **ill-conditioned** matrices (determinant near 0).  
- Use `np.allclose` when comparing float results.
---



### Problem 1
Re-write the following systems of equation in matrix form to normal algebraic equations.

$$
  \left[ {\begin{array}{cc}
    3 & 5 & 7\\
    2 & 1 & 3\\
     2 & 3 & 6\\
  \end{array} } \right]
  \left[ {\begin{array}{cc}
    x_{1}\\
    x_{2}\\
    x_{3}\\
  \end{array} } \right]
=
  \left[ {\begin{array}{cc}
    24\\
    52\\
    12\\
  \end{array} } \right]
$$

## Problem 1
```python
import numpy as np

# Coefficient matrix A
A = np.array([[2, 3], [4, 5]])
# Right - hand side vector b
b = np.array([4, 6])

# Solve the system of equations
x = np.linalg.solve(A, b)
print(x)
```
## Problem 2





# Systems of Linear Equations

## Techniques to solve Systems of Equations
In this section our goals is to learn some algorithms that help solve system of equations in python.


Matrix Determinate

Cramer's Rule - 

Now let's try to solve a problem think about how we solve a system of two linear equations by hand. 
[example problem]

You've probably learned to use one of the following methods - substitution, graphical or elimination. Is there a way to think about this algorithmically.

Step 1 - Subtract the two
$$
\begin{cases}
a_{11} x_1 + a_{12} x_2 = b_1 \\
a_{21} x_1 + a_{22} x_2 = b_2
\end{cases}
$$
$$
a_{11} x_1 + a_{12} x_2 + a_{13} x_3 = b_1 \\
a_{21} x_1 + a_{22} x_2 + a_{23} x_3 = b_2
$$


<img 
    style="display: block; 
           margin-left: auto;
           margin-right: auto;
           width: 60%;"
    src="fw_elim_bw_sub.png" 
    alt="Forward Elimination then Backwards Substitution">


### Forward Elimination

### Back Substitution

### Naive Gauss Elimination
```python
def gaussian_elimination_naive(A, b):
    A = A.astype(float).copy()
    b = b.astype(float).copy()
    n = len(b)
    # Forward elimination
    for k in range(n-1):
        if np.isclose(A[k,k], 0.0):
            raise ZeroDivisionError("Zero pivot encountered (naïve). Use pivoting.")
        for i in range(k+1, n):
            m = A[i,k]/A[k,k]
            A[i, k:] -= m * A[k, k:]
            b[i]     -= m * b[k]
    # Back substitution
    x = np.zeros(n)
    for i in range(n-1, -1, -1):
        s = A[i, i+1:] @ x[i+1:]
        x[i] = (b[i]-s)/A[i,i]
    return x
```
### Gauss Elimination
```python
def gaussian_elimination_pp(A, b):
    A = A.astype(float).copy()
    b = b.astype(float).copy()
    n = len(b)
    # Forward elimination with partial pivoting
    for k in range(n-1):
        piv = k + np.argmax(np.abs(A[k:, k]))
        if np.isclose(A[piv, k], 0.0):
            raise np.linalg.LinAlgError("Matrix is singular or nearly singular.")
        if piv != k:  # row swap
            A[[k, piv]] = A[[piv, k]]
            b[[k, piv]] = b[[piv, k]]
        for i in range(k+1, n):
            m = A[i,k]/A[k,k]
            A[i, k:] -= m * A[k, k:]
            b[i]     -= m * b[k]
    # Back substitution
    x = np.zeros(n)
    for i in range(n-1, -1, -1):
        s = A[i, i+1:] @ x[i+1:]
        x[i] = (b[i]-s)/A[i,i]
    return x
```

## Problem 1

### Problem 2



# LU Decomposition


## Problem 1

## Problem 2
Modeling of dynamic systems