Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "metadata": {},
- "cell_type": "markdown",
- "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$$"
- },
- {
- "metadata": {},
- "cell_type": "markdown",
- "source": "(a) Naive Gauss elimination"
- },
- {
- "metadata": {
- "collapsed": false,
- "trusted": true
- },
- "cell_type": "code",
- "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]])",
- "execution_count": 1,
- "outputs": []
- },
- {
- "metadata": {
- "collapsed": false,
- "trusted": true
- },
- "cell_type": "code",
- "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",
- "execution_count": 2,
- "outputs": []
- },
- {
- "metadata": {
- "collapsed": false,
- "trusted": true
- },
- "cell_type": "code",
- "source": "X = naive_gauss_elimination(A,B)\nprint(X)",
- "execution_count": 3,
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": "[ 0.5 8. -6. ]\n"
- }
- ]
- },
- {
- "metadata": {},
- "cell_type": "markdown",
- "source": "(b) Substitute the results into original equations to check the answer"
- },
- {
- "metadata": {
- "collapsed": false,
- "scrolled": true,
- "trusted": true
- },
- "cell_type": "code",
- "source": "A.dot(X)",
- "execution_count": 4,
- "outputs": [
- {
- "data": {
- "text/plain": "array([ 27. , -61.5, -21.5])"
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ]
- },
- {
- "metadata": {
- "collapsed": false,
- "scrolled": true
- },
- "cell_type": "markdown",
- "source": "(c) Compute the determinant of the matrix of the coefficients"
- },
- {
- "metadata": {
- "collapsed": false,
- "trusted": true
- },
- "cell_type": "code",
- "source": "np.linalg.det(A)",
- "execution_count": 5,
- "outputs": [
- {
- "data": {
- "text/plain": "-289.00000000000006"
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ]
- },
- {
- "metadata": {
- "collapsed": false,
- "trusted": true
- },
- "cell_type": "code",
- "source": "A, _ = upm(A, B)\ndet = 1.\nfor i in range(len(A)):\n det *= A[i][i]\nprint(det)",
- "execution_count": 6,
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": "-289.0\n"
- }
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3",
- "language": "python"
- },
- "language_info": {
- "name": "python",
- "version": "3.6.0",
- "mimetype": "text/x-python",
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "pygments_lexer": "ipython3",
- "nbconvert_exporter": "python",
- "file_extension": ".py"
- },
- "toc": {
- "threshold": 4,
- "number_sections": true,
- "toc_cell": false,
- "toc_window_display": false,
- "toc_section_display": "block",
- "sideBar": true,
- "navigate_menu": true,
- "moveMenuLeft": true,
- "widenNotebook": false,
- "colors": {
- "hover_highlight": "#DAA520",
- "selected_highlight": "#FFD700",
- "running_highlight": "#FF0000"
- },
- "nav_menu": {
- "height": "12px",
- "width": "252px"
- }
- },
- "varInspector": {
- "window_display": true,
- "cols": {
- "lenName": 16,
- "lenType": 16,
- "lenVar": 40
- },
- "kernels_config": {
- "python": {
- "library": "var_list.py",
- "delete_cmd_prefix": "del ",
- "delete_cmd_postfix": "",
- "varRefreshCmd": "print(var_dic_list())"
- },
- "r": {
- "library": "var_list.r",
- "delete_cmd_prefix": "rm(",
- "delete_cmd_postfix": ") ",
- "varRefreshCmd": "cat(var_dic_list()) "
- }
- },
- "types_to_exclude": [
- "module",
- "function",
- "builtin_function_or_method",
- "instance",
- "_Feature"
- ],
- "oldHeight": 122,
- "position": {
- "height": "144px",
- "left": "880px",
- "right": "20px",
- "top": "96px",
- "width": "350px"
- },
- "varInspector_section_display": "block"
- },
- "gist": {
- "id": "",
- "data": {
- "description": "Numerical Method Lecture - naive Gauss elimination",
- "public": true
- }
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement