Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "metadata": {},
  5. "cell_type": "markdown",
  6. "source": "Given the equations:\n\n$$\n\\begin{eqnarray*}\n10x_1 + 2x_2 - x_3 = 27 \\\\\n-3x_1 -6x_2 + 2x_3 = -61.5 \\\\\nx_1 + x_2 + 5x_3 = - 21.5 \\\\\n\\end{eqnarray*}\n$$"
  7. },
  8. {
  9. "metadata": {},
  10. "cell_type": "markdown",
  11. "source": "(a) Naive Gauss elimination"
  12. },
  13. {
  14. "metadata": {
  15. "collapsed": false,
  16. "trusted": true
  17. },
  18. "cell_type": "code",
  19. "source": "import numpy as np\nA = np.array([[10.,2.,-1.],[-3.,-6.,2.],[1.,1.,5.]])\nB = np.array([[27.],[-61.5],[-21.5]])",
  20. "execution_count": 1,
  21. "outputs": []
  22. },
  23. {
  24. "metadata": {
  25. "collapsed": false,
  26. "trusted": true
  27. },
  28. "cell_type": "code",
  29. "source": "import copy\ndef upm(A_, B_):\n A = copy.deepcopy(A_)\n B = copy.deepcopy(B_)\n for i in range(len(A)):\n for j in range((i+1),len(A)):\n c = A[j][i]/A[i][i]\n for k in range(len(A)):\n A[j][k] -= c*A[i][k]\n B[j][0] -= c*B[i][0]\n return [A,B]\n \ndef naive_gauss_elimination(A_, B_):\n A = copy.deepcopy(A_)\n B = copy.deepcopy(B_)\n A, B = upm(A, B)\n ans = np.array([0. for i in range(len(A))])\n for i in reversed(range(len(A))):\n a = B[i]\n for j in reversed(range(i+1,len(A))):\n a -= A[i][j]*ans[j]\n a /= A[i][i]\n ans[i] = a\n return ans",
  30. "execution_count": 2,
  31. "outputs": []
  32. },
  33. {
  34. "metadata": {
  35. "collapsed": false,
  36. "trusted": true
  37. },
  38. "cell_type": "code",
  39. "source": "X = naive_gauss_elimination(A,B)\nprint(X)",
  40. "execution_count": 3,
  41. "outputs": [
  42. {
  43. "name": "stdout",
  44. "output_type": "stream",
  45. "text": "[ 0.5 8. -6. ]\n"
  46. }
  47. ]
  48. },
  49. {
  50. "metadata": {},
  51. "cell_type": "markdown",
  52. "source": "(b) Substitute the results into original equations to check the answer"
  53. },
  54. {
  55. "metadata": {
  56. "collapsed": false,
  57. "scrolled": true,
  58. "trusted": true
  59. },
  60. "cell_type": "code",
  61. "source": "A.dot(X)",
  62. "execution_count": 4,
  63. "outputs": [
  64. {
  65. "data": {
  66. "text/plain": "array([ 27. , -61.5, -21.5])"
  67. },
  68. "execution_count": 4,
  69. "metadata": {},
  70. "output_type": "execute_result"
  71. }
  72. ]
  73. },
  74. {
  75. "metadata": {
  76. "collapsed": false,
  77. "scrolled": true
  78. },
  79. "cell_type": "markdown",
  80. "source": "(c) Compute the determinant of the matrix of the coefficients"
  81. },
  82. {
  83. "metadata": {
  84. "collapsed": false,
  85. "trusted": true
  86. },
  87. "cell_type": "code",
  88. "source": "np.linalg.det(A)",
  89. "execution_count": 5,
  90. "outputs": [
  91. {
  92. "data": {
  93. "text/plain": "-289.00000000000006"
  94. },
  95. "execution_count": 5,
  96. "metadata": {},
  97. "output_type": "execute_result"
  98. }
  99. ]
  100. },
  101. {
  102. "metadata": {
  103. "collapsed": false,
  104. "trusted": true
  105. },
  106. "cell_type": "code",
  107. "source": "A, _ = upm(A, B)\ndet = 1.\nfor i in range(len(A)):\n det *= A[i][i]\nprint(det)",
  108. "execution_count": 6,
  109. "outputs": [
  110. {
  111. "name": "stdout",
  112. "output_type": "stream",
  113. "text": "-289.0\n"
  114. }
  115. ]
  116. }
  117. ],
  118. "metadata": {
  119. "kernelspec": {
  120. "name": "python3",
  121. "display_name": "Python 3",
  122. "language": "python"
  123. },
  124. "language_info": {
  125. "name": "python",
  126. "version": "3.6.0",
  127. "mimetype": "text/x-python",
  128. "codemirror_mode": {
  129. "name": "ipython",
  130. "version": 3
  131. },
  132. "pygments_lexer": "ipython3",
  133. "nbconvert_exporter": "python",
  134. "file_extension": ".py"
  135. },
  136. "toc": {
  137. "threshold": 4,
  138. "number_sections": true,
  139. "toc_cell": false,
  140. "toc_window_display": false,
  141. "toc_section_display": "block",
  142. "sideBar": true,
  143. "navigate_menu": true,
  144. "moveMenuLeft": true,
  145. "widenNotebook": false,
  146. "colors": {
  147. "hover_highlight": "#DAA520",
  148. "selected_highlight": "#FFD700",
  149. "running_highlight": "#FF0000"
  150. },
  151. "nav_menu": {
  152. "height": "12px",
  153. "width": "252px"
  154. }
  155. },
  156. "varInspector": {
  157. "window_display": true,
  158. "cols": {
  159. "lenName": 16,
  160. "lenType": 16,
  161. "lenVar": 40
  162. },
  163. "kernels_config": {
  164. "python": {
  165. "library": "var_list.py",
  166. "delete_cmd_prefix": "del ",
  167. "delete_cmd_postfix": "",
  168. "varRefreshCmd": "print(var_dic_list())"
  169. },
  170. "r": {
  171. "library": "var_list.r",
  172. "delete_cmd_prefix": "rm(",
  173. "delete_cmd_postfix": ") ",
  174. "varRefreshCmd": "cat(var_dic_list()) "
  175. }
  176. },
  177. "types_to_exclude": [
  178. "module",
  179. "function",
  180. "builtin_function_or_method",
  181. "instance",
  182. "_Feature"
  183. ],
  184. "oldHeight": 122,
  185. "position": {
  186. "height": "144px",
  187. "left": "880px",
  188. "right": "20px",
  189. "top": "96px",
  190. "width": "350px"
  191. },
  192. "varInspector_section_display": "block"
  193. },
  194. "gist": {
  195. "id": "",
  196. "data": {
  197. "description": "Numerical Method Lecture - naive Gauss elimination",
  198. "public": true
  199. }
  200. }
  201. },
  202. "nbformat": 4,
  203. "nbformat_minor": 2
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement