{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 8 - Iterative solutions of linear equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example No. 8_01 Page No. 254" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1 = (5 - x2 - x3)/2 \n", "x2 = (15 - 3x1 - 2x3)/5 \n", "x3 = (8 - 2x1 - x2)/4\n", "\n", " Iteration Number : 1\n", "\n", " \n", " x1 = 2.500000\n", " x2 = 3.000000\n", " x3 = 2.000000\n", "\n", "\n", " Iteration Number : 2\n", "\n", " \n", " x1 = 0.000000\n", " x2 = 0.700000\n", " x3 = 0.000000\n", "\n", "\n", " Iteration Number : 3\n", "\n", " \n", " x1 = 2.150000\n", " x2 = 3.000000\n", " x3 = 1.825000\n", "\n", "\n", " Iteration Number : 4\n", "\n", " \n", " x1 = 0.087500\n", " x2 = 0.980000\n", " x3 = 0.175000\n", "\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import mat\n", "#Gauss Jacobi\n", "\n", "A = mat([[ 2, 1 , 1] ,[3, 5, 2],[2, 1, 4]])\n", "B = mat([[ 5],[ 15],[ 8]])\n", "x1old = 0 ;x2old = 0 ; x3old = 0 #intial assumption of x1,x2 & x3\n", "\n", "print 'x1 = (5 - x2 - x3)/2 '\n", "print 'x2 = (15 - 3x1 - 2x3)/5 '\n", "print 'x3 = (8 - 2x1 - x2)/4'\n", "\n", "for i in range(1,5):\n", " print '\\n Iteration Number : %d\\n'%(i)\n", " \n", " x1 = (5 - x2old - x3old)/2 #\n", " x2 = (15 - 3*x1old - 2*x3old)/5 # \n", " x3 = (8 - 2*x1old - x2old)/4 #\n", " \n", " print ' \\n x1 = %f\\n x2 = %f\\n x3 = %f\\n'%(x1,x2,x3)\n", " \n", " x1old = x1#\n", " x2old = x2#\n", " x3old = x3#\n", " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example No. 8_02 Page No.261" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(x1 = 5 - x2 - x3)/2 \n", "(x2 = 15 - 3x1 - 2x3)/5 \n", "(x3 = 8 - 2x1 - x2)/4\n", "\n", " Iteration Number : 1\n", " \n", " x1 = 2.500000\n", " x2 = 1.500000\n", " x3 = 0.375000\n", "\n", "\n", " Iteration Number : 2\n", " \n", " x1 = 1.562500\n", " x2 = 1.912500\n", " x3 = 0.740625\n", "\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import mat\n", "#Gauss Seidel\n", "\n", "A = mat([[ 2, 1 , 1] ,[3, 5, 2],[2, 1, 4]])\n", "B = mat([[ 5],[ 15],[ 8]])\n", "\n", "x1old = 0 ;x2old = 0 ; x3old = 0 #intial assumption\n", "\n", "print '(x1 = 5 - x2 - x3)/2 '\n", "print '(x2 = 15 - 3x1 - 2x3)/5 '\n", "print '(x3 = 8 - 2x1 - x2)/4'\n", " \n", "for i in range(1,3):\n", " \n", " print '\\n Iteration Number : %d'%(i)\n", " \n", " x1 = (5 - x2old - x3old)/2 #\n", " x1old = x1# \n", " x2 = (15 - 3*x1old - 2*x3old)/5 #\n", " x2old = x2# \n", " x3 = (8 - 2*x1old - x2old)/4 #\n", " x3old = x3#\n", " \n", " print ' \\n x1 = %f\\n x2 = %f\\n x3 = %f\\n'%(x1,x2,x3)\n", " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example No. 8_03 page no. 269" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using a matrix to display the results after each iteration, first row represents initial assumption\n", "x1 = (5-x2)/3\n", "x2 = (x1 - 5)/3\n", "The system converges to the solution ( 1.999949 , -1.000017 ) in 4 iterations\n", "\n", "Final Result is as : \n", "0.0000000000 \t0.0000000000 \t1.9999491947 \t1.00001693509\n", "-1.1111111111 \t1.6666666667 \t0.3332825281 \t0.111094176023\n", "-0.9876543210 \t2.0370370370 \t0.0370878423 \t0.0123626141002\n", "-1.0013717421 \t1.9958847737 \t0.0040644211 \t0.00135480702467\n", "-0.9998475842 \t2.0004572474 \t0.0005080526 \t0.000169350878084\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import array,zeros,ones,nditer,vstack,hstack\n", "\n", "#Gauss Seidel\n", "\n", "\n", "A = array([[ 3, 1],[ 1 ,-3]])\n", "B = array([[ 5],[5 ]])\n", "\n", "X=zeros([6,2])\n", "print ('Using a matrix to display the results after each iteration, first row represents initial assumption')\n", "X[0,0] = 0; X[0,1] = 0 ;#initial assumption\n", "\n", "maxit = 1000;#Maximum number of iterations\n", "err = 0.0003 ;\n", "\n", "print('x1 = (5-x2)/3');\n", "print('x2 = (x1 - 5)/3');\n", "\n", "for i in range(1,maxit):\n", " \n", " X[i,0] = (5 - X[i-1,1])/3 ;\n", " X[i,1] = (X[i,0] - 5)/3 ;\n", " \n", " #Error Calculations\n", " err1 =abs((X[i,0] - X[i-1,0])/X[i,0]) \n", " err2 =abs((X[i,1]- X[i-1,1])/X[i,1])\n", " \n", " #Terminating Condition \n", " if err >= err1 and err >= err2:\n", " print 'The system converges to the solution ( %f , %f ) in %d iterations\\n'%(X[i,0],X[i,1],i-1) \n", " break\n", " \n", " \n", "\n", "#calcution of true error i.e. difference between final result and results from each iteration\n", "trueerr1 = abs(X[:,0] - X[i,0]*ones([i+1,1])) ;\n", "trueerr2 = abs(X[:,1] - X[i,1]*ones([i+1,1])) ;\n", "\n", "#displaying final results\n", "print 'Final Result is as : '\n", "for i in range(0,5):\n", " print '%.10f'%X[i,:][1],'\\t%.10f'%X[i,:][0],'\\t%.10f'%trueerr1[0,i],'\\t',trueerr2[0,i]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example No. 8_04 Page No.261" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1 = 5 + 3*x2 \n", "x2 = 5 - 3*x1 \n", "\n", " Iteration : 1 x1 = 5 and x2 = -10\n", "\n", "\n", " Iteration : 2 x1 = -25 and x2 = 80\n", "\n", "\n", " Iteration : 3 x1 = 245 and x2 = -730\n", "\n", "It is clear that the process do not converge towards the solution, rather it diverges.\n" ] } ], "source": [ "from numpy import mat\n", "#Gauss Seidel\n", "\n", "A = mat([[ 1, -3],[3, 1 ]])\n", "B = mat([[ 5],[5] ])\n", "x1old = 0 #intial assumption\n", "x2old = 0 #intial assumption\n", "\n", "print 'x1 = 5 + 3*x2 '\n", "print 'x2 = 5 - 3*x1 '\n", " \n", "for i in range(1,4):\n", " x1 = 5 + 3*x2old \n", " x1old = x1\n", " x2 = 5 - 3*x1old \n", " x2old = x2\n", " \n", " print '\\n Iteration : %d x1 = %d and x2 = %d\\n'%(i,x1,x2)\n", " \n", "print 'It is clear that the process do not converge towards the solution, rather it diverges.'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }