From b7652c078a74ec0fd8419c4e0d8f9dc1d7b28020 Mon Sep 17 00:00:00 2001 From: Christian Kolset Date: Wed, 1 Oct 2025 14:04:14 -0600 Subject: Re-structure of Module 3 to make each file a seperate lecture --- tutorials/module_3/1_1_Linear_Algebra.md | 155 +++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 tutorials/module_3/1_1_Linear_Algebra.md (limited to 'tutorials/module_3/1_1_Linear_Algebra.md') diff --git a/tutorials/module_3/1_1_Linear_Algebra.md b/tutorials/module_3/1_1_Linear_Algebra.md new file mode 100644 index 0000000..a107c1f --- /dev/null +++ b/tutorials/module_3/1_1_Linear_Algebra.md @@ -0,0 +1,155 @@ +# 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. + +![](https://www.mathsisfun.com/algebra/images/matrix-multiply-a.svg) + +![](https://www.mathsisfun.com/algebra/images/matrix-multiply-b.svg) + +## Matrix operations in python + +- Matrices of the same size can be added or subtracted element-wise: +- Multiply a matrix by a scalar yields a new matrix where every element is multiplied by the scalar. +- The determinant of a matrix is number of a square matrix that summarizes certain properties of the matrix. +- **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 Math in Python +Let's create two square matrices $A$ and $B$ to perform some matrix operations on: +```python +import numpy as np +np.set_printoptions(suppress=True) + +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 +``` + +Test and see how it works. + +```python +# 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 +``` + +### 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 2 +```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 3 + + + +Matrix Determinate + +Cramer's Rule - + -- cgit v1.2.3