├── Ch_01
└── Ch_1_1.ipynb
├── Ch_02
├── Ch_2_1.ipynb
├── Ch_2_2.ipynb
├── Ch_2_3.ipynb
├── Ch_2_5.ipynb
├── Ch_2_6.ipynb
└── Ch_2_7.ipynb
├── Ch_03
├── Ch_3_1.ipynb
├── Ch_3_2.ipynb
├── Ch_3_3.ipynb
├── Ch_3_4.ipynb
└── Ch_3_5.ipynb
├── Ch_04
├── Ch_4_1.ipynb
├── Ch_4_2.ipynb
├── Ch_4_3.ipynb
├── Ch_4_4.ipynb
├── Ch_4_5.ipynb
├── scrippsm.txt
├── scrippsy.txt
└── windmill.txt
├── Ch_05
├── Ch_5_1.ipynb
├── Ch_5_2.ipynb
├── Ch_5_3.ipynb
└── Ch_5_4.ipynb
├── Ch_06
├── .ipynb_checkpoints
│ └── Ch_6_5-checkpoint.ipynb
├── Ch_6_1.ipynb
├── Ch_6_2.ipynb
├── Ch_6_3.ipynb
├── Ch_6_4.ipynb
├── Ch_6_5.ipynb
├── Lorenz_equation.py
├── Pendulum.py
└── Three_body_problem.py
├── Ch_07
├── .ipynb_checkpoints
│ └── Ch_7_1-checkpoint.ipynb
└── Ch_7_1.ipynb
├── Ch_09
├── Ch_9_1.ipynb
├── Ch_9_2.ipynb
└── Ch_9_3.ipynb
├── README.md
└── Sauer - Numerical Analysis.pdf
/Ch_02/Ch_2_1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Chapter 2
Systems of Equations"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## 2.1 Gaussian Elimination"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "### Q. 1"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 2,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "def Gaussian_elimination(equation):\n",
40 | " A = equation.copy()\n",
41 | " m, n = A.shape\n",
42 | "\n",
43 | " for i in range(m):\n",
44 | " A[i] = A[i] / A[i, i]\n",
45 | " for j in range(i+1, m):\n",
46 | " A[j] -= A[j, i]*A[i]\n",
47 | "\n",
48 | " for i in range(m-1, 0, -1):\n",
49 | " for j in range(i):\n",
50 | " A[j] -= A[j, i]*A[i]\n",
51 | " \n",
52 | " return A"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 3,
58 | "metadata": {},
59 | "outputs": [
60 | {
61 | "name": "stdout",
62 | "output_type": "stream",
63 | "text": [
64 | "x, y, z = [1. 1. 2.]\n"
65 | ]
66 | }
67 | ],
68 | "source": [
69 | "# (a)\n",
70 | "A = np.array([[2, -2, -1, -2], [4, 1, -2, 1], [-2, 1, -1, -3]], dtype=float)\n",
71 | "\n",
72 | "print(\"x, y, z =\", Gaussian_elimination(A)[:, -1])"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": 4,
78 | "metadata": {},
79 | "outputs": [
80 | {
81 | "name": "stdout",
82 | "output_type": "stream",
83 | "text": [
84 | "x, y, z = [1. 1. 1.]\n"
85 | ]
86 | }
87 | ],
88 | "source": [
89 | "# (b)\n",
90 | "A = np.array([[1, 2, -1, 2], [0, 3, 1, 4], [2, -1, 1, 2]], dtype=float)\n",
91 | "\n",
92 | "print(\"x, y, z =\", Gaussian_elimination(A)[:, -1])"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 5,
98 | "metadata": {},
99 | "outputs": [
100 | {
101 | "name": "stdout",
102 | "output_type": "stream",
103 | "text": [
104 | "x, y, z = [-1. 3. 2.]\n"
105 | ]
106 | }
107 | ],
108 | "source": [
109 | "# (c)\n",
110 | "A = np.array([[2, 1, -4, -7], [1, -1, 1, -2], [-1, 3, -2, 6]], dtype=float)\n",
111 | "\n",
112 | "print(\"x, y, z =\", Gaussian_elimination(A)[:, -1])"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "### Q. 2"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 6,
125 | "metadata": {},
126 | "outputs": [],
127 | "source": [
128 | "def Hilbert(n):\n",
129 | " H = np.ones((n, n))\n",
130 | " for i in range(n):\n",
131 | " for j in range(n):\n",
132 | " H[i, j] /= (i+j+1)\n",
133 | " \n",
134 | " return H"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 7,
140 | "metadata": {},
141 | "outputs": [
142 | {
143 | "name": "stdout",
144 | "output_type": "stream",
145 | "text": [
146 | "x = [-2. 6.]\n",
147 | "Ax = [1. 1.]\n"
148 | ]
149 | }
150 | ],
151 | "source": [
152 | "# (a)\n",
153 | "\n",
154 | "n = 2\n",
155 | "A = np.concatenate((Hilbert(n), np.ones((n, 1))), axis=1)\n",
156 | "x = Gaussian_elimination(A)[:, -1]\n",
157 | "\n",
158 | "print(\"x =\", x)\n",
159 | "print(\"Ax =\", np.dot(Hilbert(n), x))"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 8,
165 | "metadata": {},
166 | "outputs": [
167 | {
168 | "name": "stdout",
169 | "output_type": "stream",
170 | "text": [
171 | "x = [ 5. -120. 630. -1120. 630.]\n",
172 | "Ax = [1. 1. 1. 1. 1.]\n"
173 | ]
174 | }
175 | ],
176 | "source": [
177 | "# (b)\n",
178 | "\n",
179 | "n = 5\n",
180 | "A = np.concatenate((Hilbert(n), np.ones((n, 1))), axis=1)\n",
181 | "x = Gaussian_elimination(A)[:, -1]\n",
182 | "\n",
183 | "print(\"x =\", x)\n",
184 | "print(\"Ax =\", np.dot(Hilbert(n), x))"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 9,
190 | "metadata": {},
191 | "outputs": [
192 | {
193 | "name": "stdout",
194 | "output_type": "stream",
195 | "text": [
196 | "x = [-9.99737067e+00 9.89772237e+02 -2.37551401e+04 2.40195762e+05\n",
197 | " -1.26104879e+06 3.78319896e+06 -6.72576616e+06 7.00035782e+06\n",
198 | " -3.93773570e+06 9.23673464e+05]\n",
199 | "Ax = [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n"
200 | ]
201 | }
202 | ],
203 | "source": [
204 | "# (c)\n",
205 | "\n",
206 | "n = 10\n",
207 | "A = np.concatenate((Hilbert(n), np.ones((n, 1))), axis=1)\n",
208 | "x = Gaussian_elimination(A)[:, -1]\n",
209 | "\n",
210 | "print(\"x =\", x)\n",
211 | "print(\"Ax =\", np.dot(Hilbert(n), x))"
212 | ]
213 | }
214 | ],
215 | "metadata": {
216 | "kernelspec": {
217 | "display_name": "Python 3",
218 | "language": "python",
219 | "name": "python3"
220 | },
221 | "language_info": {
222 | "codemirror_mode": {
223 | "name": "ipython",
224 | "version": 3
225 | },
226 | "file_extension": ".py",
227 | "mimetype": "text/x-python",
228 | "name": "python",
229 | "nbconvert_exporter": "python",
230 | "pygments_lexer": "ipython3",
231 | "version": "3.6.4"
232 | }
233 | },
234 | "nbformat": 4,
235 | "nbformat_minor": 2
236 | }
237 |
--------------------------------------------------------------------------------
/Ch_02/Ch_2_2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Chapter 2
Systems of Equations"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## 2.2 The LU Factorization"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "### Q. 1"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 4,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "def LU(A):\n",
40 | " m = A.shape[0]\n",
41 | " L = np.identity(m)\n",
42 | " U = A.copy()\n",
43 | "\n",
44 | " for i in range(m):\n",
45 | " for j in range(i+1, m):\n",
46 | " if U[i, i] == 0:\n",
47 | " raise ValueError(\"The matrix contains a zero pivot.\")\n",
48 | " a = U[j, i] / U[i, i]\n",
49 | " L[j, i] = a\n",
50 | " U[j] -= a * U[i]\n",
51 | " \n",
52 | " return L, U"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 5,
58 | "metadata": {},
59 | "outputs": [
60 | {
61 | "name": "stdout",
62 | "output_type": "stream",
63 | "text": [
64 | "L:\n",
65 | " [[1. 0. 0.]\n",
66 | " [2. 1. 0.]\n",
67 | " [1. 0. 1.]]\n",
68 | "U:\n",
69 | " [[3. 1. 2.]\n",
70 | " [0. 1. 0.]\n",
71 | " [0. 0. 3.]]\n",
72 | "LU =\n",
73 | " [[3. 1. 2.]\n",
74 | " [6. 3. 4.]\n",
75 | " [3. 1. 5.]]\n",
76 | "A == LU: True\n"
77 | ]
78 | }
79 | ],
80 | "source": [
81 | "# (a)\n",
82 | "A = np.array([[3, 1, 2], [6, 3, 4], [3, 1, 5]], dtype=float)\n",
83 | "L, U = LU(A)\n",
84 | "\n",
85 | "print(\"L:\\n\", L)\n",
86 | "print(\"U:\\n\", U)\n",
87 | "print(\"LU =\\n\", np.dot(L, U))\n",
88 | "print(\"A == LU:\", (np.dot(L, U) == A).all())"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 6,
94 | "metadata": {},
95 | "outputs": [
96 | {
97 | "name": "stdout",
98 | "output_type": "stream",
99 | "text": [
100 | "L:\n",
101 | " [[1. 0. 0. ]\n",
102 | " [1. 1. 0. ]\n",
103 | " [0.5 0.5 1. ]]\n",
104 | "U:\n",
105 | " [[4. 2. 0.]\n",
106 | " [0. 2. 2.]\n",
107 | " [0. 0. 2.]]\n",
108 | "LU =\n",
109 | " [[4. 2. 0.]\n",
110 | " [4. 4. 2.]\n",
111 | " [2. 2. 3.]]\n",
112 | "A == LU: True\n"
113 | ]
114 | }
115 | ],
116 | "source": [
117 | "# (b)\n",
118 | "A = np.array([[4, 2, 0], [4, 4, 2], [2, 2, 3]], dtype=float)\n",
119 | "L, U = LU(A)\n",
120 | "\n",
121 | "print(\"L:\\n\", L)\n",
122 | "print(\"U:\\n\", U)\n",
123 | "print(\"LU =\\n\", np.dot(L, U))\n",
124 | "print(\"A == LU:\", (np.dot(L, U) == A).all())"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": 7,
130 | "metadata": {},
131 | "outputs": [
132 | {
133 | "name": "stdout",
134 | "output_type": "stream",
135 | "text": [
136 | "L:\n",
137 | " [[1. 0. 0. 0.]\n",
138 | " [0. 1. 0. 0.]\n",
139 | " [1. 2. 1. 0.]\n",
140 | " [0. 1. 0. 1.]]\n",
141 | "U:\n",
142 | " [[ 1. -1. 1. 2.]\n",
143 | " [ 0. 2. 1. 0.]\n",
144 | " [ 0. 0. 1. 2.]\n",
145 | " [ 0. 0. 0. -1.]]\n",
146 | "LU =\n",
147 | " [[ 1. -1. 1. 2.]\n",
148 | " [ 0. 2. 1. 0.]\n",
149 | " [ 1. 3. 4. 4.]\n",
150 | " [ 0. 2. 1. -1.]]\n",
151 | "A == LU: True\n"
152 | ]
153 | }
154 | ],
155 | "source": [
156 | "# (c)\n",
157 | "A = np.array([[1, -1, 1, 2], [0, 2, 1, 0], [1, 3, 4, 4], [0, 2, 1, -1]], dtype=float)\n",
158 | "L, U = LU(A)\n",
159 | "\n",
160 | "print(\"L:\\n\", L)\n",
161 | "print(\"U:\\n\", U)\n",
162 | "print(\"LU =\\n\", np.dot(L, U))\n",
163 | "print(\"A == LU:\", (np.dot(L, U) == A).all())"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "### Q. 2"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {},
177 | "outputs": [],
178 | "source": [
179 | "def solve(A, b):\n",
180 | " m = A.shape[0]\n",
181 | " L, U = LU(A)\n",
182 | " \n",
183 | " c = b"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": 13,
189 | "metadata": {},
190 | "outputs": [],
191 | "source": [
192 | "# (a)\n",
193 | "A = np.array([[3, 1, 2], [6, 3, 4], [3, 1, 5]], dtype=float)\n",
194 | "b = np.array([0, 1, 3])\n",
195 | "m = A.shape[0]\n",
196 | "\n",
197 | "L, U = LU(A)"
198 | ]
199 | },
200 | {
201 | "cell_type": "code",
202 | "execution_count": 14,
203 | "metadata": {},
204 | "outputs": [
205 | {
206 | "name": "stdout",
207 | "output_type": "stream",
208 | "text": [
209 | "x: [-1 1 1]\n",
210 | "Ax = [0. 1. 3.]\n"
211 | ]
212 | }
213 | ],
214 | "source": [
215 | "c = b.copy()\n",
216 | "for i in range(m):\n",
217 | " for j in range(i+1, m):\n",
218 | " c[j] -= L[j, i]*c[i]\n",
219 | "\n",
220 | "x = c.copy()\n",
221 | "for i in range(m-1, -1, -1):\n",
222 | " x[i] /= U[i, i]\n",
223 | " for j in range(i):\n",
224 | " x[j] -= U[j, i]*x[i]\n",
225 | "\n",
226 | "print(\"x:\", x)\n",
227 | "print(\"Ax = \", np.dot(A, x))"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 15,
233 | "metadata": {},
234 | "outputs": [],
235 | "source": [
236 | "# (b)\n",
237 | "A = np.array([[4, 2, 0], [4, 4, 2], [2, 2, 3]], dtype=float)\n",
238 | "b = np.array([2, 4, 6])\n",
239 | "m = A.shape[0]\n",
240 | "\n",
241 | "L, U = LU(A)"
242 | ]
243 | },
244 | {
245 | "cell_type": "code",
246 | "execution_count": 16,
247 | "metadata": {},
248 | "outputs": [
249 | {
250 | "name": "stdout",
251 | "output_type": "stream",
252 | "text": [
253 | "x: [ 1 -1 2]\n",
254 | "Ax = [2. 4. 6.]\n"
255 | ]
256 | }
257 | ],
258 | "source": [
259 | "c = b.copy()\n",
260 | "for i in range(m):\n",
261 | " for j in range(i+1, m):\n",
262 | " c[j] -= L[j, i]*c[i]\n",
263 | "\n",
264 | "x = c.copy()\n",
265 | "for i in range(m-1, -1, -1):\n",
266 | " x[i] /= U[i, i]\n",
267 | " for j in range(i):\n",
268 | " x[j] -= U[j, i]*x[i]\n",
269 | "\n",
270 | "print(\"x:\", x)\n",
271 | "print(\"Ax = \", np.dot(A, x))"
272 | ]
273 | }
274 | ],
275 | "metadata": {
276 | "kernelspec": {
277 | "display_name": "Python 3",
278 | "language": "python",
279 | "name": "python3"
280 | },
281 | "language_info": {
282 | "codemirror_mode": {
283 | "name": "ipython",
284 | "version": 3
285 | },
286 | "file_extension": ".py",
287 | "mimetype": "text/x-python",
288 | "name": "python",
289 | "nbconvert_exporter": "python",
290 | "pygments_lexer": "ipython3",
291 | "version": "3.6.4"
292 | }
293 | },
294 | "nbformat": 4,
295 | "nbformat_minor": 2
296 | }
297 |
--------------------------------------------------------------------------------
/Ch_02/Ch_2_3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Chapter 2
Systems of Equations"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## 2.3 Sources of Error"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 2,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "def cond(A):\n",
33 | " return abs(A).sum(axis=1).max() * abs(np.linalg.inv(A)).sum(axis=1).max()"
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "### Q. 1"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 3,
46 | "metadata": {},
47 | "outputs": [],
48 | "source": [
49 | "def A_ij(n):\n",
50 | " matrix = np.zeros((n, n))\n",
51 | " for i in range(n):\n",
52 | " for j in range(n):\n",
53 | " matrix[i, j] = 5/(i + 2*j + 2)\n",
54 | " return matrix"
55 | ]
56 | },
57 | {
58 | "cell_type": "code",
59 | "execution_count": 4,
60 | "metadata": {},
61 | "outputs": [],
62 | "source": [
63 | "# (a)\n",
64 | "n = 6\n",
65 | "\n",
66 | "A = A_ij(n)\n",
67 | "x = np.ones(n)\n",
68 | "b = np.dot(A, x)\n",
69 | "\n",
70 | "x_c = np.linalg.inv(A).dot(b)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 5,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "forward_error = abs(x_c - x).max()\n",
80 | "backward_error = abs(A.dot(x_c) - b).max()\n",
81 | "error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 6,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "name": "stdout",
91 | "output_type": "stream",
92 | "text": [
93 | "Forward error: 0.000000\n",
94 | "Error magnification factor: 39.357182\n",
95 | "Condition number of A: 70342013.949083\n"
96 | ]
97 | }
98 | ],
99 | "source": [
100 | "print(\"Forward error: %f\" % forward_error)\n",
101 | "print(\"Error magnification factor: %f\" % error_mag_factor)\n",
102 | "print(\"Condition number of A: %f\" % cond(A))"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 7,
108 | "metadata": {},
109 | "outputs": [],
110 | "source": [
111 | "# (b)\n",
112 | "n = 10\n",
113 | "\n",
114 | "A = A_ij(n)\n",
115 | "x = np.ones(n)\n",
116 | "b = np.dot(A, x)\n",
117 | "\n",
118 | "x_c = np.linalg.inv(A).dot(b)"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": 8,
124 | "metadata": {},
125 | "outputs": [],
126 | "source": [
127 | "forward_error = abs(x_c - x).max()\n",
128 | "backward_error = abs(A.dot(x_c) - b).max()\n",
129 | "error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 9,
135 | "metadata": {
136 | "scrolled": false
137 | },
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | "Forward error: 0.015717\n",
144 | "Error magnification factor: 228.970404\n",
145 | "Condition number of A: 131370135425813.031250\n"
146 | ]
147 | }
148 | ],
149 | "source": [
150 | "print(\"Forward error: %f\" % forward_error)\n",
151 | "print(\"Error magnification factor: %f\" % error_mag_factor)\n",
152 | "print(\"Condition number of A: %f\" % cond(A))"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "### Q. 2"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 10,
165 | "metadata": {},
166 | "outputs": [],
167 | "source": [
168 | "def A_ij(n):\n",
169 | " matrix = np.zeros((n, n))\n",
170 | " for i in range(n):\n",
171 | " for j in range(n):\n",
172 | " matrix[i, j] = 1/(abs(i-j) + 1)\n",
173 | " return matrix"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": 11,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": [
182 | "# (a)\n",
183 | "n = 6\n",
184 | "\n",
185 | "A = A_ij(n)\n",
186 | "x = np.ones(n)\n",
187 | "b = np.dot(A, x)\n",
188 | "\n",
189 | "x_c = np.linalg.inv(A).dot(b)"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": 12,
195 | "metadata": {},
196 | "outputs": [],
197 | "source": [
198 | "forward_error = abs(x_c - x).max()\n",
199 | "backward_error = abs(A.dot(x_c) - b).max()\n",
200 | "error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 13,
206 | "metadata": {},
207 | "outputs": [
208 | {
209 | "name": "stdout",
210 | "output_type": "stream",
211 | "text": [
212 | "Forward error: 0.000000\n",
213 | "Error magnification factor: 2.916667\n",
214 | "Condition number of A: 8.611677\n"
215 | ]
216 | }
217 | ],
218 | "source": [
219 | "print(\"Forward error: %f\" % forward_error)\n",
220 | "print(\"Error magnification factor: %f\" % error_mag_factor)\n",
221 | "print(\"Condition number of A: %f\" % cond(A))"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": 14,
227 | "metadata": {},
228 | "outputs": [],
229 | "source": [
230 | "# (b)\n",
231 | "n = 10\n",
232 | "\n",
233 | "A = A_ij(n)\n",
234 | "x = np.ones(n)\n",
235 | "b = np.dot(A, x)\n",
236 | "\n",
237 | "x_c = np.linalg.inv(A).dot(b)"
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 15,
243 | "metadata": {},
244 | "outputs": [],
245 | "source": [
246 | "forward_error = abs(x_c - x).max()\n",
247 | "backward_error = abs(A.dot(x_c) - b).max()\n",
248 | "error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 16,
254 | "metadata": {
255 | "scrolled": true
256 | },
257 | "outputs": [
258 | {
259 | "name": "stdout",
260 | "output_type": "stream",
261 | "text": [
262 | "Forward error: 0.000000\n",
263 | "Error magnification factor: 5.133333\n",
264 | "Condition number of A: 11.261167\n"
265 | ]
266 | }
267 | ],
268 | "source": [
269 | "print(\"Forward error: %f\" % forward_error)\n",
270 | "print(\"Error magnification factor: %f\" % error_mag_factor)\n",
271 | "print(\"Condition number of A: %f\" % cond(A))"
272 | ]
273 | },
274 | {
275 | "cell_type": "markdown",
276 | "metadata": {},
277 | "source": [
278 | "### Q. 3"
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": 17,
284 | "metadata": {},
285 | "outputs": [],
286 | "source": [
287 | "def A_ij(n):\n",
288 | " matrix = np.zeros((n, n))\n",
289 | " for i in range(n):\n",
290 | " for j in range(n):\n",
291 | " matrix[i, j] = abs(i-j) + 1\n",
292 | " return matrix"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": 18,
298 | "metadata": {},
299 | "outputs": [
300 | {
301 | "name": "stdout",
302 | "output_type": "stream",
303 | "text": [
304 | "n: 100\n",
305 | "Forward error: 0.000000\n",
306 | "Error magnification factor: 3858.529946\n",
307 | "Condition number of A: 10100.000000\n",
308 | "\n",
309 | "n: 200\n",
310 | "Forward error: 0.000000\n",
311 | "Error magnification factor: 9190.038314\n",
312 | "Condition number of A: 40200.000000\n",
313 | "\n",
314 | "n: 300\n",
315 | "Forward error: 0.000000\n",
316 | "Error magnification factor: 21818.334871\n",
317 | "Condition number of A: 90300.000001\n",
318 | "\n",
319 | "n: 400\n",
320 | "Forward error: 0.000000\n",
321 | "Error magnification factor: 19806.948349\n",
322 | "Condition number of A: 160400.000004\n",
323 | "\n",
324 | "n: 500\n",
325 | "Forward error: 0.000000\n",
326 | "Error magnification factor: 89999.863114\n",
327 | "Condition number of A: 250500.000011\n",
328 | "\n"
329 | ]
330 | }
331 | ],
332 | "source": [
333 | "for n in range(100, 501, 100):\n",
334 | " A = A_ij(n)\n",
335 | " x = np.ones(n)\n",
336 | " b = np.dot(A, x)\n",
337 | "\n",
338 | " x_c = np.linalg.inv(A).dot(b)\n",
339 | "\n",
340 | " forward_error = abs(x_c - x).max()\n",
341 | " backward_error = abs(A.dot(x_c) - b).max()\n",
342 | " error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())\n",
343 | "\n",
344 | " print(\"n: %d\" % n)\n",
345 | " print(\"Forward error: %f\" % forward_error)\n",
346 | " print(\"Error magnification factor: %f\" % error_mag_factor)\n",
347 | " print(\"Condition number of A: %f\\n\" % cond(A))"
348 | ]
349 | },
350 | {
351 | "cell_type": "markdown",
352 | "metadata": {},
353 | "source": [
354 | "\n",
355 | "### Q. 4"
356 | ]
357 | },
358 | {
359 | "cell_type": "code",
360 | "execution_count": 19,
361 | "metadata": {},
362 | "outputs": [],
363 | "source": [
364 | "def A_ij(n):\n",
365 | " matrix = np.zeros((n, n))\n",
366 | " for i in range(n):\n",
367 | " for j in range(n):\n",
368 | " matrix[i, j] = np.sqrt((i-j)**2 + n/10)\n",
369 | " return matrix"
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "execution_count": 20,
375 | "metadata": {
376 | "scrolled": false
377 | },
378 | "outputs": [
379 | {
380 | "name": "stdout",
381 | "output_type": "stream",
382 | "text": [
383 | "n: 100\n",
384 | "Forward error: 0.000035\n",
385 | "Error magnification factor: 69559.619727\n",
386 | "Condition number of A: 61843077.585326\n",
387 | "\n",
388 | "n: 200\n",
389 | "Forward error: 0.634976\n",
390 | "Error magnification factor: 8594052.687108\n",
391 | "Condition number of A: 12906622013.245913\n",
392 | "\n",
393 | "n: 300\n",
394 | "Forward error: 965.765747\n",
395 | "Error magnification factor: 875228502.173950\n",
396 | "Condition number of A: 620392433505.416260\n",
397 | "\n",
398 | "n: 400\n",
399 | "Forward error: 163856.897461\n",
400 | "Error magnification factor: 2951874692.992279\n",
401 | "Condition number of A: 14753725994212.501953\n",
402 | "\n",
403 | "n: 500\n",
404 | "Forward error: 42967469.812500\n",
405 | "Error magnification factor: 20261133885.881592\n",
406 | "Condition number of A: 228913233562526.000000\n",
407 | "\n"
408 | ]
409 | }
410 | ],
411 | "source": [
412 | "for n in range(100, 501, 100):\n",
413 | " A = A_ij(n)\n",
414 | " x = np.ones(n)\n",
415 | " b = np.dot(A, x)\n",
416 | "\n",
417 | " x_c = np.linalg.inv(A).dot(b)\n",
418 | "\n",
419 | " forward_error = abs(x_c - x).max()\n",
420 | " backward_error = abs(A.dot(x_c) - b).max()\n",
421 | " error_mag_factor = (forward_error/abs(x).max()) / (backward_error/abs(b).max())\n",
422 | "\n",
423 | " print(\"n: %d\" % n)\n",
424 | " print(\"Forward error: %f\" % forward_error)\n",
425 | " print(\"Error magnification factor: %f\" % error_mag_factor)\n",
426 | " print(\"Condition number of A: %f\\n\" % cond(A))"
427 | ]
428 | },
429 | {
430 | "cell_type": "markdown",
431 | "metadata": {},
432 | "source": [
433 | "### Q. 5"
434 | ]
435 | },
436 | {
437 | "cell_type": "code",
438 | "execution_count": 21,
439 | "metadata": {},
440 | "outputs": [],
441 | "source": [
442 | "e_mach = 2.220446049250313e-16"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 22,
448 | "metadata": {},
449 | "outputs": [],
450 | "source": [
451 | "def A_ij(n):\n",
452 | " matrix = np.zeros((n, n))\n",
453 | " for i in range(n):\n",
454 | " for j in range(n):\n",
455 | " matrix[i, j] = 5/(i + 2*j + 2)\n",
456 | " return matrix"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 23,
462 | "metadata": {},
463 | "outputs": [],
464 | "source": [
465 | "n = 1\n",
466 | "while True:\n",
467 | " cond_num = int(np.log10(cond(A_ij(n))))\n",
468 | " if cond_num >= 16:\n",
469 | " break\n",
470 | " n += 1"
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "execution_count": 24,
476 | "metadata": {},
477 | "outputs": [
478 | {
479 | "name": "stdout",
480 | "output_type": "stream",
481 | "text": [
482 | "If n is larger than 12, the solution has no correct significant digits.\n"
483 | ]
484 | }
485 | ],
486 | "source": [
487 | "print(\"If n is larger than %d, the solution has no correct significant digits.\" % n)"
488 | ]
489 | },
490 | {
491 | "cell_type": "markdown",
492 | "metadata": {},
493 | "source": [
494 | "### Q. 6"
495 | ]
496 | },
497 | {
498 | "cell_type": "code",
499 | "execution_count": 25,
500 | "metadata": {},
501 | "outputs": [],
502 | "source": [
503 | "A = np.array([[1e-20, 1], [1, 2]])\n",
504 | "b = np.array([1, 4])\n",
505 | "x = np.array([2e+20/(1e+20 - 2), (4 - 1e+20)/(2 - 1e+20)])"
506 | ]
507 | },
508 | {
509 | "cell_type": "code",
510 | "execution_count": 26,
511 | "metadata": {},
512 | "outputs": [],
513 | "source": [
514 | "# Version 2\n",
515 | "Ab = np.concatenate((A, b.reshape(-1, 1)), 1)\n",
516 | "Ab[1] -= Ab[0] * Ab[1, 0]/Ab[0, 0]\n",
517 | "Ab[1] /= Ab[1, 1]\n",
518 | "Ab[0] -= Ab[0, 1]*Ab[1]\n",
519 | "Ab[0] /= Ab[0, 0]\n",
520 | "\n",
521 | "x_c = Ab[:, 2]"
522 | ]
523 | },
524 | {
525 | "cell_type": "code",
526 | "execution_count": 27,
527 | "metadata": {},
528 | "outputs": [
529 | {
530 | "name": "stdout",
531 | "output_type": "stream",
532 | "text": [
533 | "True x: [2. 1.]\n",
534 | "x_c: [0. 1.]\n",
535 | "Forward error: 2.000000\n"
536 | ]
537 | }
538 | ],
539 | "source": [
540 | "print(\"True x:\", x)\n",
541 | "print(\"x_c:\", x_c)\n",
542 | "print(\"Forward error: %f\" % abs(x - x_c).max())"
543 | ]
544 | },
545 | {
546 | "cell_type": "code",
547 | "execution_count": 28,
548 | "metadata": {},
549 | "outputs": [],
550 | "source": [
551 | "# Version 3\n",
552 | "Ab = np.concatenate((A[::-1], b[::-1].reshape(-1, 1)), 1)\n",
553 | "Ab[1] -= Ab[0] * Ab[1, 0]/Ab[0, 0]\n",
554 | "Ab[1] /= Ab[1, 1]\n",
555 | "Ab[0] -= Ab[0, 1]*Ab[1]\n",
556 | "Ab[0] /= Ab[0, 0]\n",
557 | "\n",
558 | "x_c = Ab[:, 2]"
559 | ]
560 | },
561 | {
562 | "cell_type": "code",
563 | "execution_count": 29,
564 | "metadata": {},
565 | "outputs": [
566 | {
567 | "name": "stdout",
568 | "output_type": "stream",
569 | "text": [
570 | "True x: [2. 1.]\n",
571 | "x_c: [2. 1.]\n",
572 | "Forward error: 0.000000\n"
573 | ]
574 | }
575 | ],
576 | "source": [
577 | "print(\"True x:\", x)\n",
578 | "print(\"x_c:\", x_c)\n",
579 | "print(\"Forward error: %f\" % abs(x - x_c).max())"
580 | ]
581 | }
582 | ],
583 | "metadata": {
584 | "kernelspec": {
585 | "display_name": "Python 3",
586 | "language": "python",
587 | "name": "python3"
588 | },
589 | "language_info": {
590 | "codemirror_mode": {
591 | "name": "ipython",
592 | "version": 3
593 | },
594 | "file_extension": ".py",
595 | "mimetype": "text/x-python",
596 | "name": "python",
597 | "nbconvert_exporter": "python",
598 | "pygments_lexer": "ipython3",
599 | "version": "3.6.4"
600 | }
601 | },
602 | "nbformat": 4,
603 | "nbformat_minor": 2
604 | }
605 |
--------------------------------------------------------------------------------
/Ch_02/Ch_2_5.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np\n",
10 | "from time import time"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {},
16 | "source": [
17 | "# Chapter 2
Systems of Equations"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## 2.5 Iterative Methods"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 2,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "def Jacobi(A, b, iter_n, initial_guess=0):\n",
34 | " n = len(A)\n",
35 | " \n",
36 | " D = np.diag(A)\n",
37 | " R = A - np.diag(D)\n",
38 | " x_i = initial_guess * np.ones(n)\n",
39 | " \n",
40 | " for _ in range(iter_n):\n",
41 | " x_i = (b - R.dot(x_i)) / D\n",
42 | " \n",
43 | " return x_i"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 3,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "def Gauss_Seidel(A, b, iter_n, initial_guess=0):\n",
53 | " n = len(A)\n",
54 | " \n",
55 | " D = np.diag(A)\n",
56 | " L = np.tril(A) - np.diag(D)\n",
57 | " U = np.triu(A) - np.diag(D)\n",
58 | " \n",
59 | " x_i = initial_guess * np.ones(n)\n",
60 | " x_ii = x_i.copy()\n",
61 | " \n",
62 | " for _ in range(iter_n):\n",
63 | " for k in range(n):\n",
64 | " x_ii[k] = (b[k] - U[k].dot(x_i) - L[k].dot(x_ii)) / D[k]\n",
65 | " x_i = x_ii.copy()\n",
66 | " \n",
67 | " return x_i"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": 4,
73 | "metadata": {},
74 | "outputs": [],
75 | "source": [
76 | "def SOR(A, b, w, iter_n, initial_guess=0):\n",
77 | " n = len(A)\n",
78 | " \n",
79 | " D = np.diag(A)\n",
80 | " L = np.tril(A) - np.diag(D)\n",
81 | " U = np.triu(A) - np.diag(D)\n",
82 | " \n",
83 | " x_i = initial_guess * np.ones(n)\n",
84 | " \n",
85 | " for _ in range(iter_n):\n",
86 | " x_i = np.linalg.inv(w*L + np.diag(D)).dot((1 - w)*np.diag(D).dot(x_i) - w*U.dot(x_i)) + \\\n",
87 | " w*np.linalg.inv(np.diag(D) + w*L).dot(b)\n",
88 | " \n",
89 | " return x_i"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "### Q. 1"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 5,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": [
105 | "def A_ij(n):\n",
106 | " A = np.zeros((n, n))\n",
107 | " for i in range(n):\n",
108 | " A[i, i] = 3\n",
109 | " if i < n-1:\n",
110 | " A[i, i+1] = -1\n",
111 | " A[i+1, i] = -1\n",
112 | "\n",
113 | " return A"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 6,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": [
122 | "# n = 100\n",
123 | "n = 100\n",
124 | "\n",
125 | "A = A_ij(n)\n",
126 | "b = np.ones(n)\n",
127 | "b[0], b[-1] = 2, 2\n",
128 | "x_true = np.ones(n)"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 7,
134 | "metadata": {},
135 | "outputs": [],
136 | "source": [
137 | "j = 0\n",
138 | "guess = np.zeros(n)\n",
139 | "\n",
140 | "while True:\n",
141 | " guess = Jacobi(A, b, 1, guess)\n",
142 | " j += 1 \n",
143 | " forward_error = abs(guess - x_true).max()\n",
144 | " if forward_error < 5e-7:\n",
145 | " break\n",
146 | "\n",
147 | "backward_error = abs(A.dot(guess) - b).max()"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": 8,
153 | "metadata": {},
154 | "outputs": [
155 | {
156 | "name": "stdout",
157 | "output_type": "stream",
158 | "text": [
159 | "Number of steps needed: 36\n",
160 | "Backward error: 0.0000004579\n"
161 | ]
162 | }
163 | ],
164 | "source": [
165 | "print(\"Number of steps needed: %d\" % j)\n",
166 | "print(\"Backward error: %.10f\" % backward_error)"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 9,
172 | "metadata": {},
173 | "outputs": [],
174 | "source": [
175 | "# n = 100000\n",
176 | "n = 100000\n",
177 | "\n",
178 | "# Due to Memory Error, directly calculate the equation.\n",
179 | "# A = A_ij(n)\n",
180 | "b = np.ones(n)\n",
181 | "b[0], b[-1] = 2, 2\n",
182 | "x_true = np.ones(n)"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": 10,
188 | "metadata": {},
189 | "outputs": [],
190 | "source": [
191 | "j = 0\n",
192 | "guess = np.zeros(n)\n",
193 | "\n",
194 | "while True:\n",
195 | "# guess = Jacobi(A, b, 1, guess)\n",
196 | " guess = (b + np.concatenate((guess[1:], [0])) + np.concatenate(([0], guess[:-1]))) / 3\n",
197 | " j += 1 \n",
198 | " forward_error = abs(guess - x_true).max()\n",
199 | " if forward_error < 5e-7:\n",
200 | " break\n",
201 | "\n",
202 | "backward_error = abs((3*guess - np.concatenate((guess[1:], [0])) - np.concatenate(([0], guess[:-1]))) - b).max()"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": 11,
208 | "metadata": {
209 | "scrolled": true
210 | },
211 | "outputs": [
212 | {
213 | "name": "stdout",
214 | "output_type": "stream",
215 | "text": [
216 | "Number of steps needed: 36\n",
217 | "Backward error: 0.0000004579\n"
218 | ]
219 | }
220 | ],
221 | "source": [
222 | "print(\"Number of steps needed: %d\" % j)\n",
223 | "print(\"Backward error: %.10f\" % backward_error)"
224 | ]
225 | },
226 | {
227 | "cell_type": "markdown",
228 | "metadata": {},
229 | "source": [
230 | "### Q. 2"
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "execution_count": 12,
236 | "metadata": {},
237 | "outputs": [],
238 | "source": [
239 | "n = 100\n",
240 | "A = np.zeros((n, n))\n",
241 | "for i in range(n):\n",
242 | " A[i, i] = 2\n",
243 | " if i < n-1:\n",
244 | " A[i, i+1] = 1\n",
245 | " A[i+1, i] = 1\n",
246 | " \n",
247 | "b = np.zeros(n)\n",
248 | "b[0], b[-1] = 1, -1\n",
249 | "x_true = np.ones(n)\n",
250 | "x_true[1::2] -= 2"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 13,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": [
259 | "j = 0\n",
260 | "guess = np.zeros(n)\n",
261 | "\n",
262 | "while True:\n",
263 | " guess = Jacobi(A, b, 1, guess)\n",
264 | " j += 1 \n",
265 | " forward_error = abs(guess - x_true).max()\n",
266 | " if forward_error < 5e-4:\n",
267 | " break\n",
268 | " \n",
269 | "backward_error = abs(A.dot(guess) - b).max()"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 14,
275 | "metadata": {
276 | "scrolled": true
277 | },
278 | "outputs": [
279 | {
280 | "name": "stdout",
281 | "output_type": "stream",
282 | "text": [
283 | "Number of steps needed: 16209\n",
284 | "Backward error: 0.0000004836\n"
285 | ]
286 | }
287 | ],
288 | "source": [
289 | "print(\"Number of steps needed: %d\" % j)\n",
290 | "print(\"Backward error: %.10f\" % backward_error)"
291 | ]
292 | },
293 | {
294 | "cell_type": "markdown",
295 | "metadata": {},
296 | "source": [
297 | "### Q. 3"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 15,
303 | "metadata": {},
304 | "outputs": [],
305 | "source": [
306 | "A = np.zeros((6, 6))\n",
307 | "for i in range(6):\n",
308 | " A[i, 5-i] = 0.5\n",
309 | " A[i, i] = 3\n",
310 | " if i > 0:\n",
311 | " A[i, i-1] = -1\n",
312 | " A[i-1, i] = -1\n",
313 | "b = np.array([2.5, 1.5, 1, 1, 1.5, 2.5])\n",
314 | "x_true = np.ones(6)"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": 16,
320 | "metadata": {},
321 | "outputs": [],
322 | "source": [
323 | "guess = np.zeros(6)\n",
324 | "guess = Gauss_Seidel(A, b, 6, guess)"
325 | ]
326 | },
327 | {
328 | "cell_type": "code",
329 | "execution_count": 17,
330 | "metadata": {
331 | "scrolled": true
332 | },
333 | "outputs": [
334 | {
335 | "name": "stdout",
336 | "output_type": "stream",
337 | "text": [
338 | "Gauss-Seidel: [0.995 0.9946 0.9969 0.9996 1.0016 1.0013]\n"
339 | ]
340 | }
341 | ],
342 | "source": [
343 | "print(\"Gauss-Seidel:\", np.round(guess, 4))"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "### Q. 4"
351 | ]
352 | },
353 | {
354 | "cell_type": "code",
355 | "execution_count": 18,
356 | "metadata": {},
357 | "outputs": [],
358 | "source": [
359 | "A = np.zeros((6, 6))\n",
360 | "for i in range(6):\n",
361 | " A[i, 5-i] = 0.5\n",
362 | " A[i, i] = 3\n",
363 | " if i > 0:\n",
364 | " A[i, i-1] = -1\n",
365 | " A[i-1, i] = -1\n",
366 | "b = np.array([2.5, 1.5, 1, 1, 1.5, 2.5])\n",
367 | "x_true = np.ones(6)"
368 | ]
369 | },
370 | {
371 | "cell_type": "code",
372 | "execution_count": 19,
373 | "metadata": {},
374 | "outputs": [],
375 | "source": [
376 | "w = 1.1\n",
377 | "guess = np.zeros(6)\n",
378 | "guess = SOR(A, b, w, 6, guess)"
379 | ]
380 | },
381 | {
382 | "cell_type": "code",
383 | "execution_count": 20,
384 | "metadata": {},
385 | "outputs": [
386 | {
387 | "name": "stdout",
388 | "output_type": "stream",
389 | "text": [
390 | "SOR: [0.9989 0.9993 1.0004 1.0009 1.0009 1.0004]\n"
391 | ]
392 | }
393 | ],
394 | "source": [
395 | "print(\"SOR:\", np.round(guess, 4))"
396 | ]
397 | },
398 | {
399 | "cell_type": "markdown",
400 | "metadata": {},
401 | "source": [
402 | "### Q. 5"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 21,
408 | "metadata": {},
409 | "outputs": [],
410 | "source": [
411 | "n = 100\n",
412 | "\n",
413 | "A = np.zeros((n, n))\n",
414 | "for i in range(n):\n",
415 | " A[i, i] = 3\n",
416 | " if i < n-1:\n",
417 | " A[i, i+1] = -1\n",
418 | " A[i+1, i] = -1\n",
419 | "\n",
420 | "b = np.ones(n)\n",
421 | "b[0], b[-1] = 2, 2\n",
422 | "x_true = np.ones(n)"
423 | ]
424 | },
425 | {
426 | "cell_type": "code",
427 | "execution_count": 22,
428 | "metadata": {},
429 | "outputs": [],
430 | "source": [
431 | "# (a)\n",
432 | "j = 0\n",
433 | "guess = np.zeros(n)\n",
434 | "\n",
435 | "while True:\n",
436 | " guess = Gauss_Seidel(A, b, 1, guess)\n",
437 | " j += 1 \n",
438 | " forward_error = abs(guess - x_true).max()\n",
439 | " if forward_error < 5e-7:\n",
440 | " break\n",
441 | "\n",
442 | "backward_error = abs(A.dot(guess) - b).max()"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 23,
448 | "metadata": {},
449 | "outputs": [
450 | {
451 | "name": "stdout",
452 | "output_type": "stream",
453 | "text": [
454 | "Gauss-Seidel Method\n",
455 | "Number of steps needed: 21\n",
456 | "Backward error: 0.0000004779\n"
457 | ]
458 | }
459 | ],
460 | "source": [
461 | "print(\"Gauss-Seidel Method\")\n",
462 | "print(\"Number of steps needed: %d\" % j)\n",
463 | "print(\"Backward error: %.10f\" % backward_error)"
464 | ]
465 | },
466 | {
467 | "cell_type": "code",
468 | "execution_count": 24,
469 | "metadata": {},
470 | "outputs": [],
471 | "source": [
472 | "# (b)\n",
473 | "j = 0\n",
474 | "w = 1.2\n",
475 | "guess = np.zeros(n)\n",
476 | "\n",
477 | "while True:\n",
478 | " guess = SOR(A, b, w, 1, guess)\n",
479 | " j += 1 \n",
480 | " forward_error = abs(guess - x_true).max()\n",
481 | " if forward_error < 5e-7:\n",
482 | " break\n",
483 | "\n",
484 | "backward_error = abs(A.dot(guess) - b).max()"
485 | ]
486 | },
487 | {
488 | "cell_type": "code",
489 | "execution_count": 25,
490 | "metadata": {},
491 | "outputs": [
492 | {
493 | "name": "stdout",
494 | "output_type": "stream",
495 | "text": [
496 | "SOR Method\n",
497 | "Number of steps needed: 16\n",
498 | "Backward error: 0.0000015516\n"
499 | ]
500 | }
501 | ],
502 | "source": [
503 | "print(\"SOR Method\")\n",
504 | "print(\"Number of steps needed: %d\" % j)\n",
505 | "print(\"Backward error: %.10f\" % backward_error)"
506 | ]
507 | },
508 | {
509 | "cell_type": "markdown",
510 | "metadata": {},
511 | "source": [
512 | "### Q. 6"
513 | ]
514 | },
515 | {
516 | "cell_type": "code",
517 | "execution_count": 26,
518 | "metadata": {},
519 | "outputs": [],
520 | "source": [
521 | "n = 100\n",
522 | "A = np.zeros((n, n))\n",
523 | "for i in range(n):\n",
524 | " A[i, i] = 2\n",
525 | " if i < n-1:\n",
526 | " A[i, i+1] = 1\n",
527 | " A[i+1, i] = 1\n",
528 | " \n",
529 | "b = np.zeros(n)\n",
530 | "b[0], b[-1] = 1, -1\n",
531 | "x_true = np.ones(n)\n",
532 | "x_true[1::2] -= 2"
533 | ]
534 | },
535 | {
536 | "cell_type": "code",
537 | "execution_count": 27,
538 | "metadata": {},
539 | "outputs": [],
540 | "source": [
541 | "# (a)\n",
542 | "j = 0\n",
543 | "guess = np.zeros(n)\n",
544 | "\n",
545 | "while True:\n",
546 | " guess = Gauss_Seidel(A, b, 1, guess)\n",
547 | " j += 1 \n",
548 | " forward_error = abs(guess - x_true).max()\n",
549 | " if forward_error < 5e-4:\n",
550 | " break\n",
551 | " \n",
552 | "backward_error = abs(A.dot(guess) - b).max()"
553 | ]
554 | },
555 | {
556 | "cell_type": "code",
557 | "execution_count": 28,
558 | "metadata": {
559 | "scrolled": true
560 | },
561 | "outputs": [
562 | {
563 | "name": "stdout",
564 | "output_type": "stream",
565 | "text": [
566 | "Gauss-Seidel Method\n",
567 | "Number of steps needed: 8106\n",
568 | "Backward error: 0.0000004836\n"
569 | ]
570 | }
571 | ],
572 | "source": [
573 | "print(\"Gauss-Seidel Method\")\n",
574 | "print(\"Number of steps needed: %d\" % j)\n",
575 | "print(\"Backward error: %.10f\" % backward_error)"
576 | ]
577 | },
578 | {
579 | "cell_type": "code",
580 | "execution_count": 29,
581 | "metadata": {},
582 | "outputs": [],
583 | "source": [
584 | "# (b)\n",
585 | "j = 0\n",
586 | "w = 1.5\n",
587 | "guess = np.zeros(n)\n",
588 | "\n",
589 | "while True:\n",
590 | " guess = SOR(A, b, w, 1, guess)\n",
591 | " j += 1 \n",
592 | " forward_error = abs(guess - x_true).max()\n",
593 | " if forward_error < 5e-4:\n",
594 | " break\n",
595 | " \n",
596 | "backward_error = abs(A.dot(guess) - b).max()"
597 | ]
598 | },
599 | {
600 | "cell_type": "code",
601 | "execution_count": 30,
602 | "metadata": {},
603 | "outputs": [
604 | {
605 | "name": "stdout",
606 | "output_type": "stream",
607 | "text": [
608 | "SOR Method\n",
609 | "Number of steps needed: 2699\n",
610 | "Backward error: 0.0000004858\n"
611 | ]
612 | }
613 | ],
614 | "source": [
615 | "print(\"SOR Method\")\n",
616 | "print(\"Number of steps needed: %d\" % j)\n",
617 | "print(\"Backward error: %.10f\" % backward_error)"
618 | ]
619 | },
620 | {
621 | "cell_type": "markdown",
622 | "metadata": {},
623 | "source": [
624 | "### Q. 7"
625 | ]
626 | },
627 | {
628 | "cell_type": "code",
629 | "execution_count": 31,
630 | "metadata": {},
631 | "outputs": [
632 | {
633 | "name": "stdout",
634 | "output_type": "stream",
635 | "text": [
636 | "Size: 100 / Forward Error: 0.000000\n",
637 | "Size: 200 / Forward Error: 0.000000\n",
638 | "Size: 500 / Forward Error: 0.000000\n",
639 | "Size: 1000 / Forward Error: 0.000002\n",
640 | "Size: 2000 / Forward Error: 0.013012\n",
641 | "Size: 5000 / Forward Error: 0.216520\n",
642 | "Size: 10000 / Forward Error: 0.472222\n"
643 | ]
644 | }
645 | ],
646 | "source": [
647 | "for n in [100, 200, 500, 1000, 2000, 5000, 10000]:\n",
648 | " A = np.zeros((n, n))\n",
649 | " for i in range(n):\n",
650 | " A[i, n-1-i] = 0.5\n",
651 | " A[i, i] = 3\n",
652 | " if i > 0:\n",
653 | " A[i, i-1] = -1\n",
654 | " A[i-1, i] = -1\n",
655 | "\n",
656 | " b = 1.5 * np.ones(n)\n",
657 | " b[0], b[-1] = 2.5, 2.5\n",
658 | " b[n//2], b[n//2 - 1] = 1, 1\n",
659 | " x_true = np.ones(n)\n",
660 | "\n",
661 | " guess = np.zeros(n)\n",
662 | "\n",
663 | " tic = time()\n",
664 | " while True:\n",
665 | " guess = Gauss_Seidel(A, b, 1, guess)\n",
666 | " toc = time()\n",
667 | " if toc - tic > 1:\n",
668 | " break\n",
669 | " forward_error = abs(guess - x_true).max()\n",
670 | " \n",
671 | " print(\"Size: %5d / Forward Error: %f\" % (n, forward_error))"
672 | ]
673 | }
674 | ],
675 | "metadata": {
676 | "kernelspec": {
677 | "display_name": "Python 3",
678 | "language": "python",
679 | "name": "python3"
680 | },
681 | "language_info": {
682 | "codemirror_mode": {
683 | "name": "ipython",
684 | "version": 3
685 | },
686 | "file_extension": ".py",
687 | "mimetype": "text/x-python",
688 | "name": "python",
689 | "nbconvert_exporter": "python",
690 | "pygments_lexer": "ipython3",
691 | "version": "3.6.4"
692 | }
693 | },
694 | "nbformat": 4,
695 | "nbformat_minor": 2
696 | }
697 |
--------------------------------------------------------------------------------
/Ch_02/Ch_2_7.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 2,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "sqrt = np.sqrt"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "# Chapter 2
Systems of Equations"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "## 2.7 Nonlinear Systems of Equations"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 3,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "def Gaussian_elimination(equation):\n",
42 | " A = equation.copy()\n",
43 | " m, n = A.shape\n",
44 | "\n",
45 | " for i in range(m):\n",
46 | " A[i] = A[i] / A[i, i]\n",
47 | " for j in range(i+1, m):\n",
48 | " A[j] -= A[j, i]*A[i]\n",
49 | "\n",
50 | " for i in range(m-1, 0, -1):\n",
51 | " for j in range(i):\n",
52 | " A[j] -= A[j, i]*A[i]\n",
53 | " \n",
54 | " return A"
55 | ]
56 | },
57 | {
58 | "cell_type": "code",
59 | "execution_count": 4,
60 | "metadata": {},
61 | "outputs": [],
62 | "source": [
63 | "def Newton_Method(F, DF, x, iter_num=10**4):\n",
64 | " for _ in range(iter_num):\n",
65 | " s = np.linalg.inv(DF(x)).dot(F(x))\n",
66 | "# A = np.insert(np.array(F(x), dtype=float).reshape(-1, 1), [0], DF(x), axis=1)\n",
67 | "# s = Gaussian_elimination(A)[:, -1]\n",
68 | " \n",
69 | " x -= s\n",
70 | " \n",
71 | " return x"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": 5,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "def Broyden_Method_I(F, x, A, iter_num=10**4):\n",
81 | " x = np.array(x, dtype=float)\n",
82 | " n = len(x)\n",
83 | " \n",
84 | " for _ in range(iter_num):\n",
85 | " d_i = -np.linalg.inv(A).dot(F(x))\n",
86 | " D_i = F(x + d_i) - F(x)\n",
87 | " A += (D_i - A.dot(d_i)).reshape(n, 1).dot(d_i.reshape(1, n)) / d_i.dot(d_i)\n",
88 | " x += d_i\n",
89 | " \n",
90 | " return x "
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": 6,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": [
99 | "def Broyden_Method_II(F, x, B, iter_num=10**4):\n",
100 | " x = np.array(x, dtype=float)\n",
101 | " n = len(x)\n",
102 | " \n",
103 | " for _ in range(iter_num):\n",
104 | " d_i = -B.dot(F(x))\n",
105 | " D_i = F(x + d_i) - F(x)\n",
106 | " B += (d_i - B.dot(D_i)).reshape(n, 1).dot(d_i.reshape(1, n)).dot(B) / d_i.dot(B).dot(D_i)\n",
107 | " x += d_i\n",
108 | " \n",
109 | " return x"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "### Q. 1"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 7,
122 | "metadata": {},
123 | "outputs": [],
124 | "source": [
125 | "# (a)\n",
126 | "def F(x):\n",
127 | " u, v = x\n",
128 | " f1 = u**2 + v**2 - 1\n",
129 | " f2 = (u-1)**2 + v**2 - 1\n",
130 | " \n",
131 | " return np.array([f1, f2]) \n",
132 | "\n",
133 | "def DF(x):\n",
134 | " u, v = x\n",
135 | " df1 = (2*u, 2*v)\n",
136 | " df2 = (2*u - 2, 2*v)\n",
137 | " \n",
138 | " return np.array([df1, df2])\n",
139 | "\n",
140 | "x_true = [(0.5, sqrt(3)/2), (0.5, -sqrt(3)/2)]"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": 8,
146 | "metadata": {},
147 | "outputs": [
148 | {
149 | "name": "stdout",
150 | "output_type": "stream",
151 | "text": [
152 | "(u, v): (0.5, 0.8660254037844387) Ans: (0.5, 0.8660254037844386)\n",
153 | "(u, v): (0.5, -0.8660254037844387) Ans: (0.5, -0.8660254037844386)\n"
154 | ]
155 | }
156 | ],
157 | "source": [
158 | "starting_points = [(0, 1), (0, -1)]\n",
159 | "\n",
160 | "for i, x_0 in enumerate(starting_points):\n",
161 | " x = Newton_Method(F, DF, x_0)\n",
162 | " print(\"(u, v):\", tuple(x), \"Ans:\", tuple(x_true[i]))"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 9,
168 | "metadata": {},
169 | "outputs": [],
170 | "source": [
171 | "# (b)\n",
172 | "def F(x):\n",
173 | " u, v = x\n",
174 | " f1 = u**2 + 4*v**2 - 4\n",
175 | " f2 = 4*u**2 + v**2 - 4\n",
176 | " \n",
177 | " return np.array([f1, f2])\n",
178 | "\n",
179 | "def DF(x):\n",
180 | " u, v = x\n",
181 | " df1 = (2*u, 8*v)\n",
182 | " df2 = (8*u, 2*v)\n",
183 | " \n",
184 | " return np.array([df1, df2])\n",
185 | "\n",
186 | "x_true = [(2/sqrt(5), 2/sqrt(5)), (2/sqrt(5), -2/sqrt(5)),\n",
187 | " (-2/sqrt(5), 2/sqrt(5)), (-2/sqrt(5), -2/sqrt(5))]"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": 10,
193 | "metadata": {},
194 | "outputs": [
195 | {
196 | "name": "stdout",
197 | "output_type": "stream",
198 | "text": [
199 | "(u, v): (0.8944271909999159, 0.8944271909999159) Ans: (0.8944271909999159, 0.8944271909999159)\n",
200 | "(u, v): (0.8944271909999159, -0.8944271909999159) Ans: (0.8944271909999159, -0.8944271909999159)\n",
201 | "(u, v): (-0.8944271909999159, 0.8944271909999159) Ans: (-0.8944271909999159, 0.8944271909999159)\n",
202 | "(u, v): (-0.8944271909999159, -0.8944271909999159) Ans: (-0.8944271909999159, -0.8944271909999159)\n"
203 | ]
204 | }
205 | ],
206 | "source": [
207 | "starting_points = [(1, 1), (1, -1), (-1, 1), (-1, -1)]\n",
208 | "\n",
209 | "for i, x_0 in enumerate(starting_points):\n",
210 | " x = Newton_Method(F, DF, x_0)\n",
211 | " print(\"(u, v):\", tuple(x), \"Ans:\", x_true[i])"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 11,
217 | "metadata": {
218 | "scrolled": true
219 | },
220 | "outputs": [],
221 | "source": [
222 | "# (c)\n",
223 | "def F(x):\n",
224 | " u, v = x\n",
225 | " f1 = u**2 - 4*v**2 - 4\n",
226 | " f2 = (u-1)**2 + v**2 - 4\n",
227 | " \n",
228 | " return np.array([f1, f2])\n",
229 | "\n",
230 | "def DF(x):\n",
231 | " u, v = x\n",
232 | " df1 = (2*u, -8*v)\n",
233 | " df2 = (2*u - 2, 2*v)\n",
234 | " \n",
235 | " return np.array([df1, df2])\n",
236 | "\n",
237 | "x_true = [((4 + 4*sqrt(6))/5, sqrt(3 + 8*sqrt(6))/5), ((4 + 4*sqrt(6))/5, -sqrt(3 + 8*sqrt(6))/5)]"
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 12,
243 | "metadata": {
244 | "scrolled": true
245 | },
246 | "outputs": [
247 | {
248 | "name": "stdout",
249 | "output_type": "stream",
250 | "text": [
251 | "(u, v): (2.7595917942265427, 0.9507032753128691) Ans: (2.759591794226542, 0.950703275312869)\n",
252 | "(u, v): (2.7595917942265427, -0.9507032753128691) Ans: (2.759591794226542, -0.950703275312869)\n"
253 | ]
254 | }
255 | ],
256 | "source": [
257 | "starting_points = [(1, 1), (1, -1)]\n",
258 | "\n",
259 | "for i, x_0 in enumerate(starting_points):\n",
260 | " x = Newton_Method(F, DF, x_0)\n",
261 | " print(\"(u, v):\", tuple(x), \"Ans:\", x_true[i])"
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "metadata": {},
267 | "source": [
268 | "### Q. 2"
269 | ]
270 | },
271 | {
272 | "cell_type": "code",
273 | "execution_count": 13,
274 | "metadata": {},
275 | "outputs": [],
276 | "source": [
277 | "# The question was meant to refer to the example 2.33\n",
278 | "def F(x):\n",
279 | " u, v = x\n",
280 | " f1 = 6*u**3 + u*v - 3*v**3 - 4\n",
281 | " f2 = u**2 - 18*u*v**2 + 16*v**3 + 1\n",
282 | " \n",
283 | " return np.array([f1, f2])\n",
284 | "\n",
285 | "def DF(x):\n",
286 | " u, v = x\n",
287 | " df1 = (18*u**2 + v, u - 9*v**2)\n",
288 | " df2 = (2*u - 18*v**2, -36*u*v + 48*v**2)\n",
289 | " \n",
290 | " return np.array([df1, df2])"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 14,
296 | "metadata": {},
297 | "outputs": [
298 | {
299 | "name": "stdout",
300 | "output_type": "stream",
301 | "text": [
302 | "(u, v): (1.0, 1.0)\n",
303 | "(u, v): (0.8868094164160325, -0.29400704390184035)\n",
304 | "(u, v): (0.8659389188736499, 0.46216792132821477)\n",
305 | "(u, v): (0.8868094164160325, -0.29400704390184035)\n"
306 | ]
307 | }
308 | ],
309 | "source": [
310 | "starting_points = [(1, 1), (1, -1), (-1, 1), (-1, -1)]\n",
311 | "\n",
312 | "for x_0 in starting_points:\n",
313 | " x = Newton_Method(F, DF, x_0)\n",
314 | " print(\"(u, v):\", tuple(x))"
315 | ]
316 | },
317 | {
318 | "cell_type": "markdown",
319 | "metadata": {},
320 | "source": [
321 | "### Q. 3"
322 | ]
323 | },
324 | {
325 | "cell_type": "code",
326 | "execution_count": 15,
327 | "metadata": {},
328 | "outputs": [],
329 | "source": [
330 | "def F(x):\n",
331 | " u, v = x\n",
332 | " f1 = u**3 - v**3 + u\n",
333 | " f2 = u**2 + v**2 - 1\n",
334 | " \n",
335 | " return np.array([f1, f2])\n",
336 | "\n",
337 | "def DF(x):\n",
338 | " u, v = x\n",
339 | " df1 = (3*u**2 + 1, -3*v**2)\n",
340 | " df2 = (2*u, 2*v)\n",
341 | " \n",
342 | " return np.array([df1, df2])"
343 | ]
344 | },
345 | {
346 | "cell_type": "code",
347 | "execution_count": 16,
348 | "metadata": {},
349 | "outputs": [
350 | {
351 | "name": "stdout",
352 | "output_type": "stream",
353 | "text": [
354 | "(u, v): (0.507992000407952, 0.8613617866619853)\n",
355 | "(u, v): (-0.5079920004079519, -0.8613617866619853)\n",
356 | "(u, v): (0.5079920004079519, 0.8613617866619853)\n",
357 | "(u, v): (-0.507992000407952, -0.8613617866619853)\n"
358 | ]
359 | }
360 | ],
361 | "source": [
362 | "starting_points = [(1, 1), (1, -1), (-1, 1), (-1, -1)]\n",
363 | "\n",
364 | "for x_0 in starting_points:\n",
365 | " x = Newton_Method(F, DF, x_0)\n",
366 | " print(\"(u, v):\", tuple(x))"
367 | ]
368 | },
369 | {
370 | "cell_type": "markdown",
371 | "metadata": {},
372 | "source": [
373 | "### Q. 4"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": 17,
379 | "metadata": {},
380 | "outputs": [],
381 | "source": [
382 | "def F(x):\n",
383 | " u, v, w = x\n",
384 | " f1 = 2*u**2 - 4*u + v**2 + 3*w**2 + 6*w + 2\n",
385 | " f2 = u**2 + v**2 - 2*v + 2*w**2 - 5\n",
386 | " f3 = 3*u**2 - 12*u + v**2 + 3*w**2 + 8\n",
387 | " \n",
388 | " return np.array([f1, f2, f3])\n",
389 | "\n",
390 | "def DF(x):\n",
391 | " u, v, w = x\n",
392 | " df1 = (4*u - 4, 2*v, 6*w + 6)\n",
393 | " df2 = (2*u, 2*v - 2, 4*w)\n",
394 | " df3 = (6*u - 12, 2*v, 6*w)\n",
395 | " \n",
396 | " return np.array([df1, df2, df3])"
397 | ]
398 | },
399 | {
400 | "cell_type": "code",
401 | "execution_count": 18,
402 | "metadata": {},
403 | "outputs": [
404 | {
405 | "name": "stdout",
406 | "output_type": "stream",
407 | "text": [
408 | "(u, v, w): (1.096017841000413, -1.1592471842105878, -0.2611479367020165)\n",
409 | "(u, v, w): (2.0, 0.9999999999999999, -0.9999999999999999)\n",
410 | "(u, v, w): (1.096017841000413, -1.1592471842105878, -0.2611479367020165)\n",
411 | "(u, v, w): (1.0960178410004133, -1.1592471842105876, -0.26114793670201647)\n",
412 | "(u, v, w): (1.096017841000413, -1.1592471842105878, -0.2611479367020165)\n",
413 | "(u, v, w): (2.0, 1.0000000000000002, -1.0)\n",
414 | "(u, v, w): (1.0960178410004133, -1.1592471842105876, -0.26114793670201647)\n",
415 | "(u, v, w): (1.0960178410004133, -1.1592471842105876, -0.26114793670201647)\n"
416 | ]
417 | }
418 | ],
419 | "source": [
420 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
421 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
422 | "\n",
423 | "for x_0 in starting_points:\n",
424 | " x = Newton_Method(F, DF, x_0)\n",
425 | " print(\"(u, v, w):\", tuple(x))"
426 | ]
427 | },
428 | {
429 | "cell_type": "markdown",
430 | "metadata": {},
431 | "source": [
432 | "### Q. 5"
433 | ]
434 | },
435 | {
436 | "cell_type": "code",
437 | "execution_count": 19,
438 | "metadata": {},
439 | "outputs": [],
440 | "source": [
441 | "# (a)\n",
442 | "def F(x):\n",
443 | " u, v, w = x\n",
444 | " f1 = (u-1)**2 + (v-1)**2 + w**2 - 1\n",
445 | " f2 = (u-1)**2 + v**2 + (w-1)**2 - 1\n",
446 | " f3 = u**2 + (v-1)**2 + (w-1)**2 - 1\n",
447 | " \n",
448 | " return np.array([f1, f2, f3])\n",
449 | "\n",
450 | "def DF(x):\n",
451 | " u, v, w = x\n",
452 | " df1 = (2*u - 2, 2*v - 2, 2*w)\n",
453 | " df2 = (2*u - 2, 2*v, 2*w - 2)\n",
454 | " df3 = (2*u, 2*v - 2, 2*w - 2)\n",
455 | " \n",
456 | " return np.array([df1, df2, df3])"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 20,
462 | "metadata": {},
463 | "outputs": [
464 | {
465 | "name": "stdout",
466 | "output_type": "stream",
467 | "text": [
468 | "(u, v, w): (1.0, 1.0, 1.0)\n",
469 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333332)\n",
470 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333332)\n",
471 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333332)\n",
472 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333332)\n",
473 | "(u, v, w): (0.33333333333333337, 0.33333333333333337, 0.33333333333333337)\n",
474 | "(u, v, w): (0.33333333333333337, 0.33333333333333337, 0.33333333333333337)\n",
475 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333332)\n"
476 | ]
477 | }
478 | ],
479 | "source": [
480 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
481 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
482 | "\n",
483 | "for x_0 in starting_points:\n",
484 | " x = Newton_Method(F, DF, x_0)\n",
485 | " print(\"(u, v, w):\", tuple(x))"
486 | ]
487 | },
488 | {
489 | "cell_type": "code",
490 | "execution_count": 21,
491 | "metadata": {},
492 | "outputs": [],
493 | "source": [
494 | "# (b)\n",
495 | "def F(x):\n",
496 | " u, v, w = x\n",
497 | " f1 = (u-1)**2 + (v+2)**2 + w**2 - 25\n",
498 | " f2 = (u+2)**2 + (v-2)**2 + (w+1)**2 - 25\n",
499 | " f3 = (u-4)**2 + (v+2)**2 + (w-3)**2 - 25\n",
500 | " \n",
501 | " return np.array([f1, f2, f3])\n",
502 | "\n",
503 | "def DF(x):\n",
504 | " u, v, w = x\n",
505 | " df1 = (2*u - 2, 2*v + 4, 2*w)\n",
506 | " df2 = (2*u + 4, 2*v - 4, 2*w + 2)\n",
507 | " df3 = (2*u - 8, 2*v + 4, 2*w - 6)\n",
508 | " \n",
509 | " return np.array([df1, df2, df3])"
510 | ]
511 | },
512 | {
513 | "cell_type": "code",
514 | "execution_count": 22,
515 | "metadata": {
516 | "scrolled": true
517 | },
518 | "outputs": [
519 | {
520 | "name": "stdout",
521 | "output_type": "stream",
522 | "text": [
523 | "(u, v, w): (1.8888888888888902, 2.4444444444444446, 2.11111111111111)\n",
524 | "(u, v, w): (1.88888888888889, 2.444444444444445, 2.11111111111111)\n",
525 | "(u, v, w): (1.0000000000000009, 2.000000000000001, 2.999999999999999)\n",
526 | "(u, v, w): (1.8888888888888897, 2.4444444444444446, 2.1111111111111103)\n",
527 | "(u, v, w): (1.0000000000000027, 2.0000000000000018, 2.999999999999998)\n",
528 | "(u, v, w): (1.8888888888888895, 2.444444444444445, 2.1111111111111103)\n",
529 | "(u, v, w): (0.999999999999999, 1.9999999999999993, 3.000000000000001)\n",
530 | "(u, v, w): (1.0000000000000007, 2.000000000000001, 2.999999999999999)\n"
531 | ]
532 | }
533 | ],
534 | "source": [
535 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
536 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
537 | "\n",
538 | "for x_0 in starting_points:\n",
539 | " x = Newton_Method(F, DF, x_0)\n",
540 | " print(\"(u, v, w):\", tuple(x))"
541 | ]
542 | },
543 | {
544 | "cell_type": "markdown",
545 | "metadata": {},
546 | "source": [
547 | "### Q. 6"
548 | ]
549 | },
550 | {
551 | "cell_type": "code",
552 | "execution_count": 23,
553 | "metadata": {},
554 | "outputs": [],
555 | "source": [
556 | "def F(x):\n",
557 | " u, v, w = x\n",
558 | " f1 = (u-1)**2 + v**2 + (w-1)**2 - 8\n",
559 | " f2 = u**2 + (v-2)**2 + (w-2)**2 - 2\n",
560 | " f3 = u**2 + (v-3)**2 + (w-3)**2 - 2\n",
561 | " \n",
562 | " return np.array([f1, f2, f3])\n",
563 | "\n",
564 | "def DF(x):\n",
565 | " u, v, w = x\n",
566 | " df1 = (2*u - 2, 2*v, 2*w - 2)\n",
567 | " df2 = (2*u, 2*v - 4, 2*w - 4)\n",
568 | " df3 = (2*u, 2*v - 6, 2*w - 6)\n",
569 | " \n",
570 | " return np.array([df1, df2, df3])"
571 | ]
572 | },
573 | {
574 | "cell_type": "code",
575 | "execution_count": 24,
576 | "metadata": {},
577 | "outputs": [
578 | {
579 | "name": "stdout",
580 | "output_type": "stream",
581 | "text": [
582 | "(u, v, w): (1.000000012345515, 2.0000000123455153, 2.9999999876544847)\n",
583 | "(u, v, w): (1.0000000078038922, 2.000000007803892, 2.999999992196108)\n",
584 | "(u, v, w): (0.9999999799927836, 1.9999999799927841, 3.000000020007216)\n",
585 | "(u, v, w): (1.0000000066934143, 2.0000000066934143, 2.9999999933065857)\n",
586 | "(u, v, w): (0.999999987629457, 1.9999999876294572, 3.000000012370543)\n",
587 | "(u, v, w): (1.0000000154674602, 2.0000000154674606, 2.9999999845325394)\n",
588 | "(u, v, w): (0.9999999918520664, 1.9999999918520666, 3.0000000081479334)\n",
589 | "(u, v, w): (0.9999999878264226, 1.9999999878264227, 3.0000000121735773)\n"
590 | ]
591 | }
592 | ],
593 | "source": [
594 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
595 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
596 | "\n",
597 | "for x_0 in starting_points:\n",
598 | " x = Newton_Method(F, DF, x_0)\n",
599 | " print(\"(u, v, w):\", tuple(x))"
600 | ]
601 | },
602 | {
603 | "cell_type": "markdown",
604 | "metadata": {},
605 | "source": [
606 | "The iteration does not converge quadratically."
607 | ]
608 | },
609 | {
610 | "cell_type": "markdown",
611 | "metadata": {},
612 | "source": [
613 | "### Q. 7"
614 | ]
615 | },
616 | {
617 | "cell_type": "code",
618 | "execution_count": 25,
619 | "metadata": {},
620 | "outputs": [],
621 | "source": [
622 | "def F(x):\n",
623 | " u, v = x\n",
624 | " f1 = u**3 - v**3 + u\n",
625 | " f2 = u**2 + v**2 - 1\n",
626 | " \n",
627 | " return np.array([f1, f2])"
628 | ]
629 | },
630 | {
631 | "cell_type": "code",
632 | "execution_count": 26,
633 | "metadata": {},
634 | "outputs": [
635 | {
636 | "name": "stdout",
637 | "output_type": "stream",
638 | "text": [
639 | "Step: 67 / (u, v): (0.5079920004079519, 0.8613617866619853)\n"
640 | ]
641 | }
642 | ],
643 | "source": [
644 | "x_0 = (1, 1)\n",
645 | "A = np.identity(2)\n",
646 | "\n",
647 | "required_step = 67\n",
648 | "x = Broyden_Method_I(F, x_0, A, required_step)\n",
649 | "print(\"Step: %d / (u, v):\" % required_step, tuple(x))"
650 | ]
651 | },
652 | {
653 | "cell_type": "markdown",
654 | "metadata": {},
655 | "source": [
656 | "### Q. 8"
657 | ]
658 | },
659 | {
660 | "cell_type": "code",
661 | "execution_count": 27,
662 | "metadata": {},
663 | "outputs": [],
664 | "source": [
665 | "def F(x):\n",
666 | " u, v = x\n",
667 | " f1 = u**3 - v**3 + u\n",
668 | " f2 = u**2 + v**2 - 1\n",
669 | " \n",
670 | " return np.array([f1, f2])"
671 | ]
672 | },
673 | {
674 | "cell_type": "code",
675 | "execution_count": 28,
676 | "metadata": {
677 | "scrolled": true
678 | },
679 | "outputs": [
680 | {
681 | "name": "stdout",
682 | "output_type": "stream",
683 | "text": [
684 | "Step: 13 / (u, v): (0.507992000407952, 0.8613617866619853)\n"
685 | ]
686 | }
687 | ],
688 | "source": [
689 | "x_0 = (1, 1)\n",
690 | "B = np.identity(2)\n",
691 | "\n",
692 | "required_step = 13\n",
693 | "x = Broyden_Method_II(F, x_0, B, required_step)\n",
694 | "print(\"Step: %d / (u, v):\" % required_step, tuple(x))"
695 | ]
696 | },
697 | {
698 | "cell_type": "markdown",
699 | "metadata": {},
700 | "source": [
701 | "### Q. 9"
702 | ]
703 | },
704 | {
705 | "cell_type": "code",
706 | "execution_count": 29,
707 | "metadata": {},
708 | "outputs": [],
709 | "source": [
710 | "# (a)\n",
711 | "def F(x):\n",
712 | " u, v, w = x\n",
713 | " f1 = (u-1)**2 + (v-1)**2 + w**2 - 1\n",
714 | " f2 = (u-1)**2 + v**2 + (w-1)**2 - 1\n",
715 | " f3 = u**2 + (v-1)**2 + (w-1)**2 - 1\n",
716 | " \n",
717 | " return np.array([f1, f2, f3])"
718 | ]
719 | },
720 | {
721 | "cell_type": "code",
722 | "execution_count": 30,
723 | "metadata": {},
724 | "outputs": [
725 | {
726 | "name": "stdout",
727 | "output_type": "stream",
728 | "text": [
729 | "(u, v, w): (nan, nan, nan)\n",
730 | "(u, v, w): (0.33524142813670843, 0.33410387428267685, 0.3354162658401394)\n",
731 | "(u, v, w): (0.3333333333322386, 0.3333333333333353, 0.33333333333443216)\n",
732 | "(u, v, w): (0.3333330748493395, 0.3333330687839443, 0.33333307709030124)\n",
733 | "(u, v, w): (0.3354162658348616, 0.3341038742776292, 0.33524142813145863)\n",
734 | "(u, v, w): (0.3333333333333333, 0.3333333333333333, 0.3333333333333333)\n",
735 | "(u, v, w): (0.33333307709030124, 0.3333330687839443, 0.3333330748493395)\n",
736 | "(u, v, w): (nan, nan, nan)\n"
737 | ]
738 | },
739 | {
740 | "name": "stderr",
741 | "output_type": "stream",
742 | "text": [
743 | "C:\\Users\\snuist\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: RuntimeWarning: invalid value encountered in true_divide\n",
744 | " \n"
745 | ]
746 | }
747 | ],
748 | "source": [
749 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
750 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
751 | "\n",
752 | "for i, x_0 in enumerate(starting_points):\n",
753 | " A = np.identity(3)\n",
754 | " x = Broyden_Method_I(F, x_0, A, 20)\n",
755 | " print(\"(u, v, w):\", tuple(x))"
756 | ]
757 | },
758 | {
759 | "cell_type": "code",
760 | "execution_count": 31,
761 | "metadata": {},
762 | "outputs": [],
763 | "source": [
764 | "# (b)\n",
765 | "def F(x):\n",
766 | " u, v, w = x\n",
767 | " f1 = (u-1)**2 + (v+2)**2 + w**2 - 25\n",
768 | " f2 = (u+2)**2 + (v-2)**2 + (w+1)**2 - 25\n",
769 | " f3 = (u-4)**2 + (v+2)**2 + (w-3)**2 - 25\n",
770 | " \n",
771 | " return np.array([f1, f2, f3])"
772 | ]
773 | },
774 | {
775 | "cell_type": "code",
776 | "execution_count": 32,
777 | "metadata": {
778 | "scrolled": true
779 | },
780 | "outputs": [
781 | {
782 | "name": "stdout",
783 | "output_type": "stream",
784 | "text": [
785 | "(u, v, w): (1.8888888888887956, 2.4444444444444016, 2.1111111111112035)\n",
786 | "(u, v, w): (1.8888888888888886, 2.4444444444444438, 2.1111111111111116)\n",
787 | "(u, v, w): (1.8888888888930018, 2.4444444444465323, 2.1111111111070153)\n",
788 | "(u, v, w): (1.8888888888867532, 2.444444444443375, 2.111111111113244)\n",
789 | "(u, v, w): (0.9999999999999893, 1.9999999999999947, 3.0000000000000107)\n",
790 | "(u, v, w): (1.8888888888888904, 2.444444444444445, 2.1111111111111094)\n",
791 | "(u, v, w): (1.0000000000004665, 2.0000000000002327, 2.9999999999995337)\n",
792 | "(u, v, w): (1.888888888888895, 2.4444444444444473, 2.111111111111105)\n"
793 | ]
794 | },
795 | {
796 | "name": "stderr",
797 | "output_type": "stream",
798 | "text": [
799 | "C:\\Users\\snuist\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: RuntimeWarning: invalid value encountered in true_divide\n",
800 | " \n"
801 | ]
802 | }
803 | ],
804 | "source": [
805 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
806 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
807 | "\n",
808 | "for x_0 in starting_points:\n",
809 | " A = np.identity(3)\n",
810 | " x = Broyden_Method_I(F, x_0, A, 28)\n",
811 | " print(\"(u, v, w):\", tuple(x))"
812 | ]
813 | },
814 | {
815 | "cell_type": "markdown",
816 | "metadata": {},
817 | "source": [
818 | "### Q. 10"
819 | ]
820 | },
821 | {
822 | "cell_type": "code",
823 | "execution_count": 33,
824 | "metadata": {},
825 | "outputs": [],
826 | "source": [
827 | "def F(x):\n",
828 | " u, v, w = x\n",
829 | " f1 = (u-1)**2 + v**2 + (w-1)**2 - 8\n",
830 | " f2 = u**2 + (v-2)**2 + (w-2)**2 - 2\n",
831 | " f3 = u**2 + (v-3)**2 + (w-3)**2 - 2\n",
832 | " \n",
833 | " return np.array([f1, f2, f3])"
834 | ]
835 | },
836 | {
837 | "cell_type": "code",
838 | "execution_count": 34,
839 | "metadata": {
840 | "scrolled": false
841 | },
842 | "outputs": [
843 | {
844 | "name": "stdout",
845 | "output_type": "stream",
846 | "text": [
847 | "(u, v, w): (1.0000025946658384, 2.000002594665859, 2.999997405334141)\n",
848 | "(u, v, w): (0.999983118772993, 1.9999831187729933, 3.0000168812270065)\n",
849 | "(u, v, w): (0.9999986157486179, 1.9999986157486145, 3.000001384251386)\n",
850 | "(u, v, w): (1.0000050445765947, 2.000005044576596, 2.9999949554234044)\n",
851 | "(u, v, w): (1.000005242933653, 2.0000052429336526, 2.9999947570663474)\n",
852 | "(u, v, w): (1.0000000266555993, 2.0000000266555986, 2.999999973344402)\n",
853 | "(u, v, w): (0.9999988369809253, 1.9999988369809265, 3.0000011630190735)\n",
854 | "(u, v, w): (0.9999999646310582, 1.999999964631058, 3.000000035368942)\n"
855 | ]
856 | }
857 | ],
858 | "source": [
859 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
860 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
861 | "\n",
862 | "for x_0 in starting_points:\n",
863 | " A = np.identity(3)\n",
864 | " x = Broyden_Method_I(F, x_0, A, 48)\n",
865 | " print(\"(u, v, w):\", tuple(x))"
866 | ]
867 | },
868 | {
869 | "cell_type": "markdown",
870 | "metadata": {},
871 | "source": [
872 | "### Q. 11"
873 | ]
874 | },
875 | {
876 | "cell_type": "code",
877 | "execution_count": 35,
878 | "metadata": {},
879 | "outputs": [],
880 | "source": [
881 | "# (a)\n",
882 | "def F(x):\n",
883 | " u, v, w = x\n",
884 | " f1 = (u-1)**2 + (v-1)**2 + w**2 - 1\n",
885 | " f2 = (u-1)**2 + v**2 + (w-1)**2 - 1\n",
886 | " f3 = u**2 + (v-1)**2 + (w-1)**2 - 1\n",
887 | " \n",
888 | " return np.array([f1, f2, f3])"
889 | ]
890 | },
891 | {
892 | "cell_type": "code",
893 | "execution_count": 36,
894 | "metadata": {},
895 | "outputs": [
896 | {
897 | "name": "stdout",
898 | "output_type": "stream",
899 | "text": [
900 | "(u, v, w): (nan, nan, nan)\n",
901 | "(u, v, w): (0.33524142809960555, 0.33410387424698323, 0.3354162658028217)\n",
902 | "(u, v, w): (0.33333333333330323, 0.33333333333333265, 0.333333333333362)\n",
903 | "(u, v, w): (0.3333330748493391, 0.333333068783944, 0.333333077090301)\n",
904 | "(u, v, w): (0.3354162658320563, 0.33410387427494426, 0.33524142812867086)\n",
905 | "(u, v, w): (0.33333333333333337, 0.3333333333333333, 0.33333333333333337)\n",
906 | "(u, v, w): (0.3333330770903015, 0.3333330687839446, 0.3333330748493399)\n",
907 | "(u, v, w): (nan, nan, nan)\n"
908 | ]
909 | },
910 | {
911 | "name": "stderr",
912 | "output_type": "stream",
913 | "text": [
914 | "C:\\Users\\snuist\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: RuntimeWarning: invalid value encountered in true_divide\n",
915 | " \n"
916 | ]
917 | }
918 | ],
919 | "source": [
920 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
921 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
922 | "\n",
923 | "for i, x_0 in enumerate(starting_points):\n",
924 | " B = np.identity(3)\n",
925 | " x = Broyden_Method_II(F, x_0, B, 20)\n",
926 | " print(\"(u, v, w):\", tuple(x))"
927 | ]
928 | },
929 | {
930 | "cell_type": "code",
931 | "execution_count": 37,
932 | "metadata": {},
933 | "outputs": [],
934 | "source": [
935 | "# (b)\n",
936 | "def F(x):\n",
937 | " u, v, w = x\n",
938 | " f1 = (u-1)**2 + (v+2)**2 + w**2 - 25\n",
939 | " f2 = (u+2)**2 + (v-2)**2 + (w+1)**2 - 25\n",
940 | " f3 = (u-4)**2 + (v+2)**2 + (w-3)**2 - 25\n",
941 | " \n",
942 | " return np.array([f1, f2, f3])"
943 | ]
944 | },
945 | {
946 | "cell_type": "code",
947 | "execution_count": 38,
948 | "metadata": {
949 | "scrolled": true
950 | },
951 | "outputs": [
952 | {
953 | "name": "stdout",
954 | "output_type": "stream",
955 | "text": [
956 | "(u, v, w): (1.8888888888888955, 2.444444444444447, 2.111111111111103)\n",
957 | "(u, v, w): (1.8888888888888757, 2.4444444444444384, 2.1111111111111245)\n",
958 | "(u, v, w): (1.8888888888929614, 2.4444444444465123, 2.1111111111070553)\n",
959 | "(u, v, w): (1.8888888888870101, 2.4444444444435027, 2.111111111112987)\n",
960 | "(u, v, w): (nan, nan, nan)\n",
961 | "(u, v, w): (1.8888888888886974, 2.4444444444443487, 2.1111111111113017)\n",
962 | "(u, v, w): (0.999999999999757, 1.999999999999878, 3.0000000000002434)\n",
963 | "(u, v, w): (1.8888888888888833, 2.444444444444442, 2.1111111111111165)\n"
964 | ]
965 | },
966 | {
967 | "name": "stderr",
968 | "output_type": "stream",
969 | "text": [
970 | "C:\\Users\\snuist\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: RuntimeWarning: invalid value encountered in true_divide\n",
971 | " \n"
972 | ]
973 | }
974 | ],
975 | "source": [
976 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
977 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
978 | "\n",
979 | "for x_0 in starting_points:\n",
980 | " B = np.identity(3)\n",
981 | " x = Broyden_Method_II(F, x_0, B, 28)\n",
982 | " print(\"(u, v, w):\", tuple(x))"
983 | ]
984 | },
985 | {
986 | "cell_type": "markdown",
987 | "metadata": {},
988 | "source": [
989 | "### Q. 12"
990 | ]
991 | },
992 | {
993 | "cell_type": "code",
994 | "execution_count": 39,
995 | "metadata": {},
996 | "outputs": [],
997 | "source": [
998 | "def F(x):\n",
999 | " u, v, w = x\n",
1000 | " f1 = (u-1)**2 + v**2 + (w-1)**2 - 8\n",
1001 | " f2 = u**2 + (v-2)**2 + (w-2)**2 - 2\n",
1002 | " f3 = u**2 + (v-3)**2 + (w-3)**2 - 2\n",
1003 | " \n",
1004 | " return np.array([f1, f2, f3])"
1005 | ]
1006 | },
1007 | {
1008 | "cell_type": "code",
1009 | "execution_count": 40,
1010 | "metadata": {
1011 | "scrolled": false
1012 | },
1013 | "outputs": [
1014 | {
1015 | "name": "stdout",
1016 | "output_type": "stream",
1017 | "text": [
1018 | "(u, v, w): (1.0000001505928608, 2.0000001505928617, 2.9999998494071383)\n",
1019 | "(u, v, w): (0.9999993720401117, 1.9999993720401112, 3.0000006279598894)\n",
1020 | "(u, v, w): (0.9999998147449323, 1.9999998147449338, 3.0000001852550664)\n",
1021 | "(u, v, w): (1.000000193609215, 2.0000001936092144, 2.9999998063907856)\n",
1022 | "(u, v, w): (1.0000072034822085, 2.0000072034822, 2.9999927965178057)\n",
1023 | "(u, v, w): (1.0000000226234689, 2.000000022623469, 2.999999977376531)\n",
1024 | "(u, v, w): (0.9999997633438066, 1.9999997633438076, 3.0000002366561924)\n",
1025 | "(u, v, w): (1.0000000160979983, 2.0000000160979985, 2.9999999839020015)\n"
1026 | ]
1027 | }
1028 | ],
1029 | "source": [
1030 | "starting_points = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), \n",
1031 | " (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]\n",
1032 | "\n",
1033 | "for x_0 in starting_points:\n",
1034 | " B = np.identity(3)\n",
1035 | " x = Broyden_Method_II(F, x_0, B, 55)\n",
1036 | " print(\"(u, v, w):\", tuple(x))"
1037 | ]
1038 | }
1039 | ],
1040 | "metadata": {
1041 | "kernelspec": {
1042 | "display_name": "Python 3",
1043 | "language": "python",
1044 | "name": "python3"
1045 | },
1046 | "language_info": {
1047 | "codemirror_mode": {
1048 | "name": "ipython",
1049 | "version": 3
1050 | },
1051 | "file_extension": ".py",
1052 | "mimetype": "text/x-python",
1053 | "name": "python",
1054 | "nbconvert_exporter": "python",
1055 | "pygments_lexer": "ipython3",
1056 | "version": "3.6.4"
1057 | }
1058 | },
1059 | "nbformat": 4,
1060 | "nbformat_minor": 2
1061 | }
1062 |
--------------------------------------------------------------------------------
/Ch_04/Ch_4_3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Chapter 4
Least Squares"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## 4.3 QR Factorization"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 2,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "def norm(x):\n",
33 | " return np.sqrt(sum(x**2))"
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "### Q. 1"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 3,
46 | "metadata": {},
47 | "outputs": [],
48 | "source": [
49 | "def QR_classical(A, reduced=False):\n",
50 | " m, n = A.shape\n",
51 | "\n",
52 | " if not reduced:\n",
53 | " Q = np.zeros((m, m))\n",
54 | " R = np.zeros((m, n))\n",
55 | " while True:\n",
56 | " A_new = np.concatenate((A, np.random.randn(m, m-n)), axis=1)\n",
57 | " if np.linalg.det(A_new) != 0:\n",
58 | " A = A_new\n",
59 | " k = m\n",
60 | " break\n",
61 | " else:\n",
62 | " Q = np.zeros((m, n))\n",
63 | " R = np.zeros((n, n))\n",
64 | " k = n\n",
65 | "\n",
66 | " r = norm(A[:, 0])\n",
67 | " Q[:, 0] = A[:, 0] / r\n",
68 | " R[0, 0] = r\n",
69 | "\n",
70 | " for i in range(1, k):\n",
71 | " y = A[:, i]\n",
72 | " for j in range(i):\n",
73 | " r = np.dot(Q[:, j], A[:, i])\n",
74 | " y = y - r*Q[:, j]\n",
75 | " try:\n",
76 | " R[j, i] = r\n",
77 | " except:\n",
78 | " pass\n",
79 | " r = norm(y)\n",
80 | " Q[:, i] = y / r\n",
81 | " try:\n",
82 | " R[i, i] = r\n",
83 | " except:\n",
84 | " pass\n",
85 | " \n",
86 | " return Q, R"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 4,
92 | "metadata": {},
93 | "outputs": [
94 | {
95 | "name": "stdout",
96 | "output_type": "stream",
97 | "text": [
98 | "Q:\n",
99 | " [[ 0.8 -0.6]\n",
100 | " [ 0.6 0.8]]\n",
101 | "R:\n",
102 | " [[5. 0.6]\n",
103 | " [0. 0.8]]\n"
104 | ]
105 | }
106 | ],
107 | "source": [
108 | "# (a)\n",
109 | "A = np.array([[4, 0], [3, 1]])\n",
110 | "Q, R = QR_classical(A, reduced=True)\n",
111 | "print(\"Q:\\n\", Q)\n",
112 | "print(\"R:\\n\", R)"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 5,
118 | "metadata": {},
119 | "outputs": [
120 | {
121 | "name": "stdout",
122 | "output_type": "stream",
123 | "text": [
124 | "Q:\n",
125 | " [[ 0.70710678 0.70710678]\n",
126 | " [ 0.70710678 -0.70710678]]\n",
127 | "R:\n",
128 | " [[1.41421356 2.12132034]\n",
129 | " [0. 0.70710678]]\n"
130 | ]
131 | }
132 | ],
133 | "source": [
134 | "# (b)\n",
135 | "A = np.array([[1, 2], [1, 1]])\n",
136 | "Q, R = QR_classical(A, reduced=True)\n",
137 | "print(\"Q:\\n\", Q)\n",
138 | "print(\"R:\\n\", R)"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 6,
144 | "metadata": {},
145 | "outputs": [
146 | {
147 | "name": "stdout",
148 | "output_type": "stream",
149 | "text": [
150 | "Q:\n",
151 | " [[ 0.66666667 0.23570226]\n",
152 | " [ 0.33333333 -0.94280904]\n",
153 | " [ 0.66666667 0.23570226]]\n",
154 | "R:\n",
155 | " [[3. 1. ]\n",
156 | " [0. 1.41421356]]\n"
157 | ]
158 | }
159 | ],
160 | "source": [
161 | "# (c)\n",
162 | "A = np.array([[2, 1], [1, -1], [2, 1]])\n",
163 | "Q, R = QR_classical(A, reduced=True)\n",
164 | "print(\"Q:\\n\", Q)\n",
165 | "print(\"R:\\n\", R)"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 7,
171 | "metadata": {
172 | "scrolled": true
173 | },
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "Q:\n",
180 | " [[ 0.8 0. -0.6]\n",
181 | " [ 0. 1. 0. ]\n",
182 | " [ 0.6 0. 0.8]]\n",
183 | "R:\n",
184 | " [[ 5. 10. 5.]\n",
185 | " [ 0. 2. -2.]\n",
186 | " [ 0. 0. 5.]]\n"
187 | ]
188 | }
189 | ],
190 | "source": [
191 | "# (d)\n",
192 | "A = np.array([[4, 8, 1], [0, 2, -2], [3, 6, 7]])\n",
193 | "Q, R = QR_classical(A, reduced=True)\n",
194 | "print(\"Q:\\n\", Q)\n",
195 | "print(\"R:\\n\", R)"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {},
201 | "source": [
202 | "### Q. 2"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": 8,
208 | "metadata": {},
209 | "outputs": [],
210 | "source": [
211 | "def QR_modified(A, reduced=False):\n",
212 | " m, n = A.shape\n",
213 | "\n",
214 | " if not reduced:\n",
215 | " Q = np.zeros((m, m))\n",
216 | " R = np.zeros((m, n))\n",
217 | " while True:\n",
218 | " A_new = np.concatenate((A, np.random.randn(m, m-n)), axis=1)\n",
219 | " if np.linalg.det(A_new) != 0:\n",
220 | " A = A_new\n",
221 | " k = m\n",
222 | " break\n",
223 | " else:\n",
224 | " Q = np.zeros((m, n))\n",
225 | " R = np.zeros((n, n))\n",
226 | " k = n\n",
227 | "\n",
228 | " r = norm(A[:, 0])\n",
229 | " Q[:, 0] = A[:, 0] / r\n",
230 | " R[0, 0] = r\n",
231 | "\n",
232 | " for i in range(1, k):\n",
233 | " y = A[:, i]\n",
234 | " for j in range(i):\n",
235 | " r = np.dot(Q[:, j], y)\n",
236 | " y = y - r*Q[:, j]\n",
237 | " try:\n",
238 | " R[j, i] = r\n",
239 | " except:\n",
240 | " pass\n",
241 | " r = norm(y)\n",
242 | " Q[:, i] = y / r\n",
243 | " try:\n",
244 | " R[i, i] = r\n",
245 | " except:\n",
246 | " pass\n",
247 | "\n",
248 | " return Q, R"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 9,
254 | "metadata": {},
255 | "outputs": [
256 | {
257 | "name": "stdout",
258 | "output_type": "stream",
259 | "text": [
260 | "Q:\n",
261 | " [[ 0.8 -0.6]\n",
262 | " [ 0.6 0.8]]\n",
263 | "R:\n",
264 | " [[5. 0.6]\n",
265 | " [0. 0.8]]\n"
266 | ]
267 | }
268 | ],
269 | "source": [
270 | "# (a)\n",
271 | "A = np.array([[4, 0], [3, 1]])\n",
272 | "Q, R = QR_modified(A, reduced=True)\n",
273 | "print(\"Q:\\n\", Q)\n",
274 | "print(\"R:\\n\", R)"
275 | ]
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": 10,
280 | "metadata": {},
281 | "outputs": [
282 | {
283 | "name": "stdout",
284 | "output_type": "stream",
285 | "text": [
286 | "Q:\n",
287 | " [[ 0.70710678 0.70710678]\n",
288 | " [ 0.70710678 -0.70710678]]\n",
289 | "R:\n",
290 | " [[1.41421356 2.12132034]\n",
291 | " [0. 0.70710678]]\n"
292 | ]
293 | }
294 | ],
295 | "source": [
296 | "# (b)\n",
297 | "A = np.array([[1, 2], [1, 1]])\n",
298 | "Q, R = QR_modified(A, reduced=True)\n",
299 | "print(\"Q:\\n\", Q)\n",
300 | "print(\"R:\\n\", R)"
301 | ]
302 | },
303 | {
304 | "cell_type": "code",
305 | "execution_count": 11,
306 | "metadata": {},
307 | "outputs": [
308 | {
309 | "name": "stdout",
310 | "output_type": "stream",
311 | "text": [
312 | "Q:\n",
313 | " [[ 0.66666667 0.23570226]\n",
314 | " [ 0.33333333 -0.94280904]\n",
315 | " [ 0.66666667 0.23570226]]\n",
316 | "R:\n",
317 | " [[3. 1. ]\n",
318 | " [0. 1.41421356]]\n"
319 | ]
320 | }
321 | ],
322 | "source": [
323 | "# (c)\n",
324 | "A = np.array([[2, 1], [1, -1], [2, 1]])\n",
325 | "Q, R = QR_modified(A, reduced=True)\n",
326 | "print(\"Q:\\n\", Q)\n",
327 | "print(\"R:\\n\", R)"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 12,
333 | "metadata": {
334 | "scrolled": true
335 | },
336 | "outputs": [
337 | {
338 | "name": "stdout",
339 | "output_type": "stream",
340 | "text": [
341 | "Q:\n",
342 | " [[ 0.8 0. -0.6]\n",
343 | " [ 0. 1. 0. ]\n",
344 | " [ 0.6 0. 0.8]]\n",
345 | "R:\n",
346 | " [[ 5. 10. 5.]\n",
347 | " [ 0. 2. -2.]\n",
348 | " [ 0. 0. 5.]]\n"
349 | ]
350 | }
351 | ],
352 | "source": [
353 | "# (d)\n",
354 | "A = np.array([[4, 8, 1], [0, 2, -2], [3, 6, 7]])\n",
355 | "Q, R = QR_modified(A, reduced=True)\n",
356 | "print(\"Q:\\n\", Q)\n",
357 | "print(\"R:\\n\", R)"
358 | ]
359 | },
360 | {
361 | "cell_type": "markdown",
362 | "metadata": {},
363 | "source": [
364 | "### Q. 3"
365 | ]
366 | },
367 | {
368 | "cell_type": "code",
369 | "execution_count": 13,
370 | "metadata": {},
371 | "outputs": [],
372 | "source": [
373 | "def QR_Householder(A, reduced=False):\n",
374 | " m, n = np.shape(A)\n",
375 | " H = np.tile(np.identity(m), (n, 1, 1))\n",
376 | " R = A.copy()\n",
377 | "\n",
378 | " for i in range(n):\n",
379 | " A_i = R[i:, i]\n",
380 | " w = np.zeros(m - i)\n",
381 | " w[0] = norm(A_i) * (2*(A_i[0] < 0) - 1)\n",
382 | "\n",
383 | " v = (A_i - w).reshape(-1, 1)\n",
384 | " P = np.dot(v, v.T) / norm(v)**2\n",
385 | "\n",
386 | " H[i, i:, i:] -= 2*P\n",
387 | " R = np.dot(H[i], R)\n",
388 | " Q = np.linalg.multi_dot(H)\n",
389 | " \n",
390 | " if reduced:\n",
391 | " return Q[:, :n], R[:n, :n]\n",
392 | " \n",
393 | " return Q, R"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 14,
399 | "metadata": {
400 | "scrolled": true
401 | },
402 | "outputs": [
403 | {
404 | "name": "stdout",
405 | "output_type": "stream",
406 | "text": [
407 | "Q:\n",
408 | " [[-0.8 0.6]\n",
409 | " [-0.6 -0.8]]\n",
410 | "R:\n",
411 | " [[-5. -0.6]\n",
412 | " [-0. -0.8]]\n"
413 | ]
414 | }
415 | ],
416 | "source": [
417 | "# (a)\n",
418 | "A = np.array([[4, 0], [3, 1]])\n",
419 | "Q, R = QR_Householder(A, reduced=True)\n",
420 | "print(\"Q:\\n\", Q.round(10))\n",
421 | "print(\"R:\\n\", R.round(10))"
422 | ]
423 | },
424 | {
425 | "cell_type": "code",
426 | "execution_count": 15,
427 | "metadata": {},
428 | "outputs": [
429 | {
430 | "name": "stdout",
431 | "output_type": "stream",
432 | "text": [
433 | "Q:\n",
434 | " [[-0.70710678 0.70710678]\n",
435 | " [-0.70710678 -0.70710678]]\n",
436 | "R:\n",
437 | " [[-1.41421356 -2.12132034]\n",
438 | " [-0. 0.70710678]]\n"
439 | ]
440 | }
441 | ],
442 | "source": [
443 | "# (b)\n",
444 | "A = np.array([[1, 2], [1, 1]])\n",
445 | "Q, R = QR_Householder(A, reduced=True)\n",
446 | "print(\"Q:\\n\", Q.round(10))\n",
447 | "print(\"R:\\n\", R.round(10))"
448 | ]
449 | },
450 | {
451 | "cell_type": "code",
452 | "execution_count": 16,
453 | "metadata": {},
454 | "outputs": [
455 | {
456 | "name": "stdout",
457 | "output_type": "stream",
458 | "text": [
459 | "Q:\n",
460 | " [[-0.66666667 0.23570226]\n",
461 | " [-0.33333333 -0.94280904]\n",
462 | " [-0.66666667 0.23570226]]\n",
463 | "R:\n",
464 | " [[-3. -1. ]\n",
465 | " [-0. 1.41421356]]\n"
466 | ]
467 | }
468 | ],
469 | "source": [
470 | "# (c)\n",
471 | "A = np.array([[2, 1], [1, -1], [2, 1]])\n",
472 | "Q, R = QR_Householder(A, reduced=True)\n",
473 | "print(\"Q:\\n\", Q.round(10))\n",
474 | "print(\"R:\\n\", R.round(10))"
475 | ]
476 | },
477 | {
478 | "cell_type": "code",
479 | "execution_count": 17,
480 | "metadata": {
481 | "scrolled": true
482 | },
483 | "outputs": [
484 | {
485 | "name": "stdout",
486 | "output_type": "stream",
487 | "text": [
488 | "Q:\n",
489 | " [[-0.8 0. 0.6]\n",
490 | " [ 0. -1. 0. ]\n",
491 | " [-0.6 -0. -0.8]]\n",
492 | "R:\n",
493 | " [[ -5. -10. -5.]\n",
494 | " [ -0. -2. 2.]\n",
495 | " [ -0. 0. -5.]]\n"
496 | ]
497 | }
498 | ],
499 | "source": [
500 | "# (d)\n",
501 | "A = np.array([[4, 8, 1], [0, 2, -2], [3, 6, 7]])\n",
502 | "Q, R = QR_Householder(A, reduced=True)\n",
503 | "print(\"Q:\\n\", Q.round(10))\n",
504 | "print(\"R:\\n\", R.round(10))"
505 | ]
506 | },
507 | {
508 | "cell_type": "markdown",
509 | "metadata": {},
510 | "source": [
511 | "### Q. 4"
512 | ]
513 | },
514 | {
515 | "cell_type": "code",
516 | "execution_count": 18,
517 | "metadata": {},
518 | "outputs": [],
519 | "source": [
520 | "# (a)"
521 | ]
522 | },
523 | {
524 | "cell_type": "markdown",
525 | "metadata": {},
526 | "source": [
527 | "Classcal QR Decomposition
(1)"
528 | ]
529 | },
530 | {
531 | "cell_type": "code",
532 | "execution_count": 19,
533 | "metadata": {},
534 | "outputs": [
535 | {
536 | "name": "stdout",
537 | "output_type": "stream",
538 | "text": [
539 | "Classcal method.\n",
540 | "Q:\n",
541 | " [[ 0.8 -0.6]\n",
542 | " [ 0.6 0.8]]\n",
543 | "R:\n",
544 | " [[5. 0.6]\n",
545 | " [0. 0.8]] \n",
546 | "\n",
547 | "Numpy function.\n",
548 | "Q:\n",
549 | " [[-0.8 -0.6]\n",
550 | " [-0.6 0.8]]\n",
551 | "R:\n",
552 | " [[-5. -0.6]\n",
553 | " [ 0. 0.8]]\n"
554 | ]
555 | }
556 | ],
557 | "source": [
558 | "A = np.array([[4, 0], [3, 1]])\n",
559 | "\n",
560 | "Q, R = QR_classical(A)\n",
561 | "print(\"Classcal method.\")\n",
562 | "print(\"Q:\\n\", Q)\n",
563 | "print(\"R:\\n\", R, \"\\n\")\n",
564 | "\n",
565 | "Q, R = np.linalg.qr(A, 'complete')\n",
566 | "print(\"Numpy function.\")\n",
567 | "print(\"Q:\\n\", Q)\n",
568 | "print(\"R:\\n\", R)"
569 | ]
570 | },
571 | {
572 | "cell_type": "markdown",
573 | "metadata": {},
574 | "source": [
575 | "(2)"
576 | ]
577 | },
578 | {
579 | "cell_type": "code",
580 | "execution_count": 20,
581 | "metadata": {},
582 | "outputs": [
583 | {
584 | "name": "stdout",
585 | "output_type": "stream",
586 | "text": [
587 | "Classcal method.\n",
588 | "Q:\n",
589 | " [[ 0.70710678 0.70710678]\n",
590 | " [ 0.70710678 -0.70710678]]\n",
591 | "R:\n",
592 | " [[1.41421356 2.12132034]\n",
593 | " [0. 0.70710678]] \n",
594 | "\n",
595 | "Numpy function.\n",
596 | "Q:\n",
597 | " [[-0.70710678 -0.70710678]\n",
598 | " [-0.70710678 0.70710678]]\n",
599 | "R:\n",
600 | " [[-1.41421356 -2.12132034]\n",
601 | " [ 0. -0.70710678]]\n"
602 | ]
603 | }
604 | ],
605 | "source": [
606 | "A = np.array([[1, 2], [1, 1]])\n",
607 | "\n",
608 | "Q, R = QR_classical(A)\n",
609 | "print(\"Classcal method.\")\n",
610 | "print(\"Q:\\n\", Q)\n",
611 | "print(\"R:\\n\", R, \"\\n\")\n",
612 | "\n",
613 | "Q, R = np.linalg.qr(A, 'complete')\n",
614 | "print(\"Numpy function.\")\n",
615 | "print(\"Q:\\n\", Q)\n",
616 | "print(\"R:\\n\", R)"
617 | ]
618 | },
619 | {
620 | "cell_type": "markdown",
621 | "metadata": {},
622 | "source": [
623 | "(3)"
624 | ]
625 | },
626 | {
627 | "cell_type": "code",
628 | "execution_count": 21,
629 | "metadata": {},
630 | "outputs": [
631 | {
632 | "name": "stdout",
633 | "output_type": "stream",
634 | "text": [
635 | "Classcal method.\n",
636 | "Q:\n",
637 | " [[ 6.66666667e-01 2.35702260e-01 7.07106781e-01]\n",
638 | " [ 3.33333333e-01 -9.42809042e-01 -2.10153194e-16]\n",
639 | " [ 6.66666667e-01 2.35702260e-01 -7.07106781e-01]]\n",
640 | "R:\n",
641 | " [[3. 1. ]\n",
642 | " [0. 1.41421356]\n",
643 | " [0. 0. ]] \n",
644 | "\n",
645 | "Numpy function.\n",
646 | "Q:\n",
647 | " [[-6.66666667e-01 2.35702260e-01 -7.07106781e-01]\n",
648 | " [-3.33333333e-01 -9.42809042e-01 -2.49800181e-16]\n",
649 | " [-6.66666667e-01 2.35702260e-01 7.07106781e-01]]\n",
650 | "R:\n",
651 | " [[-3. -1. ]\n",
652 | " [ 0. 1.41421356]\n",
653 | " [ 0. 0. ]]\n"
654 | ]
655 | }
656 | ],
657 | "source": [
658 | "A = np.array([[2, 1], [1, -1], [2, 1]])\n",
659 | "\n",
660 | "Q, R = QR_classical(A)\n",
661 | "print(\"Classcal method.\")\n",
662 | "print(\"Q:\\n\", Q)\n",
663 | "print(\"R:\\n\", R, \"\\n\")\n",
664 | "\n",
665 | "Q, R = np.linalg.qr(A, 'complete')\n",
666 | "print(\"Numpy function.\")\n",
667 | "print(\"Q:\\n\", Q)\n",
668 | "print(\"R:\\n\", R)"
669 | ]
670 | },
671 | {
672 | "cell_type": "markdown",
673 | "metadata": {},
674 | "source": [
675 | "(4)"
676 | ]
677 | },
678 | {
679 | "cell_type": "code",
680 | "execution_count": 22,
681 | "metadata": {
682 | "scrolled": false
683 | },
684 | "outputs": [
685 | {
686 | "name": "stdout",
687 | "output_type": "stream",
688 | "text": [
689 | "Classcal method.\n",
690 | "Q:\n",
691 | " [[ 0.8 0. -0.6]\n",
692 | " [ 0. 1. 0. ]\n",
693 | " [ 0.6 0. 0.8]]\n",
694 | "R:\n",
695 | " [[ 5. 10. 5.]\n",
696 | " [ 0. 2. -2.]\n",
697 | " [ 0. 0. 5.]] \n",
698 | "\n",
699 | "Numpy function.\n",
700 | "Q:\n",
701 | " [[-0.8 0. -0.6]\n",
702 | " [-0. 1. 0. ]\n",
703 | " [-0.6 0. 0.8]]\n",
704 | "R:\n",
705 | " [[ -5. -10. -5.]\n",
706 | " [ 0. 2. -2.]\n",
707 | " [ 0. 0. 5.]]\n"
708 | ]
709 | }
710 | ],
711 | "source": [
712 | "A = np.array([[4, 8, 1], [0, 2, -2], [3, 6, 7]])\n",
713 | "\n",
714 | "Q, R = QR_classical(A)\n",
715 | "print(\"Classcal method.\")\n",
716 | "print(\"Q:\\n\", Q)\n",
717 | "print(\"R:\\n\", R, \"\\n\")\n",
718 | "\n",
719 | "Q, R = np.linalg.qr(A, 'complete')\n",
720 | "print(\"Numpy function.\")\n",
721 | "print(\"Q:\\n\", Q)\n",
722 | "print(\"R:\\n\", R)"
723 | ]
724 | },
725 | {
726 | "cell_type": "code",
727 | "execution_count": 23,
728 | "metadata": {},
729 | "outputs": [],
730 | "source": [
731 | "# (b)"
732 | ]
733 | },
734 | {
735 | "cell_type": "markdown",
736 | "metadata": {},
737 | "source": [
738 | "Modified QR Decomposition
(1)"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": 24,
744 | "metadata": {},
745 | "outputs": [
746 | {
747 | "name": "stdout",
748 | "output_type": "stream",
749 | "text": [
750 | "Classcal method.\n",
751 | "Q:\n",
752 | " [[ 0.8 -0.6]\n",
753 | " [ 0.6 0.8]]\n",
754 | "R:\n",
755 | " [[5. 0.6]\n",
756 | " [0. 0.8]] \n",
757 | "\n",
758 | "Numpy function.\n",
759 | "Q:\n",
760 | " [[-0.8 -0.6]\n",
761 | " [-0.6 0.8]]\n",
762 | "R:\n",
763 | " [[-5. -0.6]\n",
764 | " [ 0. 0.8]]\n"
765 | ]
766 | }
767 | ],
768 | "source": [
769 | "A = np.array([[4, 0], [3, 1]])\n",
770 | "\n",
771 | "Q, R = QR_modified(A)\n",
772 | "print(\"Classcal method.\")\n",
773 | "print(\"Q:\\n\", Q)\n",
774 | "print(\"R:\\n\", R, \"\\n\")\n",
775 | "\n",
776 | "Q, R = np.linalg.qr(A, 'complete')\n",
777 | "print(\"Numpy function.\")\n",
778 | "print(\"Q:\\n\", Q)\n",
779 | "print(\"R:\\n\", R)"
780 | ]
781 | },
782 | {
783 | "cell_type": "markdown",
784 | "metadata": {},
785 | "source": [
786 | "(2)"
787 | ]
788 | },
789 | {
790 | "cell_type": "code",
791 | "execution_count": 25,
792 | "metadata": {},
793 | "outputs": [
794 | {
795 | "name": "stdout",
796 | "output_type": "stream",
797 | "text": [
798 | "Classcal method.\n",
799 | "Q:\n",
800 | " [[ 0.70710678 0.70710678]\n",
801 | " [ 0.70710678 -0.70710678]]\n",
802 | "R:\n",
803 | " [[1.41421356 2.12132034]\n",
804 | " [0. 0.70710678]] \n",
805 | "\n",
806 | "Numpy function.\n",
807 | "Q:\n",
808 | " [[-0.70710678 -0.70710678]\n",
809 | " [-0.70710678 0.70710678]]\n",
810 | "R:\n",
811 | " [[-1.41421356 -2.12132034]\n",
812 | " [ 0. -0.70710678]]\n"
813 | ]
814 | }
815 | ],
816 | "source": [
817 | "A = np.array([[1, 2], [1, 1]])\n",
818 | "\n",
819 | "Q, R = QR_modified(A)\n",
820 | "print(\"Classcal method.\")\n",
821 | "print(\"Q:\\n\", Q)\n",
822 | "print(\"R:\\n\", R, \"\\n\")\n",
823 | "\n",
824 | "Q, R = np.linalg.qr(A, 'complete')\n",
825 | "print(\"Numpy function.\")\n",
826 | "print(\"Q:\\n\", Q)\n",
827 | "print(\"R:\\n\", R)"
828 | ]
829 | },
830 | {
831 | "cell_type": "markdown",
832 | "metadata": {},
833 | "source": [
834 | "(3)"
835 | ]
836 | },
837 | {
838 | "cell_type": "code",
839 | "execution_count": 26,
840 | "metadata": {},
841 | "outputs": [
842 | {
843 | "name": "stdout",
844 | "output_type": "stream",
845 | "text": [
846 | "Classcal method.\n",
847 | "Q:\n",
848 | " [[ 6.66666667e-01 2.35702260e-01 -7.07106781e-01]\n",
849 | " [ 3.33333333e-01 -9.42809042e-01 8.50529726e-17]\n",
850 | " [ 6.66666667e-01 2.35702260e-01 7.07106781e-01]]\n",
851 | "R:\n",
852 | " [[3. 1. ]\n",
853 | " [0. 1.41421356]\n",
854 | " [0. 0. ]] \n",
855 | "\n",
856 | "Numpy function.\n",
857 | "Q:\n",
858 | " [[-6.66666667e-01 2.35702260e-01 -7.07106781e-01]\n",
859 | " [-3.33333333e-01 -9.42809042e-01 -2.49800181e-16]\n",
860 | " [-6.66666667e-01 2.35702260e-01 7.07106781e-01]]\n",
861 | "R:\n",
862 | " [[-3. -1. ]\n",
863 | " [ 0. 1.41421356]\n",
864 | " [ 0. 0. ]]\n"
865 | ]
866 | }
867 | ],
868 | "source": [
869 | "A = np.array([[2, 1], [1, -1], [2, 1]])\n",
870 | "\n",
871 | "Q, R = QR_modified(A)\n",
872 | "print(\"Classcal method.\")\n",
873 | "print(\"Q:\\n\", Q)\n",
874 | "print(\"R:\\n\", R, \"\\n\")\n",
875 | "\n",
876 | "Q, R = np.linalg.qr(A, 'complete')\n",
877 | "print(\"Numpy function.\")\n",
878 | "print(\"Q:\\n\", Q)\n",
879 | "print(\"R:\\n\", R)"
880 | ]
881 | },
882 | {
883 | "cell_type": "markdown",
884 | "metadata": {},
885 | "source": [
886 | "(4)"
887 | ]
888 | },
889 | {
890 | "cell_type": "code",
891 | "execution_count": 27,
892 | "metadata": {
893 | "scrolled": true
894 | },
895 | "outputs": [
896 | {
897 | "name": "stdout",
898 | "output_type": "stream",
899 | "text": [
900 | "Classcal method.\n",
901 | "Q:\n",
902 | " [[ 0.8 0. -0.6]\n",
903 | " [ 0. 1. 0. ]\n",
904 | " [ 0.6 0. 0.8]]\n",
905 | "R:\n",
906 | " [[ 5. 10. 5.]\n",
907 | " [ 0. 2. -2.]\n",
908 | " [ 0. 0. 5.]] \n",
909 | "\n",
910 | "Numpy function.\n",
911 | "Q:\n",
912 | " [[-0.8 0. -0.6]\n",
913 | " [-0. 1. 0. ]\n",
914 | " [-0.6 0. 0.8]]\n",
915 | "R:\n",
916 | " [[ -5. -10. -5.]\n",
917 | " [ 0. 2. -2.]\n",
918 | " [ 0. 0. 5.]]\n"
919 | ]
920 | }
921 | ],
922 | "source": [
923 | "A = np.array([[4, 8, 1], [0, 2, -2], [3, 6, 7]])\n",
924 | "\n",
925 | "Q, R = QR_modified(A)\n",
926 | "print(\"Classcal method.\")\n",
927 | "print(\"Q:\\n\", Q)\n",
928 | "print(\"R:\\n\", R, \"\\n\")\n",
929 | "\n",
930 | "Q, R = np.linalg.qr(A, 'complete')\n",
931 | "print(\"Numpy function.\")\n",
932 | "print(\"Q:\\n\", Q)\n",
933 | "print(\"R:\\n\", R)"
934 | ]
935 | },
936 | {
937 | "cell_type": "markdown",
938 | "metadata": {},
939 | "source": [
940 | "### Q. 5"
941 | ]
942 | },
943 | {
944 | "cell_type": "code",
945 | "execution_count": 28,
946 | "metadata": {},
947 | "outputs": [],
948 | "source": [
949 | "# (a)\n",
950 | "A = np.array([[1, 1], [2, 1], [1, 2], [0, 3]])\n",
951 | "b = np.array([3, 5, 5, 5])\n",
952 | "m, n = A.shape\n",
953 | "\n",
954 | "Q, R = np.linalg.qr(A, 'complete')"
955 | ]
956 | },
957 | {
958 | "cell_type": "code",
959 | "execution_count": 29,
960 | "metadata": {},
961 | "outputs": [
962 | {
963 | "name": "stdout",
964 | "output_type": "stream",
965 | "text": [
966 | "x: [1.61538462 1.66153846]\n",
967 | "RMSE: 0.151911\n"
968 | ]
969 | }
970 | ],
971 | "source": [
972 | "d = np.dot(Q.T, b)\n",
973 | "\n",
974 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
975 | "error = np.sqrt(sum(d[n:]**2) / m)\n",
976 | "\n",
977 | "print(\"x:\", x)\n",
978 | "print(\"RMSE: %f\" % error)"
979 | ]
980 | },
981 | {
982 | "cell_type": "code",
983 | "execution_count": 30,
984 | "metadata": {},
985 | "outputs": [],
986 | "source": [
987 | "# (b)\n",
988 | "A = np.array([[1, 2, 2], [2, -1, 2], [3, 1, 1], [1, 1, -1]])\n",
989 | "b = np.array([10, 5, 10, 3])\n",
990 | "m, n = A.shape\n",
991 | "\n",
992 | "Q, R = np.linalg.qr(A, 'complete')"
993 | ]
994 | },
995 | {
996 | "cell_type": "code",
997 | "execution_count": 31,
998 | "metadata": {},
999 | "outputs": [
1000 | {
1001 | "name": "stdout",
1002 | "output_type": "stream",
1003 | "text": [
1004 | "x: [2.05882353 2.37254902 1.57843137]\n",
1005 | "RMSE: 0.110702\n"
1006 | ]
1007 | }
1008 | ],
1009 | "source": [
1010 | "d = np.dot(Q.T, b)\n",
1011 | "\n",
1012 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
1013 | "error = np.sqrt(sum(d[n:]**2) / m)\n",
1014 | "\n",
1015 | "print(\"x:\", x)\n",
1016 | "print(\"RMSE: %f\" % error)"
1017 | ]
1018 | },
1019 | {
1020 | "cell_type": "markdown",
1021 | "metadata": {},
1022 | "source": [
1023 | "### Q. 6"
1024 | ]
1025 | },
1026 | {
1027 | "cell_type": "code",
1028 | "execution_count": 32,
1029 | "metadata": {},
1030 | "outputs": [],
1031 | "source": [
1032 | "# (a)\n",
1033 | "A = np.array([[3, -1, 2], [4, 1, 0], [-3, 2, 1], [1, 1, 5], [-2, 0, 3]])\n",
1034 | "b = np.array([10, 10, -5, 15, 0])\n",
1035 | "m, n = A.shape\n",
1036 | "\n",
1037 | "Q, R = np.linalg.qr(A, 'complete')"
1038 | ]
1039 | },
1040 | {
1041 | "cell_type": "code",
1042 | "execution_count": 33,
1043 | "metadata": {},
1044 | "outputs": [
1045 | {
1046 | "name": "stdout",
1047 | "output_type": "stream",
1048 | "text": [
1049 | "x: [2.5246085 0.66163311 2.09340045]\n",
1050 | "RMSE: 1.079346\n"
1051 | ]
1052 | }
1053 | ],
1054 | "source": [
1055 | "d = np.dot(Q.T, b)\n",
1056 | "\n",
1057 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
1058 | "error = np.sqrt(sum(d[n:]**2) / m)\n",
1059 | "\n",
1060 | "print(\"x:\", x)\n",
1061 | "print(\"RMSE: %f\" % error)"
1062 | ]
1063 | },
1064 | {
1065 | "cell_type": "code",
1066 | "execution_count": 34,
1067 | "metadata": {},
1068 | "outputs": [],
1069 | "source": [
1070 | "# (b)\n",
1071 | "A = np.array([[4, 2, 3, 0], [-2, 3, -1, 1], [1, 3, -4, 2], [1, 0, 1, -1], [3, 1, 3, -2]])\n",
1072 | "b = np.array([10, 0, 2, 0, 5])\n",
1073 | "m, n = A.shape\n",
1074 | "\n",
1075 | "Q, R = np.linalg.qr(A, 'complete')"
1076 | ]
1077 | },
1078 | {
1079 | "cell_type": "code",
1080 | "execution_count": 35,
1081 | "metadata": {},
1082 | "outputs": [
1083 | {
1084 | "name": "stdout",
1085 | "output_type": "stream",
1086 | "text": [
1087 | "x: [1.27389608 0.6885086 1.21244902 1.74968966]\n",
1088 | "RMSE: 0.369237\n"
1089 | ]
1090 | }
1091 | ],
1092 | "source": [
1093 | "d = np.dot(Q.T, b)\n",
1094 | "\n",
1095 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
1096 | "error = np.sqrt(sum(d[n:]**2) / m)\n",
1097 | "\n",
1098 | "print(\"x:\", x)\n",
1099 | "print(\"RMSE: %f\" % error)"
1100 | ]
1101 | },
1102 | {
1103 | "cell_type": "markdown",
1104 | "metadata": {},
1105 | "source": [
1106 | "### Q. 7"
1107 | ]
1108 | },
1109 | {
1110 | "cell_type": "code",
1111 | "execution_count": 36,
1112 | "metadata": {},
1113 | "outputs": [],
1114 | "source": [
1115 | "# (a)\n",
1116 | "n = 6\n",
1117 | "\n",
1118 | "hilbert = np.ones((10, 10))\n",
1119 | "for i in range(10):\n",
1120 | " for j in range(10):\n",
1121 | " hilbert[i, j] /= (1 + i + j)\n",
1122 | " \n",
1123 | "A = hilbert[:, :n]\n",
1124 | "c = np.ones(n)\n",
1125 | "b = np.dot(A, c)"
1126 | ]
1127 | },
1128 | {
1129 | "cell_type": "code",
1130 | "execution_count": 37,
1131 | "metadata": {},
1132 | "outputs": [
1133 | {
1134 | "name": "stdout",
1135 | "output_type": "stream",
1136 | "text": [
1137 | "x: [1. 1. 1. 1. 1. 1.]\n",
1138 | "Error: 0.0000000000\n"
1139 | ]
1140 | }
1141 | ],
1142 | "source": [
1143 | "Q, R = np.linalg.qr(A, 'complete')\n",
1144 | "d = np.dot(Q.T, b)\n",
1145 | "\n",
1146 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
1147 | "error = np.sqrt(sum((x - c)**2) / n)\n",
1148 | "\n",
1149 | "print(\"x:\", x)\n",
1150 | "print(\"Error: %.10f\" % error)"
1151 | ]
1152 | },
1153 | {
1154 | "cell_type": "code",
1155 | "execution_count": 38,
1156 | "metadata": {},
1157 | "outputs": [],
1158 | "source": [
1159 | "# (b)\n",
1160 | "n = 8\n",
1161 | "\n",
1162 | "hilbert = np.ones((10, 10))\n",
1163 | "for i in range(10):\n",
1164 | " for j in range(10):\n",
1165 | " hilbert[i, j] /= (1 + i + j)\n",
1166 | " \n",
1167 | "A = hilbert[:, :n]\n",
1168 | "c = np.ones(n)\n",
1169 | "b = np.dot(A, c)"
1170 | ]
1171 | },
1172 | {
1173 | "cell_type": "code",
1174 | "execution_count": 39,
1175 | "metadata": {},
1176 | "outputs": [
1177 | {
1178 | "name": "stdout",
1179 | "output_type": "stream",
1180 | "text": [
1181 | "x: [1. 1. 0.99999999 1.00000005 0.99999988 1.00000016\n",
1182 | " 0.99999989 1.00000003]\n",
1183 | "Error: 0.0000000832\n"
1184 | ]
1185 | }
1186 | ],
1187 | "source": [
1188 | "Q, R = np.linalg.qr(A, 'complete')\n",
1189 | "d = np.dot(Q.T, b)\n",
1190 | "\n",
1191 | "x = np.linalg.inv(R[:n]).dot(d[:n])\n",
1192 | "error = np.sqrt(sum((x - c)**2) / n)\n",
1193 | "\n",
1194 | "print(\"x:\", x)\n",
1195 | "print(\"Error: %.10f\" % error)"
1196 | ]
1197 | },
1198 | {
1199 | "cell_type": "markdown",
1200 | "metadata": {},
1201 | "source": [
1202 | "### Q . 8"
1203 | ]
1204 | },
1205 | {
1206 | "cell_type": "code",
1207 | "execution_count": 40,
1208 | "metadata": {},
1209 | "outputs": [],
1210 | "source": [
1211 | "# (a)\n",
1212 | "d = 5\n",
1213 | "\n",
1214 | "x_i = np.linspace(2, 4, 11)\n",
1215 | "A = np.ones((d+1, 11))\n",
1216 | "for i in range(1, d+1):\n",
1217 | " A[i] = x_i**i\n",
1218 | "A = A.T\n",
1219 | "b = np.sum(A, axis=1)"
1220 | ]
1221 | },
1222 | {
1223 | "cell_type": "code",
1224 | "execution_count": 41,
1225 | "metadata": {},
1226 | "outputs": [
1227 | {
1228 | "name": "stdout",
1229 | "output_type": "stream",
1230 | "text": [
1231 | "x: [1. 1. 1. 1. 1. 1.]\n",
1232 | "Error: 0.0000000003\n",
1233 | "Problem 4.1.9 Error: 0.0003735141\n"
1234 | ]
1235 | }
1236 | ],
1237 | "source": [
1238 | "Q, R = np.linalg.qr(A, 'complete')\n",
1239 | "d_i = np.dot(Q.T, b)\n",
1240 | "\n",
1241 | "x = np.linalg.inv(R[:d+1]).dot(d_i[:d+1])\n",
1242 | "error = np.sqrt(sum((x - np.ones(d+1))**2) / (d+1))\n",
1243 | "\n",
1244 | "print(\"x:\", x)\n",
1245 | "print(\"Error: %.10f\" % error)\n",
1246 | "print(\"Problem 4.1.9 Error: %.10f\" % 0.0003735141150385977)"
1247 | ]
1248 | },
1249 | {
1250 | "cell_type": "code",
1251 | "execution_count": 42,
1252 | "metadata": {},
1253 | "outputs": [],
1254 | "source": [
1255 | "# (b)\n",
1256 | "d = 6\n",
1257 | "\n",
1258 | "x_i = np.linspace(2, 4, 11)\n",
1259 | "A = np.ones((d+1, 11))\n",
1260 | "for i in range(1, d+1):\n",
1261 | " A[i] = x_i**i\n",
1262 | "A = A.T\n",
1263 | "b = np.sum(A, axis=1)"
1264 | ]
1265 | },
1266 | {
1267 | "cell_type": "code",
1268 | "execution_count": 43,
1269 | "metadata": {},
1270 | "outputs": [
1271 | {
1272 | "name": "stdout",
1273 | "output_type": "stream",
1274 | "text": [
1275 | "x: [1. 1. 1. 1. 1. 1. 1.]\n",
1276 | "Error: 0.0000000013\n",
1277 | "Problem 4.1.9 Error: 0.3977554581\n"
1278 | ]
1279 | }
1280 | ],
1281 | "source": [
1282 | "Q, R = np.linalg.qr(A, 'complete')\n",
1283 | "d_i = np.dot(Q.T, b)\n",
1284 | "\n",
1285 | "x = np.linalg.inv(R[:d+1]).dot(d_i[:d+1])\n",
1286 | "error = np.sqrt(sum((x - np.ones(d+1))**2) / (d+1))\n",
1287 | "\n",
1288 | "print(\"x:\", x)\n",
1289 | "print(\"Error: %.10f\" % error)\n",
1290 | "print(\"Problem 4.1.9 Error: %.10f\" % 0.3977554581037068)"
1291 | ]
1292 | },
1293 | {
1294 | "cell_type": "code",
1295 | "execution_count": 44,
1296 | "metadata": {},
1297 | "outputs": [],
1298 | "source": [
1299 | "# (c)\n",
1300 | "d = 8\n",
1301 | "\n",
1302 | "x_i = np.linspace(2, 4, 11)\n",
1303 | "A = np.ones((d+1, 11))\n",
1304 | "for i in range(1, d+1):\n",
1305 | " A[i] = x_i**i\n",
1306 | "A = A.T\n",
1307 | "b = np.sum(A, axis=1)"
1308 | ]
1309 | },
1310 | {
1311 | "cell_type": "code",
1312 | "execution_count": 45,
1313 | "metadata": {},
1314 | "outputs": [
1315 | {
1316 | "name": "stdout",
1317 | "output_type": "stream",
1318 | "text": [
1319 | "x: [0.9999988 1.00000368 0.99999511 1.00000367 0.9999983 1.0000005\n",
1320 | " 0.99999991 1.00000001 1. ]\n",
1321 | "Error: 0.0000024831\n",
1322 | "Problem 4.1.9 Error: 48391.9658412549\n"
1323 | ]
1324 | }
1325 | ],
1326 | "source": [
1327 | "Q, R = np.linalg.qr(A, 'complete')\n",
1328 | "d_i = np.dot(Q.T, b)\n",
1329 | "\n",
1330 | "x = np.linalg.inv(R[:d+1]).dot(d_i[:d+1])\n",
1331 | "error = np.sqrt(sum((x - np.ones(d+1))**2) / (d+1))\n",
1332 | "\n",
1333 | "print(\"x:\", x)\n",
1334 | "print(\"Error: %.10f\" % error)\n",
1335 | "print(\"Problem 4.1.9 Error: %.10f\" % 48391.96584125488)"
1336 | ]
1337 | }
1338 | ],
1339 | "metadata": {
1340 | "kernelspec": {
1341 | "display_name": "Python 3",
1342 | "language": "python",
1343 | "name": "python3"
1344 | },
1345 | "language_info": {
1346 | "codemirror_mode": {
1347 | "name": "ipython",
1348 | "version": 3
1349 | },
1350 | "file_extension": ".py",
1351 | "mimetype": "text/x-python",
1352 | "name": "python",
1353 | "nbconvert_exporter": "python",
1354 | "pygments_lexer": "ipython3",
1355 | "version": "3.6.4"
1356 | }
1357 | },
1358 | "nbformat": 4,
1359 | "nbformat_minor": 2
1360 | }
1361 |
--------------------------------------------------------------------------------
/Ch_04/scrippsm.txt:
--------------------------------------------------------------------------------
1 | 362.05
363.25
364.02
364.72
365.41
364.97
363.65
361.48
359.45
359.60
360.76
362.33
363.18
364.00
364.57
366.35
366.80
365.62
364.47
362.51
360.19
360.77
362.43
364.28
365.32
366.15
367.31
368.61
369.30
368.87
367.64
365.78
363.90
364.23
365.46
366.97
368.15
368.87
369.59
371.14
371.00
370.35
369.27
366.93
364.63
365.13
366.68
368.00
369.14
369.46
370.52
371.66
371.82
371.70
370.12
368.12
366.62
366.73
368.29
369.53
370.28
371.50
372.12
372.87
374.02
373.30
371.62
369.55
367.96
368.09
369.68
371.24
372.43
373.09
373.52
374.86
375.55
375.40
374.02
371.49
370.71
370.25
372.08
373.78
374.68
375.63
376.11
377.65
378.35
378.13
376.61
374.49
372.98
373.00
374.35
375.69
376.78
377.36
378.40
380.50
380.61
379.55
377.77
375.84
374.05
374.22
375.84
377.44
378.34
379.66
380.38
382.14
382.24
382.10
380.66
378.68
376.40
376.79
378.32
380.02
381.37
382.02
382.63
384.40
384.94
384.08
382.37
380.48
378.78
379.07
380.17
381.67
382.49
383.72
384.33
386.23
386.43
385.87
384.44
381.84
380.86
380.88
382.40
383.72
385.02
385.87
385.85
386.77
388.49
387.92
386.32
384.17
383.00
382.81
384.06
385.15
386.71
387.17
388.62
389.51
390.18
389.60
388.01
386.07
384.61
384.34
386.02
387.36
388.56
389.98
390.99
392.52
393.22
392.29
390.49
388.55
386.54
386.21
388.61
389.83
--------------------------------------------------------------------------------
/Ch_04/scrippsy.txt:
--------------------------------------------------------------------------------
1 | 320.58
321.01
322.25
322.24
322.16
324.01
325.00
325.57
327.34
328.07
328.92
330.07
332.48
333.09
333.97
334.87
336.75
338.01
339.47
341.46
342.91
344.14
345.75
347.43
348.93
350.21
351.84
354.22
355.67
357.16
359.34
359.66
360.28
361.68
363.79
365.41
366.80
369.30
371.00
371.82
374.02
375.55
378.35
380.61
382.24
384.94
386.43
388.49
390.18
393.22
--------------------------------------------------------------------------------
/Ch_04/windmill.txt:
--------------------------------------------------------------------------------
1 | 207
2 | 207
3 | 253
4 | 296
5 | 274
6 | 209
7 | 163
8 | 181
9 | 212
10 | 260
11 | 260
12 | 204
13 | 254
14 | 256
15 | 266
16 | 244
17 | 258
18 | 168
19 | 162
20 | 175
21 | 198
22 | 296
23 | 225
24 | 270
25 | 350
26 | 212
27 | 295
28 | 239
29 | 290
30 | 192
31 | 168
32 | 170
33 | 249
34 | 261
35 | 260
36 | 226
37 | 248
38 | 216
39 | 271
40 | 296
41 | 248
42 | 152
43 | 160
44 | 171
45 | 171
46 | 264
47 | 211
48 | 294
49 | 346
50 | 253
51 | 267
52 | 254
53 | 257
54 | 153
55 | 172
56 | 159
57 | 211
58 | 189
59 | 239
60 | 182
61 |
62 |
--------------------------------------------------------------------------------
/Ch_05/Ch_5_4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import numpy as np\n",
10 | "from scipy.special import erf"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 2,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "pi = np.pi\n",
20 | "exp = np.exp\n",
21 | "log = np.log\n",
22 | "sqrt = np.sqrt\n",
23 | "sin = np.sin\n",
24 | "cos = np.cos"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {},
30 | "source": [
31 | "# Chapter 5
Numerical Differentiation and Integration"
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "## 5.4 Adaptive Quadrature"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 3,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "def Adapt_quad(f, interval, tol, Method):\n",
48 | " interval_list = [interval]\n",
49 | " A, B = interval\n",
50 | " S, criterion = Method\n",
51 | " \n",
52 | " area = 0\n",
53 | " num = 1\n",
54 | " \n",
55 | " while len(interval_list) > 0:\n",
56 | " interval = interval_list.pop(0)\n",
57 | " a, b = interval\n",
58 | " c = (a + b)/2\n",
59 | " num += 1\n",
60 | "\n",
61 | " S_ab, S_ac, S_cb = S(f, [a, b]), S(f, [a, c]), S(f, [c, b])\n",
62 | " error = abs(S_ab - S_ac - S_cb)\n",
63 | "\n",
64 | " if error < criterion*tol*(b - a)/(B - A):\n",
65 | " area += S_ac + S_cb\n",
66 | " else:\n",
67 | " interval_list += [[a, c], [c, b]]\n",
68 | " \n",
69 | " return area, num"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 4,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": [
78 | "def S_trapezoid(f, interval):\n",
79 | " a, b = interval\n",
80 | " return (b - a)*(f(a) + f(b))/2\n",
81 | "crit_trapezoid = 3\n",
82 | "\n",
83 | "trapezoid = (S_trapezoid, crit_trapezoid)"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": 5,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | "def S_Simpson(f, interval):\n",
93 | " a, b = interval\n",
94 | " c = (a + b)/2\n",
95 | " return (b - a)*(f(a) + 4*f(c) + f(b))/6\n",
96 | "crit_Simpson = 10\n",
97 | "\n",
98 | "Simpson = (S_Simpson, crit_Simpson)"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 6,
104 | "metadata": {},
105 | "outputs": [],
106 | "source": [
107 | "def S_midpoint(f, interval):\n",
108 | " a, b = interval\n",
109 | " c = (a + b)/2\n",
110 | " return (b - a)*f(c)\n",
111 | "crit_midpoint = 3\n",
112 | "\n",
113 | "midpoint = (S_midpoint, crit_midpoint)"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "### Q. 1"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": 7,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "tol = 5e-9"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 8,
135 | "metadata": {},
136 | "outputs": [
137 | {
138 | "name": "stdout",
139 | "output_type": "stream",
140 | "text": [
141 | "Area: 2.00000000 / Number of subintervals: 12606\n"
142 | ]
143 | }
144 | ],
145 | "source": [
146 | "# (a)\n",
147 | "def f(x):\n",
148 | " return x / sqrt(x**2 + 9)\n",
149 | "\n",
150 | "interval = [0, 4]\n",
151 | "\n",
152 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
153 | "\n",
154 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 9,
160 | "metadata": {},
161 | "outputs": [
162 | {
163 | "name": "stdout",
164 | "output_type": "stream",
165 | "text": [
166 | "Area: 0.15342641 / Number of subintervals: 6204\n"
167 | ]
168 | }
169 | ],
170 | "source": [
171 | "# (b)\n",
172 | "def f(x):\n",
173 | " return x**3 / (x**2 + 1)\n",
174 | "\n",
175 | "interval = [0, 1]\n",
176 | "\n",
177 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
178 | "\n",
179 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 10,
185 | "metadata": {},
186 | "outputs": [
187 | {
188 | "name": "stdout",
189 | "output_type": "stream",
190 | "text": [
191 | "Area: 1.00000000 / Number of subintervals: 12424\n"
192 | ]
193 | }
194 | ],
195 | "source": [
196 | "# (c)\n",
197 | "def f(x):\n",
198 | " return x*exp(x)\n",
199 | "\n",
200 | "interval = [0, 1]\n",
201 | "\n",
202 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
203 | "\n",
204 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": 11,
210 | "metadata": {},
211 | "outputs": [
212 | {
213 | "name": "stdout",
214 | "output_type": "stream",
215 | "text": [
216 | "Area: 6.99862171 / Number of subintervals: 32768\n"
217 | ]
218 | }
219 | ],
220 | "source": [
221 | "# (d)\n",
222 | "def f(x):\n",
223 | " return x**2 * log(x)\n",
224 | "\n",
225 | "interval = [1, 3]\n",
226 | "\n",
227 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
228 | "\n",
229 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 12,
235 | "metadata": {},
236 | "outputs": [
237 | {
238 | "name": "stdout",
239 | "output_type": "stream",
240 | "text": [
241 | "Area: 5.86960440 / Number of subintervals: 73322\n"
242 | ]
243 | }
244 | ],
245 | "source": [
246 | "# (e)\n",
247 | "def f(x):\n",
248 | " return x**2 * sin(x)\n",
249 | "\n",
250 | "interval = [0, pi]\n",
251 | "\n",
252 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
253 | "\n",
254 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
255 | ]
256 | },
257 | {
258 | "cell_type": "code",
259 | "execution_count": 13,
260 | "metadata": {},
261 | "outputs": [
262 | {
263 | "name": "stdout",
264 | "output_type": "stream",
265 | "text": [
266 | "Area: 2.53564428 / Number of subintervals: 1568\n"
267 | ]
268 | }
269 | ],
270 | "source": [
271 | "# (f)\n",
272 | "def f(x):\n",
273 | " return x**3 / sqrt(x**4 - 1)\n",
274 | "\n",
275 | "interval = [2, 3]\n",
276 | "\n",
277 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
278 | "\n",
279 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
280 | ]
281 | },
282 | {
283 | "cell_type": "code",
284 | "execution_count": 14,
285 | "metadata": {},
286 | "outputs": [
287 | {
288 | "name": "stdout",
289 | "output_type": "stream",
290 | "text": [
291 | "Area: 1.31695790 / Number of subintervals: 7146\n"
292 | ]
293 | }
294 | ],
295 | "source": [
296 | "# (g)\n",
297 | "def f(x):\n",
298 | " return 1 / sqrt(x**2 + 4)\n",
299 | "\n",
300 | "interval = [0, 2*sqrt(3)]\n",
301 | "\n",
302 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
303 | "\n",
304 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
305 | ]
306 | },
307 | {
308 | "cell_type": "code",
309 | "execution_count": 15,
310 | "metadata": {
311 | "scrolled": true
312 | },
313 | "outputs": [
314 | {
315 | "name": "stdout",
316 | "output_type": "stream",
317 | "text": [
318 | "Area: 0.44068679 / Number of subintervals: 5308\n"
319 | ]
320 | }
321 | ],
322 | "source": [
323 | "# (h)\n",
324 | "def f(x):\n",
325 | " return x / sqrt(x**4 + 1)\n",
326 | "\n",
327 | "interval = [0, 1]\n",
328 | "\n",
329 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
330 | "\n",
331 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "metadata": {},
337 | "source": [
338 | "### Q. 2"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 16,
344 | "metadata": {},
345 | "outputs": [
346 | {
347 | "name": "stdout",
348 | "output_type": "stream",
349 | "text": [
350 | "Area: 2.500282 / Number of subintervals: 20\n"
351 | ]
352 | }
353 | ],
354 | "source": [
355 | "def f(x):\n",
356 | " return 1 + sin(exp(3*x))\n",
357 | "\n",
358 | "interval = [-1, 1]\n",
359 | "tol = 5e-3\n",
360 | "\n",
361 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
362 | "\n",
363 | "print(\"Area: %f / Number of subintervals: %d\" % (area, num))"
364 | ]
365 | },
366 | {
367 | "cell_type": "markdown",
368 | "metadata": {},
369 | "source": [
370 | "### Q. 3"
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": 17,
376 | "metadata": {},
377 | "outputs": [],
378 | "source": [
379 | "tol = 5e-9"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 18,
385 | "metadata": {
386 | "scrolled": true
387 | },
388 | "outputs": [
389 | {
390 | "name": "stdout",
391 | "output_type": "stream",
392 | "text": [
393 | "Area: 2.00000000 / Number of subintervals: 56\n"
394 | ]
395 | }
396 | ],
397 | "source": [
398 | "# (a)\n",
399 | "def f(x):\n",
400 | " return x / sqrt(x**2 + 9)\n",
401 | "\n",
402 | "interval = [0, 4]\n",
403 | "\n",
404 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
405 | "\n",
406 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 19,
412 | "metadata": {},
413 | "outputs": [
414 | {
415 | "name": "stdout",
416 | "output_type": "stream",
417 | "text": [
418 | "Area: 0.15342641 / Number of subintervals: 46\n"
419 | ]
420 | }
421 | ],
422 | "source": [
423 | "# (b)\n",
424 | "def f(x):\n",
425 | " return x**3 / (x**2 + 1)\n",
426 | "\n",
427 | "interval = [0, 1]\n",
428 | "\n",
429 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
430 | "\n",
431 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
432 | ]
433 | },
434 | {
435 | "cell_type": "code",
436 | "execution_count": 20,
437 | "metadata": {},
438 | "outputs": [
439 | {
440 | "name": "stdout",
441 | "output_type": "stream",
442 | "text": [
443 | "Area: 1.00000000 / Number of subintervals: 40\n"
444 | ]
445 | }
446 | ],
447 | "source": [
448 | "# (c)\n",
449 | "def f(x):\n",
450 | " return x*exp(x)\n",
451 | "\n",
452 | "interval = [0, 1]\n",
453 | "\n",
454 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
455 | "\n",
456 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 21,
462 | "metadata": {},
463 | "outputs": [
464 | {
465 | "name": "stdout",
466 | "output_type": "stream",
467 | "text": [
468 | "Area: 6.99862171 / Number of subintervals: 56\n"
469 | ]
470 | }
471 | ],
472 | "source": [
473 | "# (d)\n",
474 | "def f(x):\n",
475 | " return x**2 * log(x)\n",
476 | "\n",
477 | "interval = [1, 3]\n",
478 | "\n",
479 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
480 | "\n",
481 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
482 | ]
483 | },
484 | {
485 | "cell_type": "code",
486 | "execution_count": 22,
487 | "metadata": {},
488 | "outputs": [
489 | {
490 | "name": "stdout",
491 | "output_type": "stream",
492 | "text": [
493 | "Area: 5.86960440 / Number of subintervals: 206\n"
494 | ]
495 | }
496 | ],
497 | "source": [
498 | "# (e)\n",
499 | "def f(x):\n",
500 | " return x**2 * sin(x)\n",
501 | "\n",
502 | "interval = [0, pi]\n",
503 | "\n",
504 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
505 | "\n",
506 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
507 | ]
508 | },
509 | {
510 | "cell_type": "code",
511 | "execution_count": 23,
512 | "metadata": {},
513 | "outputs": [
514 | {
515 | "name": "stdout",
516 | "output_type": "stream",
517 | "text": [
518 | "Area: 2.53564428 / Number of subintervals: 22\n"
519 | ]
520 | }
521 | ],
522 | "source": [
523 | "# (f)\n",
524 | "def f(x):\n",
525 | " return x**3 / sqrt(x**4 - 1)\n",
526 | "\n",
527 | "interval = [2, 3]\n",
528 | "\n",
529 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
530 | "\n",
531 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": 24,
537 | "metadata": {},
538 | "outputs": [
539 | {
540 | "name": "stdout",
541 | "output_type": "stream",
542 | "text": [
543 | "Area: 1.31695790 / Number of subintervals: 54\n"
544 | ]
545 | }
546 | ],
547 | "source": [
548 | "# (g)\n",
549 | "def f(x):\n",
550 | " return 1 / sqrt(x**2 + 4)\n",
551 | "\n",
552 | "interval = [0, 2*sqrt(3)]\n",
553 | "\n",
554 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
555 | "\n",
556 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
557 | ]
558 | },
559 | {
560 | "cell_type": "code",
561 | "execution_count": 25,
562 | "metadata": {
563 | "scrolled": false
564 | },
565 | "outputs": [
566 | {
567 | "name": "stdout",
568 | "output_type": "stream",
569 | "text": [
570 | "Area: 0.44068679 / Number of subintervals: 52\n"
571 | ]
572 | }
573 | ],
574 | "source": [
575 | "# (h)\n",
576 | "def f(x):\n",
577 | " return x / sqrt(x**4 + 1)\n",
578 | "\n",
579 | "interval = [0, 1]\n",
580 | "\n",
581 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
582 | "\n",
583 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
584 | ]
585 | },
586 | {
587 | "cell_type": "markdown",
588 | "metadata": {},
589 | "source": [
590 | "### Q. 4"
591 | ]
592 | },
593 | {
594 | "cell_type": "code",
595 | "execution_count": 26,
596 | "metadata": {},
597 | "outputs": [],
598 | "source": [
599 | "tol = 5e-9"
600 | ]
601 | },
602 | {
603 | "cell_type": "code",
604 | "execution_count": 27,
605 | "metadata": {},
606 | "outputs": [
607 | {
608 | "name": "stdout",
609 | "output_type": "stream",
610 | "text": [
611 | "Area: 2.00000000 / Number of subintervals: 7856\n"
612 | ]
613 | }
614 | ],
615 | "source": [
616 | "# (a)\n",
617 | "def f(x):\n",
618 | " return x / sqrt(x**2 + 9)\n",
619 | "\n",
620 | "interval = [0, 4]\n",
621 | "\n",
622 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
623 | "\n",
624 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
625 | ]
626 | },
627 | {
628 | "cell_type": "code",
629 | "execution_count": 28,
630 | "metadata": {},
631 | "outputs": [
632 | {
633 | "name": "stdout",
634 | "output_type": "stream",
635 | "text": [
636 | "Area: 0.15342641 / Number of subintervals: 3892\n"
637 | ]
638 | }
639 | ],
640 | "source": [
641 | "# (b)\n",
642 | "def f(x):\n",
643 | " return x**3 / (x**2 + 1)\n",
644 | "\n",
645 | "interval = [0, 1]\n",
646 | "\n",
647 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
648 | "\n",
649 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
650 | ]
651 | },
652 | {
653 | "cell_type": "code",
654 | "execution_count": 29,
655 | "metadata": {},
656 | "outputs": [
657 | {
658 | "name": "stdout",
659 | "output_type": "stream",
660 | "text": [
661 | "Area: 1.00000000 / Number of subintervals: 8252\n"
662 | ]
663 | }
664 | ],
665 | "source": [
666 | "# (c)\n",
667 | "def f(x):\n",
668 | " return x*exp(x)\n",
669 | "\n",
670 | "interval = [0, 1]\n",
671 | "\n",
672 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
673 | "\n",
674 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
675 | ]
676 | },
677 | {
678 | "cell_type": "code",
679 | "execution_count": 30,
680 | "metadata": {},
681 | "outputs": [
682 | {
683 | "name": "stdout",
684 | "output_type": "stream",
685 | "text": [
686 | "Area: 6.99862171 / Number of subintervals: 27274\n"
687 | ]
688 | }
689 | ],
690 | "source": [
691 | "# (d)\n",
692 | "def f(x):\n",
693 | " return x**2 * log(x)\n",
694 | "\n",
695 | "interval = [1, 3]\n",
696 | "\n",
697 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
698 | "\n",
699 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
700 | ]
701 | },
702 | {
703 | "cell_type": "code",
704 | "execution_count": 31,
705 | "metadata": {},
706 | "outputs": [
707 | {
708 | "name": "stdout",
709 | "output_type": "stream",
710 | "text": [
711 | "Area: 5.86960440 / Number of subintervals: 43108\n"
712 | ]
713 | }
714 | ],
715 | "source": [
716 | "# (e)\n",
717 | "def f(x):\n",
718 | " return x**2 * sin(x)\n",
719 | "\n",
720 | "interval = [0, pi]\n",
721 | "\n",
722 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
723 | "\n",
724 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
725 | ]
726 | },
727 | {
728 | "cell_type": "code",
729 | "execution_count": 32,
730 | "metadata": {},
731 | "outputs": [
732 | {
733 | "name": "stdout",
734 | "output_type": "stream",
735 | "text": [
736 | "Area: 2.53564428 / Number of subintervals: 1200\n"
737 | ]
738 | }
739 | ],
740 | "source": [
741 | "# (f)\n",
742 | "def f(x):\n",
743 | " return x**3 / sqrt(x**4 - 1)\n",
744 | "\n",
745 | "interval = [2, 3]\n",
746 | "\n",
747 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
748 | "\n",
749 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
750 | ]
751 | },
752 | {
753 | "cell_type": "code",
754 | "execution_count": 33,
755 | "metadata": {},
756 | "outputs": [
757 | {
758 | "name": "stdout",
759 | "output_type": "stream",
760 | "text": [
761 | "Area: 1.31695790 / Number of subintervals: 4880\n"
762 | ]
763 | }
764 | ],
765 | "source": [
766 | "# (g)\n",
767 | "def f(x):\n",
768 | " return 1 / sqrt(x**2 + 4)\n",
769 | "\n",
770 | "interval = [0, 2*sqrt(3)]\n",
771 | "\n",
772 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
773 | "\n",
774 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
775 | ]
776 | },
777 | {
778 | "cell_type": "code",
779 | "execution_count": 34,
780 | "metadata": {
781 | "scrolled": false
782 | },
783 | "outputs": [
784 | {
785 | "name": "stdout",
786 | "output_type": "stream",
787 | "text": [
788 | "Area: 0.44068680 / Number of subintervals: 2978\n"
789 | ]
790 | }
791 | ],
792 | "source": [
793 | "# (h)\n",
794 | "def f(x):\n",
795 | " return x / sqrt(x**4 + 1)\n",
796 | "\n",
797 | "interval = [0, 1]\n",
798 | "\n",
799 | "area, num = Adapt_quad(f, interval, tol, midpoint)\n",
800 | "\n",
801 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
802 | ]
803 | },
804 | {
805 | "cell_type": "markdown",
806 | "metadata": {},
807 | "source": [
808 | "### Q. 5"
809 | ]
810 | },
811 | {
812 | "cell_type": "code",
813 | "execution_count": 35,
814 | "metadata": {},
815 | "outputs": [],
816 | "source": [
817 | "def S_Q5(f, interval):\n",
818 | " x_0, x_1, x_2, x_3, x_4 = np.linspace(*interval, 5)\n",
819 | " return (x_4 - x_0)*(2*f(x_1) - f(x_2) + 2*f(x_3))/3\n",
820 | "crit_Q5 = 10\n",
821 | "\n",
822 | "Q_5 = (S_Q5, crit_Q5)"
823 | ]
824 | },
825 | {
826 | "cell_type": "code",
827 | "execution_count": 36,
828 | "metadata": {},
829 | "outputs": [],
830 | "source": [
831 | "tol = 5e-9"
832 | ]
833 | },
834 | {
835 | "cell_type": "code",
836 | "execution_count": 37,
837 | "metadata": {},
838 | "outputs": [
839 | {
840 | "name": "stdout",
841 | "output_type": "stream",
842 | "text": [
843 | "Area: 2.00000000 / Number of subintervals: 50\n"
844 | ]
845 | }
846 | ],
847 | "source": [
848 | "# (a)\n",
849 | "def f(x):\n",
850 | " return x / sqrt(x**2 + 9)\n",
851 | "\n",
852 | "interval = [0, 4]\n",
853 | "\n",
854 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
855 | "\n",
856 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
857 | ]
858 | },
859 | {
860 | "cell_type": "code",
861 | "execution_count": 38,
862 | "metadata": {},
863 | "outputs": [
864 | {
865 | "name": "stdout",
866 | "output_type": "stream",
867 | "text": [
868 | "Area: 0.15342641 / Number of subintervals: 44\n"
869 | ]
870 | }
871 | ],
872 | "source": [
873 | "# (b)\n",
874 | "def f(x):\n",
875 | " return x**3 / (x**2 + 1)\n",
876 | "\n",
877 | "interval = [0, 1]\n",
878 | "\n",
879 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
880 | "\n",
881 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
882 | ]
883 | },
884 | {
885 | "cell_type": "code",
886 | "execution_count": 39,
887 | "metadata": {},
888 | "outputs": [
889 | {
890 | "name": "stdout",
891 | "output_type": "stream",
892 | "text": [
893 | "Area: 1.00000000 / Number of subintervals: 36\n"
894 | ]
895 | }
896 | ],
897 | "source": [
898 | "# (c)\n",
899 | "def f(x):\n",
900 | " return x*exp(x)\n",
901 | "\n",
902 | "interval = [0, 1]\n",
903 | "\n",
904 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
905 | "\n",
906 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
907 | ]
908 | },
909 | {
910 | "cell_type": "code",
911 | "execution_count": 40,
912 | "metadata": {},
913 | "outputs": [
914 | {
915 | "name": "stdout",
916 | "output_type": "stream",
917 | "text": [
918 | "Area: 6.99862171 / Number of subintervals: 54\n"
919 | ]
920 | }
921 | ],
922 | "source": [
923 | "# (d)\n",
924 | "def f(x):\n",
925 | " return x**2 * log(x)\n",
926 | "\n",
927 | "interval = [1, 3]\n",
928 | "\n",
929 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
930 | "\n",
931 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
932 | ]
933 | },
934 | {
935 | "cell_type": "code",
936 | "execution_count": 41,
937 | "metadata": {},
938 | "outputs": [
939 | {
940 | "name": "stdout",
941 | "output_type": "stream",
942 | "text": [
943 | "Area: 5.86960440 / Number of subintervals: 198\n"
944 | ]
945 | }
946 | ],
947 | "source": [
948 | "# (e)\n",
949 | "def f(x):\n",
950 | " return x**2 * sin(x)\n",
951 | "\n",
952 | "interval = [0, pi]\n",
953 | "\n",
954 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
955 | "\n",
956 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
957 | ]
958 | },
959 | {
960 | "cell_type": "code",
961 | "execution_count": 42,
962 | "metadata": {},
963 | "outputs": [
964 | {
965 | "name": "stdout",
966 | "output_type": "stream",
967 | "text": [
968 | "Area: 2.53564428 / Number of subintervals: 22\n"
969 | ]
970 | }
971 | ],
972 | "source": [
973 | "# (f)\n",
974 | "def f(x):\n",
975 | " return x**3 / sqrt(x**4 - 1)\n",
976 | "\n",
977 | "interval = [2, 3]\n",
978 | "\n",
979 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
980 | "\n",
981 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
982 | ]
983 | },
984 | {
985 | "cell_type": "code",
986 | "execution_count": 43,
987 | "metadata": {},
988 | "outputs": [
989 | {
990 | "name": "stdout",
991 | "output_type": "stream",
992 | "text": [
993 | "Area: 1.31695790 / Number of subintervals: 50\n"
994 | ]
995 | }
996 | ],
997 | "source": [
998 | "# (g)\n",
999 | "def f(x):\n",
1000 | " return 1 / sqrt(x**2 + 4)\n",
1001 | "\n",
1002 | "interval = [0, 2*sqrt(3)]\n",
1003 | "\n",
1004 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
1005 | "\n",
1006 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
1007 | ]
1008 | },
1009 | {
1010 | "cell_type": "code",
1011 | "execution_count": 44,
1012 | "metadata": {
1013 | "scrolled": false
1014 | },
1015 | "outputs": [
1016 | {
1017 | "name": "stdout",
1018 | "output_type": "stream",
1019 | "text": [
1020 | "Area: 0.44068679 / Number of subintervals: 52\n"
1021 | ]
1022 | }
1023 | ],
1024 | "source": [
1025 | "# (h)\n",
1026 | "def f(x):\n",
1027 | " return x / sqrt(x**4 + 1)\n",
1028 | "\n",
1029 | "interval = [0, 1]\n",
1030 | "\n",
1031 | "area, num = Adapt_quad(f, interval, tol, Q_5)\n",
1032 | "\n",
1033 | "print(\"Area: %.8f / Number of subintervals: %d\" % (area, num))"
1034 | ]
1035 | },
1036 | {
1037 | "cell_type": "markdown",
1038 | "metadata": {},
1039 | "source": [
1040 | "### Q. 6"
1041 | ]
1042 | },
1043 | {
1044 | "cell_type": "code",
1045 | "execution_count": 45,
1046 | "metadata": {},
1047 | "outputs": [],
1048 | "source": [
1049 | "tol = 5e-9"
1050 | ]
1051 | },
1052 | {
1053 | "cell_type": "code",
1054 | "execution_count": 46,
1055 | "metadata": {},
1056 | "outputs": [
1057 | {
1058 | "name": "stdout",
1059 | "output_type": "stream",
1060 | "text": [
1061 | "Area: 1.462652\n"
1062 | ]
1063 | }
1064 | ],
1065 | "source": [
1066 | "# (a)\n",
1067 | "def f(x):\n",
1068 | " return exp(x**2)\n",
1069 | "\n",
1070 | "interval = [0, 1]\n",
1071 | "\n",
1072 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1073 | "\n",
1074 | "print(\"Area: %f\" % area)"
1075 | ]
1076 | },
1077 | {
1078 | "cell_type": "code",
1079 | "execution_count": 47,
1080 | "metadata": {},
1081 | "outputs": [
1082 | {
1083 | "name": "stdout",
1084 | "output_type": "stream",
1085 | "text": [
1086 | "Area: 0.894831\n"
1087 | ]
1088 | }
1089 | ],
1090 | "source": [
1091 | "# (b)\n",
1092 | "def f(x):\n",
1093 | " return sin(x**2)\n",
1094 | "\n",
1095 | "interval = [0, sqrt(pi)]\n",
1096 | "\n",
1097 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1098 | "\n",
1099 | "print(\"Area: %f\" % area)"
1100 | ]
1101 | },
1102 | {
1103 | "cell_type": "code",
1104 | "execution_count": 48,
1105 | "metadata": {},
1106 | "outputs": [
1107 | {
1108 | "name": "stdout",
1109 | "output_type": "stream",
1110 | "text": [
1111 | "Area: 3.977463\n"
1112 | ]
1113 | }
1114 | ],
1115 | "source": [
1116 | "# (c)\n",
1117 | "def f(x):\n",
1118 | " return exp(cos(x))\n",
1119 | "\n",
1120 | "interval = [0, pi]\n",
1121 | "\n",
1122 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1123 | "\n",
1124 | "print(\"Area: %f\" % area)"
1125 | ]
1126 | },
1127 | {
1128 | "cell_type": "code",
1129 | "execution_count": 49,
1130 | "metadata": {},
1131 | "outputs": [
1132 | {
1133 | "name": "stdout",
1134 | "output_type": "stream",
1135 | "text": [
1136 | "Area: 0.263944\n"
1137 | ]
1138 | }
1139 | ],
1140 | "source": [
1141 | "# (d)\n",
1142 | "def f(x):\n",
1143 | " return log(x**2 + 1)\n",
1144 | "\n",
1145 | "interval = [0, 1]\n",
1146 | "\n",
1147 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1148 | "\n",
1149 | "print(\"Area: %f\" % area)"
1150 | ]
1151 | },
1152 | {
1153 | "cell_type": "code",
1154 | "execution_count": 50,
1155 | "metadata": {},
1156 | "outputs": [
1157 | {
1158 | "name": "stdout",
1159 | "output_type": "stream",
1160 | "text": [
1161 | "Area: 0.161020\n"
1162 | ]
1163 | }
1164 | ],
1165 | "source": [
1166 | "# (e)\n",
1167 | "def f(x):\n",
1168 | " return x / (2*exp(x) - exp(-x))\n",
1169 | "\n",
1170 | "interval = [0, 1]\n",
1171 | "\n",
1172 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1173 | "\n",
1174 | "print(\"Area: %f\" % area)"
1175 | ]
1176 | },
1177 | {
1178 | "cell_type": "code",
1179 | "execution_count": 51,
1180 | "metadata": {},
1181 | "outputs": [
1182 | {
1183 | "name": "stdout",
1184 | "output_type": "stream",
1185 | "text": [
1186 | "Area: -0.375940\n"
1187 | ]
1188 | }
1189 | ],
1190 | "source": [
1191 | "# (f)\n",
1192 | "def f(x):\n",
1193 | " return cos(exp(x))\n",
1194 | "\n",
1195 | "interval = [0, pi]\n",
1196 | "\n",
1197 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1198 | "\n",
1199 | "print(\"Area: %f\" % area)"
1200 | ]
1201 | },
1202 | {
1203 | "cell_type": "code",
1204 | "execution_count": 52,
1205 | "metadata": {},
1206 | "outputs": [
1207 | {
1208 | "name": "stdout",
1209 | "output_type": "stream",
1210 | "text": [
1211 | "Area: 0.783431\n"
1212 | ]
1213 | }
1214 | ],
1215 | "source": [
1216 | "# (g)\n",
1217 | "def f(x):\n",
1218 | " return x**x\n",
1219 | "\n",
1220 | "interval = [0, 1]\n",
1221 | "\n",
1222 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1223 | "\n",
1224 | "print(\"Area: %f\" % area)"
1225 | ]
1226 | },
1227 | {
1228 | "cell_type": "code",
1229 | "execution_count": 53,
1230 | "metadata": {
1231 | "scrolled": false
1232 | },
1233 | "outputs": [
1234 | {
1235 | "name": "stdout",
1236 | "output_type": "stream",
1237 | "text": [
1238 | "Area: 0.371569\n"
1239 | ]
1240 | }
1241 | ],
1242 | "source": [
1243 | "# (h)\n",
1244 | "def f(x):\n",
1245 | " return log(cos(x) + sin(x))\n",
1246 | "\n",
1247 | "interval = [0, pi/2]\n",
1248 | "\n",
1249 | "area, num = Adapt_quad(f, interval, tol, trapezoid)\n",
1250 | "\n",
1251 | "print(\"Area: %f\" % area)"
1252 | ]
1253 | },
1254 | {
1255 | "cell_type": "markdown",
1256 | "metadata": {},
1257 | "source": [
1258 | "### Q. 7"
1259 | ]
1260 | },
1261 | {
1262 | "cell_type": "code",
1263 | "execution_count": 54,
1264 | "metadata": {},
1265 | "outputs": [],
1266 | "source": [
1267 | "tol = 5e-9"
1268 | ]
1269 | },
1270 | {
1271 | "cell_type": "code",
1272 | "execution_count": 55,
1273 | "metadata": {},
1274 | "outputs": [
1275 | {
1276 | "name": "stdout",
1277 | "output_type": "stream",
1278 | "text": [
1279 | "Area: 1.462652\n"
1280 | ]
1281 | }
1282 | ],
1283 | "source": [
1284 | "# (a)\n",
1285 | "def f(x):\n",
1286 | " return exp(x**2)\n",
1287 | "\n",
1288 | "interval = [0, 1]\n",
1289 | "\n",
1290 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1291 | "\n",
1292 | "print(\"Area: %f\" % area)"
1293 | ]
1294 | },
1295 | {
1296 | "cell_type": "code",
1297 | "execution_count": 56,
1298 | "metadata": {},
1299 | "outputs": [
1300 | {
1301 | "name": "stdout",
1302 | "output_type": "stream",
1303 | "text": [
1304 | "Area: 0.894831\n"
1305 | ]
1306 | }
1307 | ],
1308 | "source": [
1309 | "# (b)\n",
1310 | "def f(x):\n",
1311 | " return sin(x**2)\n",
1312 | "\n",
1313 | "interval = [0, sqrt(pi)]\n",
1314 | "\n",
1315 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1316 | "\n",
1317 | "print(\"Area: %f\" % area)"
1318 | ]
1319 | },
1320 | {
1321 | "cell_type": "code",
1322 | "execution_count": 57,
1323 | "metadata": {},
1324 | "outputs": [
1325 | {
1326 | "name": "stdout",
1327 | "output_type": "stream",
1328 | "text": [
1329 | "Area: 3.977463\n"
1330 | ]
1331 | }
1332 | ],
1333 | "source": [
1334 | "# (c)\n",
1335 | "def f(x):\n",
1336 | " return exp(cos(x))\n",
1337 | "\n",
1338 | "interval = [0, pi]\n",
1339 | "\n",
1340 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1341 | "\n",
1342 | "print(\"Area: %f\" % area)"
1343 | ]
1344 | },
1345 | {
1346 | "cell_type": "code",
1347 | "execution_count": 58,
1348 | "metadata": {},
1349 | "outputs": [
1350 | {
1351 | "name": "stdout",
1352 | "output_type": "stream",
1353 | "text": [
1354 | "Area: 0.263944\n"
1355 | ]
1356 | }
1357 | ],
1358 | "source": [
1359 | "# (d)\n",
1360 | "def f(x):\n",
1361 | " return log(x**2 + 1)\n",
1362 | "\n",
1363 | "interval = [0, 1]\n",
1364 | "\n",
1365 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1366 | "\n",
1367 | "print(\"Area: %f\" % area)"
1368 | ]
1369 | },
1370 | {
1371 | "cell_type": "code",
1372 | "execution_count": 59,
1373 | "metadata": {},
1374 | "outputs": [
1375 | {
1376 | "name": "stdout",
1377 | "output_type": "stream",
1378 | "text": [
1379 | "Area: 0.161020\n"
1380 | ]
1381 | }
1382 | ],
1383 | "source": [
1384 | "# (e)\n",
1385 | "def f(x):\n",
1386 | " return x / (2*exp(x) - exp(-x))\n",
1387 | "\n",
1388 | "interval = [0, 1]\n",
1389 | "\n",
1390 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1391 | "\n",
1392 | "print(\"Area: %f\" % area)"
1393 | ]
1394 | },
1395 | {
1396 | "cell_type": "code",
1397 | "execution_count": 60,
1398 | "metadata": {},
1399 | "outputs": [
1400 | {
1401 | "name": "stdout",
1402 | "output_type": "stream",
1403 | "text": [
1404 | "Area: -0.375940\n"
1405 | ]
1406 | }
1407 | ],
1408 | "source": [
1409 | "# (f)\n",
1410 | "def f(x):\n",
1411 | " return cos(exp(x))\n",
1412 | "\n",
1413 | "interval = [0, pi]\n",
1414 | "\n",
1415 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1416 | "\n",
1417 | "print(\"Area: %f\" % area)"
1418 | ]
1419 | },
1420 | {
1421 | "cell_type": "code",
1422 | "execution_count": 61,
1423 | "metadata": {},
1424 | "outputs": [
1425 | {
1426 | "name": "stdout",
1427 | "output_type": "stream",
1428 | "text": [
1429 | "Area: 0.783431\n"
1430 | ]
1431 | }
1432 | ],
1433 | "source": [
1434 | "# (g)\n",
1435 | "def f(x):\n",
1436 | " return x**x\n",
1437 | "\n",
1438 | "interval = [0, 1]\n",
1439 | "\n",
1440 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1441 | "\n",
1442 | "print(\"Area: %f\" % area)"
1443 | ]
1444 | },
1445 | {
1446 | "cell_type": "code",
1447 | "execution_count": 62,
1448 | "metadata": {
1449 | "scrolled": false
1450 | },
1451 | "outputs": [
1452 | {
1453 | "name": "stdout",
1454 | "output_type": "stream",
1455 | "text": [
1456 | "Area: 0.371569\n"
1457 | ]
1458 | }
1459 | ],
1460 | "source": [
1461 | "# (h)\n",
1462 | "def f(x):\n",
1463 | " return log(cos(x) + sin(x))\n",
1464 | "\n",
1465 | "interval = [0, pi/2]\n",
1466 | "\n",
1467 | "area, num = Adapt_quad(f, interval, tol, Simpson)\n",
1468 | "\n",
1469 | "print(\"Area: %f\" % area)"
1470 | ]
1471 | },
1472 | {
1473 | "cell_type": "markdown",
1474 | "metadata": {},
1475 | "source": [
1476 | "### Q. 8"
1477 | ]
1478 | },
1479 | {
1480 | "cell_type": "code",
1481 | "execution_count": 63,
1482 | "metadata": {},
1483 | "outputs": [],
1484 | "source": [
1485 | "def f(x):\n",
1486 | " return exp(-x**2 / 2) / sqrt(2*pi)\n",
1487 | "\n",
1488 | "tol = 5e-9"
1489 | ]
1490 | },
1491 | {
1492 | "cell_type": "code",
1493 | "execution_count": 64,
1494 | "metadata": {},
1495 | "outputs": [
1496 | {
1497 | "name": "stdout",
1498 | "output_type": "stream",
1499 | "text": [
1500 | "Probability: 0.68268949\n"
1501 | ]
1502 | }
1503 | ],
1504 | "source": [
1505 | "# (a)\n",
1506 | "sigma = 1\n",
1507 | "interval = [-sigma, sigma]\n",
1508 | "\n",
1509 | "prob, num = Adapt_quad(f, interval, tol, Simpson)\n",
1510 | "\n",
1511 | "print(\"Probability: %.8f\" % prob)"
1512 | ]
1513 | },
1514 | {
1515 | "cell_type": "code",
1516 | "execution_count": 65,
1517 | "metadata": {},
1518 | "outputs": [
1519 | {
1520 | "name": "stdout",
1521 | "output_type": "stream",
1522 | "text": [
1523 | "Probability: 0.95449974\n"
1524 | ]
1525 | }
1526 | ],
1527 | "source": [
1528 | "# (b)\n",
1529 | "sigma = 2\n",
1530 | "interval = [-sigma, sigma]\n",
1531 | "\n",
1532 | "prob, num = Adapt_quad(f, interval, tol, Simpson)\n",
1533 | "\n",
1534 | "print(\"Probability: %.8f\" % prob)"
1535 | ]
1536 | },
1537 | {
1538 | "cell_type": "code",
1539 | "execution_count": 66,
1540 | "metadata": {},
1541 | "outputs": [
1542 | {
1543 | "name": "stdout",
1544 | "output_type": "stream",
1545 | "text": [
1546 | "Probability: 0.99730020\n"
1547 | ]
1548 | }
1549 | ],
1550 | "source": [
1551 | "# (c)\n",
1552 | "sigma = 3\n",
1553 | "interval = [-sigma, sigma]\n",
1554 | "\n",
1555 | "prob, num = Adapt_quad(f, interval, tol, Simpson)\n",
1556 | "\n",
1557 | "print(\"Probability: %.8f\" % prob)"
1558 | ]
1559 | },
1560 | {
1561 | "cell_type": "markdown",
1562 | "metadata": {},
1563 | "source": [
1564 | "### Q. 9"
1565 | ]
1566 | },
1567 | {
1568 | "cell_type": "code",
1569 | "execution_count": 67,
1570 | "metadata": {},
1571 | "outputs": [],
1572 | "source": [
1573 | "tol = 5e-9\n",
1574 | "\n",
1575 | "def my_erf(x):\n",
1576 | " interval = [0, x]\n",
1577 | " def f(x):\n",
1578 | " return exp(-x**2) * 2/sqrt(pi)\n",
1579 | " ans, _ = Adapt_quad(f, interval, tol, Simpson)\n",
1580 | " \n",
1581 | " return ans"
1582 | ]
1583 | },
1584 | {
1585 | "cell_type": "code",
1586 | "execution_count": 68,
1587 | "metadata": {
1588 | "scrolled": true
1589 | },
1590 | "outputs": [
1591 | {
1592 | "name": "stdout",
1593 | "output_type": "stream",
1594 | "text": [
1595 | "Adaptive Simpson result: 0.84270079 / Scipy erf result: 0.84270079\n"
1596 | ]
1597 | }
1598 | ],
1599 | "source": [
1600 | "x = 1\n",
1601 | "\n",
1602 | "print(\"Adaptive Simpson result: %.8f / Scipy erf result: %.8f\" % (my_erf(x), erf(x)))"
1603 | ]
1604 | },
1605 | {
1606 | "cell_type": "code",
1607 | "execution_count": 69,
1608 | "metadata": {
1609 | "scrolled": true
1610 | },
1611 | "outputs": [
1612 | {
1613 | "name": "stdout",
1614 | "output_type": "stream",
1615 | "text": [
1616 | "Adaptive Simpson result: 0.99997791 / Scipy erf result: 0.99997791\n"
1617 | ]
1618 | }
1619 | ],
1620 | "source": [
1621 | "x = 3\n",
1622 | "\n",
1623 | "print(\"Adaptive Simpson result: %.8f / Scipy erf result: %.8f\" % (my_erf(x), erf(x)))"
1624 | ]
1625 | }
1626 | ],
1627 | "metadata": {
1628 | "kernelspec": {
1629 | "display_name": "Python 3",
1630 | "language": "python",
1631 | "name": "python3"
1632 | },
1633 | "language_info": {
1634 | "codemirror_mode": {
1635 | "name": "ipython",
1636 | "version": 3
1637 | },
1638 | "file_extension": ".py",
1639 | "mimetype": "text/x-python",
1640 | "name": "python",
1641 | "nbconvert_exporter": "python",
1642 | "pygments_lexer": "ipython3",
1643 | "version": "3.6.4"
1644 | }
1645 | },
1646 | "nbformat": 4,
1647 | "nbformat_minor": 2
1648 | }
1649 |
--------------------------------------------------------------------------------
/Ch_06/Lorenz_equation.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import matplotlib.pyplot as plt
3 | import matplotlib.animation as animation
4 |
5 | ################################################################################################
6 |
7 | a, b = 0, 50
8 | h = 0.001
9 | init_cond = (5, 5, 5)
10 |
11 | ################################################################################################
12 |
13 | fig = plt.figure(figsize=(5, 5))
14 | ax = fig.add_subplot(111, autoscale_on=False, xlim=(-20, 20), ylim=(0, 50))
15 | ax.grid()
16 |
17 | mass, = ax.plot([], [], 'o', lw=2)
18 | time_template = 'time = %.1fs'
19 | time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
20 |
21 | ################################################################################################
22 |
23 | def RK4(f, a, b, h, y_0):
24 | n = int((b - a)/h)
25 | d = len(y_0)
26 |
27 | t = np.linspace(a, b, n+1)
28 | w = np.zeros((d, n+1))
29 | w[:, 0] = y_0
30 |
31 | for i in range(n):
32 | s_1 = f(t[i], w[:, i])
33 | s_2 = f(t[i] + h/2, w[:, i] + s_1*h/2)
34 | s_3 = f(t[i] + h/2, w[:, i] + s_2*h/2)
35 | s_4 = f(t[i] + h, w[:, i] + s_3*h)
36 |
37 | w[:, i+1] = w[:, i] + (s_1 + 2*s_2 + 2*s_3 + s_4)*h/6
38 |
39 | return t, w
40 |
41 | ################################################################################################
42 |
43 | def Lorenz(t, xyz):
44 | x, y, z = xyz
45 | s = 10
46 | r = 28
47 | b = 8/3
48 |
49 | x_ = -s*x + s*y
50 | y_ = -x*z + r*x - y
51 | z_ = x*y - b*z
52 |
53 | return np.array([x_, y_, z_])
54 |
55 | t, w = RK4(Lorenz, a, b, h, init_cond)
56 | x, y, z = w
57 |
58 | ################################################################################################
59 |
60 | def init():
61 | mass.set_data([], [])
62 | time_text.set_text('')
63 | return mass, time_text
64 |
65 | def animate(i):
66 | thisx = [x[i]]
67 | thisz = [z[i]]
68 |
69 | mass.set_data(thisx, thisz)
70 | time_text.set_text(time_template % (i*h))
71 | return mass, time_text
72 |
73 | ani = animation.FuncAnimation(fig, animate, np.arange(1, int((b - a)/h)),
74 | interval=5, blit=True, init_func=init)
75 |
76 | plt.show()
--------------------------------------------------------------------------------
/Ch_06/Pendulum.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import matplotlib.pyplot as plt
3 | import matplotlib.animation as animation
4 |
5 | ################################################################################################
6 |
7 | pi = np.pi
8 | exp = np.exp
9 | sqrt = np.sqrt
10 | sin = np.sin
11 | cos = np.cos
12 | tan = np.tan
13 |
14 | ################################################################################################
15 |
16 | a, b = 0, 100
17 | h = 0.005
18 | t = np.arange(a, b, h)
19 |
20 | ################################################################################################
21 |
22 | fig = plt.figure(figsize=(5, 5))
23 | ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
24 | ax.grid()
25 |
26 | mass, = ax.plot([], [], '-o', lw=2)
27 | time_template = 'time = %.1fs'
28 | time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
29 |
30 | ################################################################################################
31 |
32 | def Trapezoid_method(f, a, b, h, y_0):
33 | n = int((b - a)/h)
34 | d = len(y_0)
35 |
36 | t = np.linspace(a, b, n+1)
37 | w = np.zeros((d, n+1))
38 | w[:, 0] = y_0
39 |
40 | for i in range(n):
41 | w[:, i+1] = w[:, i] + (f(t[i], w[:, i]) + f(t[i] + h, w[:, i] + h*f(t[i], w[:, i])))*(h/2)
42 |
43 | return t, w
44 |
45 | ################################################################################################
46 |
47 | d = 0.1
48 |
49 | def f(t, y):
50 | y1, y2 = y
51 | g = 9.81
52 | l = 1
53 | return np.array([y2, -g/l*sin(y1) - d*y2])
54 |
55 | y_0 = (pi, 1)
56 |
57 |
58 | t, y = Trapezoid_method(f, a, b, h, y_0)
59 | x1, y1 = y
60 |
61 | ################################################################################################
62 |
63 | def init():
64 | mass.set_data([], [])
65 | time_text.set_text('')
66 | return mass, time_text
67 |
68 | def animate(i):
69 | thisx = [0, sin(x1[i])]
70 | thisy = [0, -cos(x1[i])]
71 |
72 | mass.set_data(thisx, thisy)
73 | time_text.set_text(time_template % (i*h))
74 | return mass, time_text
75 |
76 | ani = animation.FuncAnimation(fig, animate, np.arange(1, int((b - a)/h)),
77 | interval=5, blit=True, init_func=init)
78 |
79 | plt.show()
--------------------------------------------------------------------------------
/Ch_06/Three_body_problem.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import matplotlib.pyplot as plt
3 | import matplotlib.animation as animation
4 |
5 | ################################################################################################
6 |
7 | pi = np.pi
8 | exp = np.exp
9 | sqrt = np.sqrt
10 | sin = np.sin
11 | cos = np.cos
12 | tan = np.tan
13 |
14 | ################################################################################################
15 |
16 | a, b = 0, 100
17 | h = 0.005
18 | t = np.arange(a, b, h)
19 |
20 | ################################################################################################
21 |
22 | fig = plt.figure(figsize=(5, 5))
23 | ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
24 | ax.grid()
25 |
26 | mass, = ax.plot([], [], 'o', lw=2)
27 | time_template = 'time = %.1fs'
28 | time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
29 |
30 | ################################################################################################
31 |
32 | def Trapezoid_method(f, a, b, h, y_0):
33 | n = int((b - a)/h)
34 | d = len(y_0)
35 |
36 | t = np.linspace(a, b, n+1)
37 | w = np.zeros((d, n+1))
38 | w[:, 0] = y_0
39 |
40 | for i in range(n):
41 | w[:, i+1] = w[:, i] + (f(t[i], w[:, i]) + f(t[i] + h, w[:, i] + h*f(t[i], w[:, i])))*(h/2)
42 |
43 | return t, w
44 |
45 | ################################################################################################
46 |
47 | m1 = 1
48 | m2 = 1
49 | m3 = 1
50 | g = 1
51 |
52 | y_01 = (-0.970, 0.243) + (-0.466, -0.433)
53 | y_02 = (0.970, -0.243) + (-0.466, -0.433)
54 | y_03 = (0, 0) + (0.932, 0.866)
55 | y_0 = y_01 + y_02 + y_03
56 |
57 | def f(t, y):
58 | x1, y1, v1x, v1y, x2, y2, v2x, v2y , x3, y3, v3x, v3y = y
59 |
60 | P1 = (x1, y1, m1)
61 | P2 = (x2, y2, m2)
62 | P3 = (x3, y3, m3)
63 |
64 | def force(P1, P2, position):
65 | x1, y1, m1 = P1
66 | x2, y2, m2 = P2
67 | if position == 'x':
68 | return g*m2*(x2 - x1) / sqrt((x2 - x1)**2 + (y2 - y1)**2)**3
69 | elif position == 'y':
70 | return g*m2*(y2 - y1) / sqrt((x2 - x1)**2 + (y2 - y1)**2)**3
71 |
72 | f1 = [v1x, v1y, force(P1, P2, 'x') + force(P1, P3, 'x'), force(P1, P2, 'y') + force(P1, P3, 'y')]
73 | f2 = [v2x, v2y, force(P2, P3, 'x') + force(P2, P1, 'x'), force(P2, P3, 'y') + force(P2, P1, 'y')]
74 | f3 = [v3x, v3y, force(P3, P1, 'x') + force(P3, P2, 'x'), force(P3, P1, 'y') + force(P3, P2, 'y')]
75 |
76 | return np.array(f1 + f2 + f3)
77 |
78 | t, y = Trapezoid_method(f, a, b, h, y_0)
79 | x1, y1, _, _, x2, y2, _, _, x3, y3, _, _ = y
80 |
81 | ################################################################################################
82 |
83 | def init():
84 | mass.set_data([], [])
85 | time_text.set_text('')
86 | return mass, time_text
87 |
88 | def animate(i):
89 | thisx = [x1[i], x2[i], x3[i]]
90 | thisy = [y1[i], y2[i], y3[i]]
91 |
92 | mass.set_data(thisx, thisy)
93 | time_text.set_text(time_template % (i*h))
94 | return mass, time_text
95 |
96 | ani = animation.FuncAnimation(fig, animate, np.arange(1, int((b - a)/h)),
97 | interval=5, blit=True, init_func=init)
98 |
99 | plt.show()
--------------------------------------------------------------------------------
/Ch_09/Ch_9_2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import numpy as np\n",
12 | "from scipy.linalg import lu\n",
13 | "from random import random"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": 2,
19 | "metadata": {
20 | "collapsed": true
21 | },
22 | "outputs": [],
23 | "source": [
24 | "pi = np.pi\n",
25 | "arcsin = np.arcsin"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "# Chapter 9
Random Numbers and Applications"
33 | ]
34 | },
35 | {
36 | "cell_type": "markdown",
37 | "metadata": {},
38 | "source": [
39 | "## 9.2 Monte Carlo Simulation"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 3,
45 | "metadata": {
46 | "collapsed": true
47 | },
48 | "outputs": [],
49 | "source": [
50 | "def halton(p, n):\n",
51 | " num = 0\n",
52 | " i = 1\n",
53 | " while n > 0:\n",
54 | " num += (n % p)/p**i\n",
55 | " n //= p\n",
56 | " i += 1\n",
57 | " \n",
58 | " return num"
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "metadata": {},
64 | "source": [
65 | "### Q. 1"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 4,
71 | "metadata": {
72 | "collapsed": true
73 | },
74 | "outputs": [],
75 | "source": [
76 | "# (a)"
77 | ]
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "Exact area $: \\frac{1}{3}$."
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": 5,
89 | "metadata": {},
90 | "outputs": [
91 | {
92 | "name": "stdout",
93 | "output_type": "stream",
94 | "text": [
95 | "n = 10**2 / Area: 0.3328857422\n",
96 | "n = 10**3 / Area: 0.3333457489\n",
97 | "n = 10**4 / Area: 0.3333319515\n",
98 | "n = 10**5 / Area: 0.3333333147\n"
99 | ]
100 | }
101 | ],
102 | "source": [
103 | "# (b)\n",
104 | "for k in range(2, 6):\n",
105 | " n = 10**k\n",
106 | " area = 0\n",
107 | " \n",
108 | " for i in range(n):\n",
109 | " x = halton(2, i)\n",
110 | " area += -2*x**2 + 2*x\n",
111 | " area /= n\n",
112 | " \n",
113 | " print(\"n = 10**%d / Area: %.10f\" % (k, area))"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 6,
119 | "metadata": {
120 | "scrolled": true
121 | },
122 | "outputs": [
123 | {
124 | "name": "stdout",
125 | "output_type": "stream",
126 | "text": [
127 | "n = 10**2 / Area: 0.3400000000\n",
128 | "n = 10**3 / Area: 0.3330000000\n",
129 | "n = 10**4 / Area: 0.3339000000\n",
130 | "n = 10**5 / Area: 0.3333800000\n"
131 | ]
132 | }
133 | ],
134 | "source": [
135 | "# (c)\n",
136 | "for k in range(2, 6):\n",
137 | " n = 10**k\n",
138 | " area = 0\n",
139 | "\n",
140 | " for i in range(n):\n",
141 | " x = halton(2, i)\n",
142 | " y = halton(3, i)\n",
143 | "\n",
144 | " if x**2 - x + 1/2 < y and -x**2 + x + 1/2 > y:\n",
145 | " area += 1\n",
146 | " area /= n\n",
147 | " \n",
148 | " print(\"n = 10**%d / Area: %.10f\" % (k, area))"
149 | ]
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "Type 1 Monte Carlo converges slightly faster than Type 2. Moreover Type 1 begins with more accurate estimate."
156 | ]
157 | },
158 | {
159 | "cell_type": "markdown",
160 | "metadata": {},
161 | "source": [
162 | "### Q. 2"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 7,
168 | "metadata": {
169 | "collapsed": true
170 | },
171 | "outputs": [],
172 | "source": [
173 | "# (a)"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "Exact area $: \\frac{5}{12}$."
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 8,
186 | "metadata": {},
187 | "outputs": [
188 | {
189 | "name": "stdout",
190 | "output_type": "stream",
191 | "text": [
192 | "n = 10**2 / Area: 0.4160851479\n",
193 | "n = 10**3 / Area: 0.4166814329\n",
194 | "n = 10**4 / Area: 0.4166649302\n",
195 | "n = 10**5 / Area: 0.4166666428\n"
196 | ]
197 | }
198 | ],
199 | "source": [
200 | "# (b)\n",
201 | "for k in range(2, 6):\n",
202 | " n = 10**k\n",
203 | " area = 0\n",
204 | " \n",
205 | " for i in range(n):\n",
206 | " x = halton(2, i)\n",
207 | " area += 2*x - x**2 - x**3\n",
208 | " area /= n\n",
209 | " \n",
210 | " print(\"n = 10**%d / Area: %.10f\" % (k, area))"
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "execution_count": 9,
216 | "metadata": {
217 | "scrolled": false
218 | },
219 | "outputs": [
220 | {
221 | "name": "stdout",
222 | "output_type": "stream",
223 | "text": [
224 | "n = 10**2 / Area: 0.4200000000\n",
225 | "n = 10**3 / Area: 0.4160000000\n",
226 | "n = 10**4 / Area: 0.4166000000\n",
227 | "n = 10**5 / Area: 0.4163700000\n"
228 | ]
229 | }
230 | ],
231 | "source": [
232 | "# (c)\n",
233 | "for k in range(2, 6):\n",
234 | " n = 10**k\n",
235 | " area = 0\n",
236 | "\n",
237 | " for i in range(n):\n",
238 | " x = halton(2, i)\n",
239 | " y = halton(3, i)\n",
240 | "\n",
241 | " if x**3 < y and 2*x - x**2 > y:\n",
242 | " area += 1\n",
243 | " area /= n\n",
244 | " \n",
245 | " print(\"n = 10**%d / Area: %.10f\" % (k, area))"
246 | ]
247 | },
248 | {
249 | "cell_type": "markdown",
250 | "metadata": {},
251 | "source": [
252 | "Type 1 Monte Carlo converges slightly faster than Type 2. Moreover Type 1 begins with more accurate estimate."
253 | ]
254 | },
255 | {
256 | "cell_type": "markdown",
257 | "metadata": {},
258 | "source": [
259 | "### Q. 3"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": 10,
265 | "metadata": {},
266 | "outputs": [
267 | {
268 | "name": "stdout",
269 | "output_type": "stream",
270 | "text": [
271 | "n = 10**4 / Area: 0.5232000000 / Error: 0.0003987756\n",
272 | "n = 10**5 / Area: 0.5239600000 / Error: 0.0003612244\n"
273 | ]
274 | }
275 | ],
276 | "source": [
277 | "# (a)\n",
278 | "exact_area = pi/6\n",
279 | "\n",
280 | "for k in range(4, 6):\n",
281 | " n = 10**k\n",
282 | " area = 0\n",
283 | "\n",
284 | " for i in range(n):\n",
285 | " x = 2*halton(2, i) - 1\n",
286 | " y = 2*halton(3, i) - 1\n",
287 | "\n",
288 | " if 13*x**2 + 34*x*y + 25*y**2 <= 1:\n",
289 | " area += 1\n",
290 | " area *= 4/n\n",
291 | " \n",
292 | " print(\"n = 10**%d / Area: %.10f / Error: %.10f\" % (k, area, abs(area - exact_area)))"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": 11,
298 | "metadata": {},
299 | "outputs": [
300 | {
301 | "name": "stdout",
302 | "output_type": "stream",
303 | "text": [
304 | "n = 10**4 / Area: 0.1743000000 / Error: 0.0002329252\n",
305 | "n = 10**5 / Area: 0.1745500000 / Error: 0.0000170748\n"
306 | ]
307 | }
308 | ],
309 | "source": [
310 | "# (b)\n",
311 | "exact_area = pi/18\n",
312 | "\n",
313 | "for k in range(4, 6):\n",
314 | " n = 10**k\n",
315 | " area = 0\n",
316 | "\n",
317 | " for i in range(n):\n",
318 | " x = halton(2, i)\n",
319 | " y = halton(3, i)\n",
320 | "\n",
321 | " if 40*x**2 + 25*y**2 + y + 9/4 <= 52*x*y + 14*x:\n",
322 | " area += 1\n",
323 | " area /= n\n",
324 | " \n",
325 | " print(\"n = 10**%d / Area: %.10f / Error: %.10f\" % (k, area, abs(area - exact_area)))"
326 | ]
327 | },
328 | {
329 | "cell_type": "markdown",
330 | "metadata": {},
331 | "source": [
332 | "### Q. 4"
333 | ]
334 | },
335 | {
336 | "cell_type": "code",
337 | "execution_count": 12,
338 | "metadata": {},
339 | "outputs": [
340 | {
341 | "name": "stdout",
342 | "output_type": "stream",
343 | "text": [
344 | "n = 10**4 / Area: 0.1317000000 / Error: 0.0008003061\n",
345 | "n = 10**5 / Area: 0.1309300000 / Error: 0.0000303061\n"
346 | ]
347 | }
348 | ],
349 | "source": [
350 | "# (b)\n",
351 | "exact_area = pi/24\n",
352 | "\n",
353 | "for k in range(4, 6):\n",
354 | " n = 10**k\n",
355 | " area = 0\n",
356 | "\n",
357 | " for i in range(n):\n",
358 | " x = halton(2, i)\n",
359 | " y = halton(3, i)\n",
360 | " z = halton(5, i)\n",
361 | "\n",
362 | " if 2 + 4*x**2 + 4*z**2 + y**2 <= 4*x + 4*z + y:\n",
363 | " area += 1\n",
364 | " area /= n\n",
365 | " \n",
366 | " print(\"n = 10**%d / Area: %.10f / Error: %.10f\" % (k, area, abs(area - exact_area)))"
367 | ]
368 | },
369 | {
370 | "cell_type": "markdown",
371 | "metadata": {},
372 | "source": [
373 | "### Q. 5"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": 13,
379 | "metadata": {
380 | "collapsed": true
381 | },
382 | "outputs": [],
383 | "source": [
384 | "exact_area = pi**2 / 2\n",
385 | "n = 10**5"
386 | ]
387 | },
388 | {
389 | "cell_type": "code",
390 | "execution_count": 14,
391 | "metadata": {},
392 | "outputs": [
393 | {
394 | "name": "stdout",
395 | "output_type": "stream",
396 | "text": [
397 | "Monte Carlo\n",
398 | "n = 10**5 / Area: 4.9454400000 / Error: 0.0106377995\n"
399 | ]
400 | }
401 | ],
402 | "source": [
403 | "area = 0\n",
404 | "\n",
405 | "for i in range(n):\n",
406 | " x = random()\n",
407 | " y = random()\n",
408 | " z = random()\n",
409 | " w = random()\n",
410 | " \n",
411 | " if x**2 + y**2 + z**2 + w**2 <= 1:\n",
412 | " area += 16\n",
413 | "area /= n\n",
414 | "\n",
415 | "print(\"Monte Carlo\")\n",
416 | "print(\"n = 10**5 / Area: %.10f / Error: %.10f\" % (area, abs(area - exact_area)))"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 15,
422 | "metadata": {},
423 | "outputs": [
424 | {
425 | "name": "stdout",
426 | "output_type": "stream",
427 | "text": [
428 | "quasi-Monte Carlo\n",
429 | "n = 10**5 / Area: 4.9326400000 / Error: 0.0021622005\n"
430 | ]
431 | }
432 | ],
433 | "source": [
434 | "area = 0\n",
435 | "\n",
436 | "for i in range(n):\n",
437 | " x = halton(2, i)\n",
438 | " y = halton(3, i)\n",
439 | " z = halton(5, i)\n",
440 | " w = halton(7, i)\n",
441 | " \n",
442 | " if x**2 + y**2 + z**2 + w**2 <= 1:\n",
443 | " area += 16\n",
444 | "area /= n\n",
445 | "\n",
446 | "print(\"quasi-Monte Carlo\")\n",
447 | "print(\"n = 10**5 / Area: %.10f / Error: %.10f\" % (area, abs(area - exact_area)))"
448 | ]
449 | },
450 | {
451 | "cell_type": "markdown",
452 | "metadata": {},
453 | "source": [
454 | "### Q. 6"
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 16,
460 | "metadata": {
461 | "collapsed": true
462 | },
463 | "outputs": [],
464 | "source": [
465 | "# (a)"
466 | ]
467 | },
468 | {
469 | "cell_type": "markdown",
470 | "metadata": {},
471 | "source": [
472 | "W.L.O.G, assume the length of the needle is 2. Let's define $d$ be the distance between the needle's midpoint and the nearest edge, and $\\theta$ be the angle with the stripes. Note that $d$ and $\\theta$ are uniformly distributed on $[0, 1)$, and $[0, \\pi)$ respectively.
\n",
473 | "In order for the needle to straddle both colors, the angle $\\theta$ must be in the interval $(\\arcsin d, \\pi - \\arcsin d)$. Therefore, the probability is calculated as follow;
\n",
474 | "$p = \\int_{0}^{1}{\\int_{\\arcsin d}^{\\pi - \\arcsin d}{d\\theta}\\ d\\mathrm{d}}\\ /\\ \\pi = \\frac{2}{\\pi}$"
475 | ]
476 | },
477 | {
478 | "cell_type": "code",
479 | "execution_count": 17,
480 | "metadata": {},
481 | "outputs": [
482 | {
483 | "name": "stdout",
484 | "output_type": "stream",
485 | "text": [
486 | "Probability: 0.636589\n"
487 | ]
488 | }
489 | ],
490 | "source": [
491 | "# (b)\n",
492 | "prob = 0\n",
493 | "n = 10**6\n",
494 | "\n",
495 | "for i in range(n):\n",
496 | " d = halton(2, i)\n",
497 | " theta = pi*halton(3, i)\n",
498 | " \n",
499 | " if arcsin(d) <= theta and theta <= pi - arcsin(d):\n",
500 | " prob += 1\n",
501 | "prob /= n\n",
502 | "\n",
503 | "print(\"Probability: %f\" % prob)"
504 | ]
505 | },
506 | {
507 | "cell_type": "markdown",
508 | "metadata": {},
509 | "source": [
510 | "### Q. 7"
511 | ]
512 | },
513 | {
514 | "cell_type": "code",
515 | "execution_count": 18,
516 | "metadata": {},
517 | "outputs": [
518 | {
519 | "name": "stdout",
520 | "output_type": "stream",
521 | "text": [
522 | "Exact value: 0.500000\n",
523 | "Proportion: 0.499987\n"
524 | ]
525 | }
526 | ],
527 | "source": [
528 | "# (a)\n",
529 | "exact_value = 0.5\n",
530 | "\n",
531 | "proportion = 0\n",
532 | "n = 10**6\n",
533 | "for i in range(n):\n",
534 | " a = halton(2, i)\n",
535 | " b = halton(3, i)\n",
536 | " c = halton(5, i)\n",
537 | " d = halton(7, i)\n",
538 | " \n",
539 | " if a*d - b*c > 0:\n",
540 | " proportion += 1\n",
541 | "proportion /= n\n",
542 | "\n",
543 | "print(\"Exact value: %f\" % exact_value)\n",
544 | "print(\"Proportion: %f\" % proportion)"
545 | ]
546 | },
547 | {
548 | "cell_type": "code",
549 | "execution_count": 19,
550 | "metadata": {},
551 | "outputs": [
552 | {
553 | "name": "stdout",
554 | "output_type": "stream",
555 | "text": [
556 | "Exact value: 0.444444\n",
557 | "Proportion: 0.444391\n"
558 | ]
559 | }
560 | ],
561 | "source": [
562 | "# (b)\n",
563 | "exact_value = 4/9\n",
564 | "\n",
565 | "proportion = 0\n",
566 | "n = 10**6\n",
567 | "for i in range(n):\n",
568 | " a = halton(2, i)\n",
569 | " b = halton(3, i)\n",
570 | " c = halton(5, i)\n",
571 | " \n",
572 | " if a*c - b**2 > 0:\n",
573 | " proportion += 1\n",
574 | "proportion /= n\n",
575 | "\n",
576 | "print(\"Exact value: %f\" % exact_value)\n",
577 | "print(\"Proportion: %f\" % proportion)"
578 | ]
579 | },
580 | {
581 | "cell_type": "markdown",
582 | "metadata": {},
583 | "source": [
584 | "### Q. 8"
585 | ]
586 | },
587 | {
588 | "cell_type": "code",
589 | "execution_count": 20,
590 | "metadata": {},
591 | "outputs": [
592 | {
593 | "name": "stdout",
594 | "output_type": "stream",
595 | "text": [
596 | "Proportion: 0.680581\n"
597 | ]
598 | }
599 | ],
600 | "source": [
601 | "proportion = 0\n",
602 | "n = 10**6\n",
603 | "\n",
604 | "for i in range(n):\n",
605 | " a = 2*halton(2, i) - 1\n",
606 | " b = 2*halton(3, i) - 1\n",
607 | " c = 2*halton(5, i) - 1\n",
608 | " d = 2*halton(7, i) - 1\n",
609 | " \n",
610 | " if a**2 - 2*a*d + d**2 + 4*b*c >= 0:\n",
611 | " proportion += 1\n",
612 | "proportion /= n\n",
613 | "\n",
614 | "print(\"Proportion: %f\" % proportion)"
615 | ]
616 | },
617 | {
618 | "cell_type": "markdown",
619 | "metadata": {},
620 | "source": [
621 | "### Q. 9"
622 | ]
623 | },
624 | {
625 | "cell_type": "code",
626 | "execution_count": 21,
627 | "metadata": {},
628 | "outputs": [
629 | {
630 | "name": "stdout",
631 | "output_type": "stream",
632 | "text": [
633 | "Proportion: 0.041634\n"
634 | ]
635 | }
636 | ],
637 | "source": [
638 | "proportion = 0\n",
639 | "n = 10**6\n",
640 | "I_4 = np.identity(4)\n",
641 | "\n",
642 | "for i in range(n):\n",
643 | " M = np.array([[halton(2, i), halton(3, i), halton(5, i), halton(7, i)], \n",
644 | " [halton(11, i), halton(13, i), halton(17, i), halton(19, i)], \n",
645 | " [halton(23, i), halton(29, i), halton(31, i), halton(37, i)], \n",
646 | " [halton(41, i), halton(43, i), halton(47, i), halton(53, i)]])\n",
647 | " P, _, _ = lu(M)\n",
648 | "\n",
649 | " if (P == I_4).all():\n",
650 | " proportion += 1\n",
651 | "proportion /= n\n",
652 | "\n",
653 | "print(\"Proportion: %f\" % proportion)"
654 | ]
655 | }
656 | ],
657 | "metadata": {
658 | "kernelspec": {
659 | "display_name": "Python 3",
660 | "language": "python",
661 | "name": "python3"
662 | },
663 | "language_info": {
664 | "codemirror_mode": {
665 | "name": "ipython",
666 | "version": 3
667 | },
668 | "file_extension": ".py",
669 | "mimetype": "text/x-python",
670 | "name": "python",
671 | "nbconvert_exporter": "python",
672 | "pygments_lexer": "ipython3",
673 | "version": "3.6.3"
674 | }
675 | },
676 | "nbformat": 4,
677 | "nbformat_minor": 2
678 | }
679 |
--------------------------------------------------------------------------------
/Ch_09/Ch_9_3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Chapter 9
Random Numbers and Applications"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## 9.3 Discrete and Continuous Brownian Motion"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 1,
20 | "metadata": {
21 | "collapsed": true
22 | },
23 | "outputs": [],
24 | "source": [
25 | "import numpy as np\n",
26 | "from numpy import pi, arcsin, sqrt"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "### Q. 1"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 2,
39 | "metadata": {
40 | "collapsed": true
41 | },
42 | "outputs": [],
43 | "source": [
44 | "n = 10000"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 3,
50 | "metadata": {},
51 | "outputs": [
52 | {
53 | "name": "stdout",
54 | "output_type": "stream",
55 | "text": [
56 | "Correct Answer: 0.285714 / Estimate: 0.286900 / Error: 0.001186\n"
57 | ]
58 | }
59 | ],
60 | "source": [
61 | "# (a)\n",
62 | "interval = [-2, 5]\n",
63 | "b = -interval[0]\n",
64 | "a = interval[1]\n",
65 | "correct_ans = b / (a+b)\n",
66 | "count_top = 0\n",
67 | "\n",
68 | "for _ in range(n):\n",
69 | " W_t = 0\n",
70 | " while (W_t < a) & (W_t > -b):\n",
71 | " s_i = np.random.choice((-1, 1))\n",
72 | " W_t += s_i\n",
73 | " \n",
74 | " if W_t == a:\n",
75 | " count_top += 1\n",
76 | " \n",
77 | "estimate = count_top / n\n",
78 | "error = abs(estimate - correct_ans)\n",
79 | "\n",
80 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 4,
86 | "metadata": {},
87 | "outputs": [
88 | {
89 | "name": "stdout",
90 | "output_type": "stream",
91 | "text": [
92 | "Correct Answer: 0.625000 / Estimate: 0.623700 / Error: 0.001300\n"
93 | ]
94 | }
95 | ],
96 | "source": [
97 | "# (b)\n",
98 | "interval = [-5, 3]\n",
99 | "b = -interval[0]\n",
100 | "a = interval[1]\n",
101 | "correct_ans = b / (a+b)\n",
102 | "count_top = 0\n",
103 | "\n",
104 | "for _ in range(n):\n",
105 | " W_t = 0\n",
106 | " while (W_t < a) & (W_t > -b):\n",
107 | " s_i = np.random.choice((-1, 1))\n",
108 | " W_t += s_i\n",
109 | " \n",
110 | " if W_t == a:\n",
111 | " count_top += 1\n",
112 | " \n",
113 | "estimate = count_top / n\n",
114 | "error = abs(estimate - correct_ans)\n",
115 | "\n",
116 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 5,
122 | "metadata": {},
123 | "outputs": [
124 | {
125 | "name": "stdout",
126 | "output_type": "stream",
127 | "text": [
128 | "Correct Answer: 0.727273 / Estimate: 0.724700 / Error: 0.002573\n"
129 | ]
130 | }
131 | ],
132 | "source": [
133 | "# (c)\n",
134 | "interval = [-8, 3]\n",
135 | "b = -interval[0]\n",
136 | "a = interval[1]\n",
137 | "correct_ans = b / (a+b)\n",
138 | "count_top = 0\n",
139 | "\n",
140 | "for _ in range(n):\n",
141 | " W_t = 0\n",
142 | " while (W_t < a) & (W_t > -b):\n",
143 | " s_i = np.random.choice((-1, 1))\n",
144 | " W_t += s_i\n",
145 | " \n",
146 | " if W_t == a:\n",
147 | " count_top += 1\n",
148 | " \n",
149 | "estimate = count_top / n\n",
150 | "error = abs(estimate - correct_ans)\n",
151 | "\n",
152 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "### Q. 2"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 6,
165 | "metadata": {
166 | "collapsed": true
167 | },
168 | "outputs": [],
169 | "source": [
170 | "n = 10000"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": 7,
176 | "metadata": {},
177 | "outputs": [
178 | {
179 | "name": "stdout",
180 | "output_type": "stream",
181 | "text": [
182 | "Correct Answer: 10.000000 / Estimate: 9.985900 / Error: 0.014100\n"
183 | ]
184 | }
185 | ],
186 | "source": [
187 | "# (a)\n",
188 | "interval = [-2, 5]\n",
189 | "b = -interval[0]\n",
190 | "a = interval[1]\n",
191 | "correct_ans = a*b\n",
192 | "count_time = 0\n",
193 | "\n",
194 | "for _ in range(n):\n",
195 | " W_t = 0\n",
196 | " while (W_t < a) & (W_t > -b):\n",
197 | " s_i = np.random.choice((-1, 1))\n",
198 | " W_t += s_i\n",
199 | " count_time += 1\n",
200 | " \n",
201 | "estimate = count_time / n\n",
202 | "error = abs(estimate - correct_ans)\n",
203 | "\n",
204 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": 8,
210 | "metadata": {},
211 | "outputs": [
212 | {
213 | "name": "stdout",
214 | "output_type": "stream",
215 | "text": [
216 | "Correct Answer: 15.000000 / Estimate: 14.989000 / Error: 0.011000\n"
217 | ]
218 | }
219 | ],
220 | "source": [
221 | "# (b)\n",
222 | "interval = [-5, 3]\n",
223 | "b = -interval[0]\n",
224 | "a = interval[1]\n",
225 | "correct_ans = a*b\n",
226 | "count_time = 0\n",
227 | "\n",
228 | "for _ in range(n):\n",
229 | " W_t = 0\n",
230 | " while (W_t < a) & (W_t > -b):\n",
231 | " s_i = np.random.choice((-1, 1))\n",
232 | " W_t += s_i\n",
233 | " count_time += 1\n",
234 | " \n",
235 | "estimate = count_time / n\n",
236 | "error = abs(estimate - correct_ans)\n",
237 | "\n",
238 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": 9,
244 | "metadata": {},
245 | "outputs": [
246 | {
247 | "name": "stdout",
248 | "output_type": "stream",
249 | "text": [
250 | "Correct Answer: 24.000000 / Estimate: 24.313900 / Error: 0.313900\n"
251 | ]
252 | }
253 | ],
254 | "source": [
255 | "# (c)\n",
256 | "interval = [-8, 3]\n",
257 | "b = -interval[0]\n",
258 | "a = interval[1]\n",
259 | "correct_ans = a*b\n",
260 | "count_time = 0\n",
261 | "\n",
262 | "for _ in range(n):\n",
263 | " W_t = 0\n",
264 | " while (W_t < a) & (W_t > -b):\n",
265 | " s_i = np.random.choice((-1, 1))\n",
266 | " W_t += s_i\n",
267 | " count_time += 1\n",
268 | " \n",
269 | "estimate = count_time / n\n",
270 | "error = abs(estimate - correct_ans)\n",
271 | "\n",
272 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "### Q. 3"
280 | ]
281 | },
282 | {
283 | "cell_type": "code",
284 | "execution_count": 10,
285 | "metadata": {
286 | "collapsed": true
287 | },
288 | "outputs": [],
289 | "source": [
290 | "n = 10000\n",
291 | "p = 0.7\n",
292 | "q = 1 - p"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": 11,
298 | "metadata": {},
299 | "outputs": [
300 | {
301 | "name": "stdout",
302 | "output_type": "stream",
303 | "text": [
304 | "Correct Answer: 0.818500 / Estimate: 0.815900 / Error: 0.002600\n"
305 | ]
306 | }
307 | ],
308 | "source": [
309 | "# (a)\n",
310 | "interval = [-2, 5]\n",
311 | "b = -interval[0]\n",
312 | "a = interval[1]\n",
313 | "correct_ans = ((q/p)**b - 1) / ((q/p)**(a+b) - 1)\n",
314 | "count_top = 0\n",
315 | "\n",
316 | "for _ in range(n):\n",
317 | " W_t = 0\n",
318 | " while (W_t < a) & (W_t > -b):\n",
319 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
320 | " W_t += s_i\n",
321 | " \n",
322 | " if W_t == a:\n",
323 | " count_top += 1\n",
324 | " \n",
325 | "estimate = count_top / n\n",
326 | "error = abs(estimate - correct_ans)\n",
327 | "\n",
328 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
329 | ]
330 | },
331 | {
332 | "cell_type": "code",
333 | "execution_count": 12,
334 | "metadata": {},
335 | "outputs": [
336 | {
337 | "name": "stdout",
338 | "output_type": "stream",
339 | "text": [
340 | "Correct Answer: 0.986665 / Estimate: 0.989100 / Error: 0.002435\n"
341 | ]
342 | }
343 | ],
344 | "source": [
345 | "# (b)\n",
346 | "interval = [-5, 3]\n",
347 | "b = -interval[0]\n",
348 | "a = interval[1]\n",
349 | "correct_ans = ((q/p)**b - 1) / ((q/p)**(a+b) - 1)\n",
350 | "count_top = 0\n",
351 | "\n",
352 | "for _ in range(n):\n",
353 | " W_t = 0\n",
354 | " while (W_t < a) & (W_t > -b):\n",
355 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
356 | " W_t += s_i\n",
357 | " \n",
358 | " if W_t == a:\n",
359 | " count_top += 1\n",
360 | " \n",
361 | "estimate = count_top / n\n",
362 | "error = abs(estimate - correct_ans)\n",
363 | "\n",
364 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
365 | ]
366 | },
367 | {
368 | "cell_type": "code",
369 | "execution_count": 13,
370 | "metadata": {},
371 | "outputs": [
372 | {
373 | "name": "stdout",
374 | "output_type": "stream",
375 | "text": [
376 | "Correct Answer: 0.998951 / Estimate: 0.999500 / Error: 0.000549\n"
377 | ]
378 | }
379 | ],
380 | "source": [
381 | "# (c)\n",
382 | "interval = [-8, 3]\n",
383 | "b = -interval[0]\n",
384 | "a = interval[1]\n",
385 | "correct_ans = ((q/p)**b - 1) / ((q/p)**(a+b) - 1)\n",
386 | "count_top = 0\n",
387 | "\n",
388 | "for _ in range(n):\n",
389 | " W_t = 0\n",
390 | " while (W_t < a) & (W_t > -b):\n",
391 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
392 | " W_t += s_i\n",
393 | " \n",
394 | " if W_t == a:\n",
395 | " count_top += 1\n",
396 | " \n",
397 | "estimate = count_top / n\n",
398 | "error = abs(estimate - correct_ans)\n",
399 | "\n",
400 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
401 | ]
402 | },
403 | {
404 | "cell_type": "markdown",
405 | "metadata": {},
406 | "source": [
407 | "### Q. 4"
408 | ]
409 | },
410 | {
411 | "cell_type": "code",
412 | "execution_count": 14,
413 | "metadata": {
414 | "collapsed": true
415 | },
416 | "outputs": [],
417 | "source": [
418 | "n = 10000\n",
419 | "p = 0.7\n",
420 | "q = 1 - p"
421 | ]
422 | },
423 | {
424 | "cell_type": "code",
425 | "execution_count": 15,
426 | "metadata": {},
427 | "outputs": [
428 | {
429 | "name": "stdout",
430 | "output_type": "stream",
431 | "text": [
432 | "Correct Answer: 9.323752 / Estimate: 9.212600 / Error: 0.111152\n"
433 | ]
434 | }
435 | ],
436 | "source": [
437 | "# (a)\n",
438 | "interval = [-2, 5]\n",
439 | "b = -interval[0]\n",
440 | "a = interval[1]\n",
441 | "correct_ans = (b - (a+b)*(1 - (q/p)**b) / (1 - (q/p)**(a+b))) / (q-p)\n",
442 | "count_time = 0\n",
443 | "\n",
444 | "for _ in range(n):\n",
445 | " W_t = 0\n",
446 | " while (W_t < a) & (W_t > -b):\n",
447 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
448 | " W_t += s_i\n",
449 | " count_time += 1\n",
450 | " \n",
451 | "estimate = count_time / n\n",
452 | "error = abs(estimate - correct_ans)\n",
453 | "\n",
454 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 16,
460 | "metadata": {},
461 | "outputs": [
462 | {
463 | "name": "stdout",
464 | "output_type": "stream",
465 | "text": [
466 | "Correct Answer: 7.233294 / Estimate: 7.298400 / Error: 0.065106\n"
467 | ]
468 | }
469 | ],
470 | "source": [
471 | "# (b)\n",
472 | "interval = [-5, 3]\n",
473 | "b = -interval[0]\n",
474 | "a = interval[1]\n",
475 | "correct_ans = (b - (a+b)*(1 - (q/p)**b) / (1 - (q/p)**(a+b))) / (q-p)\n",
476 | "count_time = 0\n",
477 | "\n",
478 | "for _ in range(n):\n",
479 | " W_t = 0\n",
480 | " while (W_t < a) & (W_t > -b):\n",
481 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
482 | " W_t += s_i\n",
483 | " count_time += 1\n",
484 | " \n",
485 | "estimate = count_time / n\n",
486 | "error = abs(estimate - correct_ans)\n",
487 | "\n",
488 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
489 | ]
490 | },
491 | {
492 | "cell_type": "code",
493 | "execution_count": 17,
494 | "metadata": {},
495 | "outputs": [
496 | {
497 | "name": "stdout",
498 | "output_type": "stream",
499 | "text": [
500 | "Correct Answer: 7.471163 / Estimate: 7.527000 / Error: 0.055837\n"
501 | ]
502 | }
503 | ],
504 | "source": [
505 | "# (c)\n",
506 | "interval = [-8, 3]\n",
507 | "b = -interval[0]\n",
508 | "a = interval[1]\n",
509 | "correct_ans = (b - (a+b)*(1 - (q/p)**b) / (1 - (q/p)**(a+b))) / (q-p)\n",
510 | "count_time = 0\n",
511 | "\n",
512 | "for _ in range(n):\n",
513 | " W_t = 0\n",
514 | " while (W_t < a) & (W_t > -b):\n",
515 | " s_i = np.random.choice((-1, 1), p=(q, p))\n",
516 | " W_t += s_i\n",
517 | " count_time += 1\n",
518 | " \n",
519 | "estimate = count_time / n\n",
520 | "error = abs(estimate - correct_ans)\n",
521 | "\n",
522 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
523 | ]
524 | },
525 | {
526 | "cell_type": "markdown",
527 | "metadata": {},
528 | "source": [
529 | "### Q. 5"
530 | ]
531 | },
532 | {
533 | "cell_type": "code",
534 | "execution_count": 18,
535 | "metadata": {
536 | "collapsed": true
537 | },
538 | "outputs": [],
539 | "source": [
540 | "n = 1000\n",
541 | "dt = 0.01\n",
542 | "step_height = 0.1"
543 | ]
544 | },
545 | {
546 | "cell_type": "code",
547 | "execution_count": 19,
548 | "metadata": {},
549 | "outputs": [
550 | {
551 | "name": "stdout",
552 | "output_type": "stream",
553 | "text": [
554 | "Correct Answer: 0.285714 / Estimate: 0.292000 / Error: 0.006286\n"
555 | ]
556 | }
557 | ],
558 | "source": [
559 | "# (a)\n",
560 | "interval = [-2, 5]\n",
561 | "b = -interval[0]\n",
562 | "a = interval[1]\n",
563 | "correct_ans = b / (a + b)\n",
564 | "count_top = 0\n",
565 | "\n",
566 | "for _ in range(n):\n",
567 | " W_t = 0\n",
568 | " while (W_t < a) & (W_t > -b):\n",
569 | " s_i = step_height * np.random.randn()\n",
570 | " W_t += s_i\n",
571 | " \n",
572 | " if W_t >= a:\n",
573 | " count_top += 1\n",
574 | " \n",
575 | "estimate = count_top / n\n",
576 | "error = abs(estimate - correct_ans)\n",
577 | "\n",
578 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
579 | ]
580 | },
581 | {
582 | "cell_type": "code",
583 | "execution_count": 20,
584 | "metadata": {},
585 | "outputs": [
586 | {
587 | "name": "stdout",
588 | "output_type": "stream",
589 | "text": [
590 | "Correct Answer: 0.388985 / Estimate: 0.430000 / Error: 0.041015\n"
591 | ]
592 | }
593 | ],
594 | "source": [
595 | "# (b)\n",
596 | "interval = [-2, pi]\n",
597 | "b = -interval[0]\n",
598 | "a = interval[1]\n",
599 | "correct_ans = b / (a + b)\n",
600 | "count_top = 0\n",
601 | "\n",
602 | "for _ in range(n):\n",
603 | " W_t = 0\n",
604 | " while (W_t < a) & (W_t > -b):\n",
605 | " s_i = step_height * np.random.randn()\n",
606 | " W_t += s_i\n",
607 | " \n",
608 | " if W_t >= a:\n",
609 | " count_top += 1\n",
610 | " \n",
611 | "estimate = count_top / n\n",
612 | "error = abs(estimate - correct_ans)\n",
613 | "\n",
614 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "execution_count": 21,
620 | "metadata": {},
621 | "outputs": [
622 | {
623 | "name": "stdout",
624 | "output_type": "stream",
625 | "text": [
626 | "Correct Answer: 0.470588 / Estimate: 0.472000 / Error: 0.001412\n"
627 | ]
628 | }
629 | ],
630 | "source": [
631 | "# (c)\n",
632 | "interval = [-8/3, 3]\n",
633 | "b = -interval[0]\n",
634 | "a = interval[1]\n",
635 | "correct_ans = b / (a + b)\n",
636 | "count_top = 0\n",
637 | "\n",
638 | "for _ in range(n):\n",
639 | " W_t = 0\n",
640 | " while (W_t < a) & (W_t > -b):\n",
641 | " s_i = step_height * np.random.randn()\n",
642 | " W_t += s_i\n",
643 | " \n",
644 | " if W_t >= a:\n",
645 | " count_top += 1\n",
646 | " \n",
647 | "estimate = count_top / n\n",
648 | "error = abs(estimate - correct_ans)\n",
649 | "\n",
650 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
651 | ]
652 | },
653 | {
654 | "cell_type": "markdown",
655 | "metadata": {},
656 | "source": [
657 | "### Q. 6"
658 | ]
659 | },
660 | {
661 | "cell_type": "code",
662 | "execution_count": 22,
663 | "metadata": {
664 | "collapsed": true
665 | },
666 | "outputs": [],
667 | "source": [
668 | "n = 1000\n",
669 | "dt = 0.01\n",
670 | "step_height = 0.1"
671 | ]
672 | },
673 | {
674 | "cell_type": "code",
675 | "execution_count": 23,
676 | "metadata": {},
677 | "outputs": [
678 | {
679 | "name": "stdout",
680 | "output_type": "stream",
681 | "text": [
682 | "Correct Answer: 10.000000 / Estimate: 10.634640 / Error: 0.634640\n"
683 | ]
684 | }
685 | ],
686 | "source": [
687 | "# (a)\n",
688 | "interval = [-2, 5]\n",
689 | "b = -interval[0]\n",
690 | "a = interval[1]\n",
691 | "correct_ans = a*b\n",
692 | "count_time = 0\n",
693 | "\n",
694 | "for _ in range(n):\n",
695 | " W_t = 0\n",
696 | " while (W_t < a) & (W_t > -b):\n",
697 | " s_i = step_height * np.random.randn()\n",
698 | " W_t += s_i\n",
699 | " count_time += dt\n",
700 | " \n",
701 | "estimate = count_time / n\n",
702 | "error = abs(estimate - correct_ans)\n",
703 | "\n",
704 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
705 | ]
706 | },
707 | {
708 | "cell_type": "code",
709 | "execution_count": 24,
710 | "metadata": {},
711 | "outputs": [
712 | {
713 | "name": "stdout",
714 | "output_type": "stream",
715 | "text": [
716 | "Correct Answer: 6.283185 / Estimate: 6.717150 / Error: 0.433965\n"
717 | ]
718 | }
719 | ],
720 | "source": [
721 | "# (b)\n",
722 | "interval = [-2, pi]\n",
723 | "b = -interval[0]\n",
724 | "a = interval[1]\n",
725 | "correct_ans = a*b\n",
726 | "count_time = 0\n",
727 | "\n",
728 | "for _ in range(n):\n",
729 | " W_t = 0\n",
730 | " while (W_t < a) & (W_t > -b):\n",
731 | " s_i = step_height * np.random.randn()\n",
732 | " W_t += s_i\n",
733 | " count_time += dt\n",
734 | " \n",
735 | "estimate = count_time / n\n",
736 | "error = abs(estimate - correct_ans)\n",
737 | "\n",
738 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": 25,
744 | "metadata": {},
745 | "outputs": [
746 | {
747 | "name": "stdout",
748 | "output_type": "stream",
749 | "text": [
750 | "Correct Answer: 8.000000 / Estimate: 8.508450 / Error: 0.508450\n"
751 | ]
752 | }
753 | ],
754 | "source": [
755 | "# (c)\n",
756 | "interval = [-8/3, 3]\n",
757 | "b = -interval[0]\n",
758 | "a = interval[1]\n",
759 | "correct_ans = a*b\n",
760 | "count_time = 0\n",
761 | "\n",
762 | "for _ in range(n):\n",
763 | " W_t = 0\n",
764 | " while (W_t < a) & (W_t > -b):\n",
765 | " s_i = step_height * np.random.randn()\n",
766 | " W_t += s_i\n",
767 | " count_time += dt\n",
768 | " \n",
769 | "estimate = count_time / n\n",
770 | "error = abs(estimate - correct_ans)\n",
771 | "\n",
772 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
773 | ]
774 | },
775 | {
776 | "cell_type": "markdown",
777 | "metadata": {},
778 | "source": [
779 | "### Q. 7"
780 | ]
781 | },
782 | {
783 | "cell_type": "code",
784 | "execution_count": 26,
785 | "metadata": {
786 | "collapsed": true
787 | },
788 | "outputs": [],
789 | "source": [
790 | "n = 10000\n",
791 | "dt = 0.01\n",
792 | "step_height = 0.1"
793 | ]
794 | },
795 | {
796 | "cell_type": "code",
797 | "execution_count": 27,
798 | "metadata": {},
799 | "outputs": [
800 | {
801 | "name": "stdout",
802 | "output_type": "stream",
803 | "text": [
804 | "Correct Answer: 0.564094 / Estimate: 0.588300 / Error: 0.024206\n"
805 | ]
806 | }
807 | ],
808 | "source": [
809 | "# (a)\n",
810 | "time_interval = [3, 5]\n",
811 | "t1, t2 = time_interval\n",
812 | "correct_ans = (2/pi)*arcsin(sqrt(t1/t2))\n",
813 | "count = 0\n",
814 | "\n",
815 | "for _ in range(n):\n",
816 | " W_t = 0\n",
817 | " time = 0\n",
818 | " while (time <= t2):\n",
819 | " s_i = step_height * np.random.randn()\n",
820 | " \n",
821 | " if (t1 <= time) & (time <= t2) & (W_t * (W_t + s_i) < 0):\n",
822 | " count += 1\n",
823 | " break\n",
824 | " time += dt\n",
825 | " W_t += s_i\n",
826 | "\n",
827 | "estimate = 1 - (count / n)\n",
828 | "error = abs(estimate - correct_ans)\n",
829 | "\n",
830 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
831 | ]
832 | },
833 | {
834 | "cell_type": "code",
835 | "execution_count": 28,
836 | "metadata": {},
837 | "outputs": [
838 | {
839 | "name": "stdout",
840 | "output_type": "stream",
841 | "text": [
842 | "Correct Answer: 0.295167 / Estimate: 0.309200 / Error: 0.014033\n"
843 | ]
844 | }
845 | ],
846 | "source": [
847 | "# (b)\n",
848 | "time_interval = [2, 10]\n",
849 | "t1, t2 = time_interval\n",
850 | "correct_ans = (2/pi)*arcsin(sqrt(t1/t2))\n",
851 | "count = 0\n",
852 | "\n",
853 | "for _ in range(n):\n",
854 | " W_t = 0\n",
855 | " time = 0\n",
856 | " while (time <= t2):\n",
857 | " s_i = step_height * np.random.randn()\n",
858 | " \n",
859 | " if (t1 <= time) & (time <= t2) & (W_t * (W_t + s_i) < 0):\n",
860 | " count += 1\n",
861 | " break\n",
862 | " time += dt\n",
863 | " W_t += s_i\n",
864 | "\n",
865 | "estimate = 1 - (count / n)\n",
866 | "error = abs(estimate - correct_ans)\n",
867 | "\n",
868 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
869 | ]
870 | },
871 | {
872 | "cell_type": "code",
873 | "execution_count": 29,
874 | "metadata": {},
875 | "outputs": [
876 | {
877 | "name": "stdout",
878 | "output_type": "stream",
879 | "text": [
880 | "Correct Answer: 0.704833 / Estimate: 0.720800 / Error: 0.015967\n"
881 | ]
882 | }
883 | ],
884 | "source": [
885 | "# (a)\n",
886 | "time_interval = [8, 10]\n",
887 | "t1, t2 = time_interval\n",
888 | "correct_ans = (2/pi)*arcsin(sqrt(t1/t2))\n",
889 | "count = 0\n",
890 | "\n",
891 | "for _ in range(n):\n",
892 | " W_t = 0\n",
893 | " time = 0\n",
894 | " while (time <= t2):\n",
895 | " s_i = step_height * np.random.randn()\n",
896 | " \n",
897 | " if (t1 <= time) & (time <= t2) & (W_t * (W_t + s_i) < 0):\n",
898 | " count += 1\n",
899 | " break\n",
900 | " time += dt\n",
901 | " W_t += s_i\n",
902 | "\n",
903 | "estimate = 1 - (count / n)\n",
904 | "error = abs(estimate - correct_ans)\n",
905 | "\n",
906 | "print(\"Correct Answer: %f / Estimate: %f / Error: %f\" % (correct_ans, estimate, error))"
907 | ]
908 | }
909 | ],
910 | "metadata": {
911 | "kernelspec": {
912 | "display_name": "Python 3",
913 | "language": "python",
914 | "name": "python3"
915 | },
916 | "language_info": {
917 | "codemirror_mode": {
918 | "name": "ipython",
919 | "version": 3
920 | },
921 | "file_extension": ".py",
922 | "mimetype": "text/x-python",
923 | "name": "python",
924 | "nbconvert_exporter": "python",
925 | "pygments_lexer": "ipython3",
926 | "version": "3.6.3"
927 | }
928 | },
929 | "nbformat": 4,
930 | "nbformat_minor": 2
931 | }
932 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Numerical Analysis
2 | Timothy Sauer - Numerical Analysis(2nd edition)
3 |
4 | These are solutions for the book's computer problems.
5 | If there is any mistake or comment, please let me know.
6 | Thank you.
7 |
8 |
--------------------------------------------------------------------------------
/Sauer - Numerical Analysis.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riverlike14/Numerical_Analysis/c2cb61f409553dbd2fe0bb23c1e669e8c03649dc/Sauer - Numerical Analysis.pdf
--------------------------------------------------------------------------------