11---
2- jupyter :
3- jupytext :
4- text_representation :
5- extension : .md
6- format_name : markdown
7- format_version : ' 1.3'
8- jupytext_version : 1.14.4
9- kernelspec :
10- display_name : Python 3 (ipykernel)
11- language : python
12- name : python3
2+ jupytext :
3+ text_representation :
4+ extension : .md
5+ format_name : myst
6+ format_version : 0.13
7+ jupytext_version : 1.14.4
8+ kernelspec :
9+ display_name : Python 3 (ipykernel)
10+ language : python
11+ name : python3
1312---
1413
1514# Linear Equations and Matrix Algebra
@@ -42,7 +41,8 @@ from matplotlib import cm
4241from mpl_toolkits.mplot3d import Axes3D
4342```
4443
45- <!-- #region -->
44+ +++
45+
4646## A Two Good Example
4747
4848We discuss a simple two good example and solve it by
@@ -127,7 +127,8 @@ For example, $\mathbb R ^2$ is the plane and a vector in $\mathbb R^2$ is just a
127127Traditionally, vectors are represented visually as arrows from the origin to the point.
128128
129129The following figure represents three vectors in this manner.
130- <!-- #endregion -->
130+
131+ +++
131132
132133``` {code-cell} ipython3
133134fig, ax = plt.subplots(figsize=(10, 8))
@@ -377,7 +378,8 @@ np.sqrt(np.sum(x**2)) # Norm of x, take one
377378np.linalg.norm(x) # Norm of x, take two
378379```
379380
380- <!-- #region -->
381+ +++
382+
381383## Matrix Operations
382384
383385``` {index} single: Matrix; Operations
@@ -587,7 +589,8 @@ It is a useful exercise to check the following:
587589NumPy arrays are also used as matrices, and have fast, efficient functions and methods for all the standard matrix operations.
588590
589591You can create them manually from tuples of tuples (or lists of lists) as follows
590- <!-- #endregion -->
592+
593+ +++
591594
592595``` {code-cell} ipython3
593596A = ((1, 2),
@@ -627,7 +630,8 @@ B = np.ones((3, 3)) # 3 x 3 matrix of ones
627630A + B
628631```
629632
630- <!-- #region -->
633+ +++
634+
631635To multiply matrices we use the ` @ ` symbol.
632636
633637
850854It can be verified manually that this system has no possible solution.
851855
852856To illustrate why this situation arises let's plot the two lines.
853- <!-- #endregion -->
857+
858+ +++
854859
855860``` {code-cell} ipython3
856861fig, ax = plt.subplots(figsize=(5, 4))
@@ -861,7 +866,8 @@ plt.legend()
861866plt.show()
862867```
863868
864- <!-- #region -->
869+ +++
870+
865871Clearly, these are parallel lines and hence we will never find a point $x \in \mathbb{R}^2$
866872such that these lines intersect.
867873
@@ -987,7 +993,8 @@ This equation is analogous to {eq}`la_se_inv` with $A = (C-D)^{-1}$, $b = h$, an
987993We can now solve for equilibrium prices with NumPy's ` linalg ` submodule.
988994
989995All of these routines are Python front ends to time-tested and highly optimized FORTRAN code.
990- <!-- #endregion -->
996+
997+ +++
991998
992999``` {code-cell} ipython3
9931000C = ((10, 5), #matrix C
@@ -1047,7 +1054,8 @@ q = C @ p # equilibrium quantities
10471054q
10481055```
10491056
1050- <!-- #region -->
1057+ +++
1058+
10511059Observe how we can solve for $x = A^{-1} y$ by either via ` inv(A) @ y ` , or using ` solve(A, y) ` .
10521060
10531061The latter method uses a different algorithm that is numerically more stable and hence should be the default option.
@@ -1108,7 +1116,86 @@ Equilibrium holds when supply equals demand, i.e, $q_0^d = q_0^s$, $q_1^d = q_1^
11081116
11091117``` {exercise-end}
11101118```
1119+ ``` {solution-start} lin_eqs_ex1
1120+ :class: dropdown
1121+ ```
1122+
1123+ The generated system would be:
1124+
1125+ $$
1126+ \begin{aligned}
1127+ 35p_0 - 5p_1 - 5p_2 = 100 \\
1128+ -5p_0 + 25p_1 - 10p_2 = 75 \\
1129+ -5p_0 - 5p_1 + 15p_2 = 55
1130+ \end{aligned}
1131+ $$
11111132
1133+ In matrix form we will write this as:
1134+
1135+ $$
1136+ Ap = b
1137+ \quad \text{where} \quad
1138+ A =
1139+ \begin{bmatrix}
1140+ 35 & -5 & -5 \\
1141+ -5 & 25 & -10 \\
1142+ -5 & -5 & 15
1143+ \end{bmatrix}
1144+ , \quad p =
1145+ \begin{bmatrix}
1146+ p_0 \\
1147+ p_1 \\
1148+ p_2
1149+ \end{bmatrix}
1150+ \quad \text{and} \quad
1151+ b =
1152+ \begin{bmatrix}
1153+ 100 \\
1154+ 75 \\
1155+ 55
1156+ \end{bmatrix}
1157+ $$
1158+
1159+ +++
1160+
1161+ ``` {code-cell} ipython3
1162+ import numpy as np
1163+ from numpy.linalg import det
1164+
1165+ A = np.array([[35, -5, -5], #matrix A
1166+ [-5, 25, -10],
1167+ [-5, -5, 15]])
1168+
1169+ b = np.array((100, 75, 55)) #column vector b
1170+ b.shape = (3,1)
1171+
1172+ det(A) #check if A is nonsingular
1173+ ```
1174+
1175+ ``` {code-cell} ipython3
1176+ #using inverse
1177+ from numpy.linalg import det
1178+
1179+ A_inv = inv(A)
1180+
1181+ p = A_inv @ b
1182+ p
1183+ ```
1184+
1185+ ``` {code-cell} ipython3
1186+ #using numpy.linalg.solve
1187+ from numpy.linalg import solve
1188+ p = solve(A,b)
1189+ p
1190+ ```
1191+
1192+ The solution is given by:
1193+ $$
1194+ p_0 = 4.6925, \; p_1 = 7.0625 \;\; \text{and} \;\; p_2 = 7.675
1195+ $$
1196+
1197+ ``` {solution-end}
1198+ ```
11121199
11131200``` {exercise-start}
11141201:label: lin_eqs_ex2
@@ -1192,90 +1279,6 @@ We will thus try to find the best approximate solution for $x$.
11921279``` {exercise-end}
11931280```
11941281
1195-
1196-
1197- ## Solutions
1198-
1199- ``` {solution-start} lin_eqs_ex1
1200- :class: dropdown
1201- ```
1202-
1203- The generated system would be:
1204-
1205- $$
1206- \begin{aligned}
1207- 35p_0 - 5p_1 - 5p_2 = 100 \\
1208- -5p_0 + 25p_1 - 10p_2 = 75 \\
1209- -5p_0 - 5p_1 + 15p_2 = 55
1210- \end{aligned}
1211- $$
1212-
1213- In matrix form we will write this as:
1214-
1215- $$
1216- Ap = b
1217- \quad \text{where} \quad
1218- A =
1219- \begin{bmatrix}
1220- 35 & -5 & -5 \\
1221- -5 & 25 & -10 \\
1222- -5 & -5 & 15
1223- \end{bmatrix}
1224- , \quad p =
1225- \begin{bmatrix}
1226- p_0 \\
1227- p_1 \\
1228- p_2
1229- \end{bmatrix}
1230- \quad \text{and} \quad
1231- b =
1232- \begin{bmatrix}
1233- 100 \\
1234- 75 \\
1235- 55
1236- \end{bmatrix}
1237- $$
1238- <!-- #endregion -->
1239-
1240- ``` {code-cell} ipython3
1241- import numpy as np
1242- from numpy.linalg import det
1243-
1244- A = np.array([[35, -5, -5], #matrix A
1245- [-5, 25, -10],
1246- [-5, -5, 15]])
1247-
1248- b = np.array((100, 75, 55)) #column vector b
1249- b.shape = (3,1)
1250-
1251- det(A) #check if A is nonsingular
1252- ```
1253-
1254- ``` {code-cell} ipython3
1255- #using inverse
1256- from numpy.linalg import det
1257-
1258- A_inv = inv(A)
1259-
1260- p = A_inv @ b
1261- p
1262- ```
1263-
1264- ``` {code-cell} ipython3
1265- #using numpy.linalg.solve
1266- from numpy.linalg import solve
1267- p = solve(A,b)
1268- p
1269- ```
1270-
1271- The solution is given by:
1272- $$
1273- p_0 = 4.6925, \; p_1 = 7.0625 \;\; \text{and} \;\; p_2 = 7.675
1274- $$
1275-
1276- ``` {solution-end}
1277- ```
1278-
12791282``` {solution-start} lin_eqs_ex2
12801283:class: dropdown
12811284```
@@ -1326,11 +1329,4 @@ plt.show()
13261329```
13271330
13281331``` {solution-end}
1329- ```
1330-
1331- <!-- #region tags=[] -->
1332- ``` {solution-end}
1333- ```
1334-
1335- solution end
1336- <!-- #endregion -->
1332+ ```
0 commit comments