{
"cells": [
 {
		   "cell_type": "markdown",
	   "metadata": {},
	   "source": [
       "# Chapter 8: Iterative Solution of Linear Equations"
	   ]
	},
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 8.1: Gauss_Jacobi_Iteration_Method.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Example No. 8_01\n",
"//Gauss Jacobi\n",
"//Page No. 254\n",
"clear ; close ; clc ;\n",
"\n",
"A = [ 2  1  1 ; 3  5  2 ; 2  1  4];\n",
"B = [ 5  ;  15  ;  8];\n",
"x1old = 0 ,x2old = 0 , x3old = 0 //intial assumption of x1,x2 & x3\n",
"\n",
"disp('x1 = (5 - x2 - x3)/2 ')\n",
"disp('x2 = (15 - 3x1 - 2x3)/5 ')\n",
"disp('x3 = (8 - 2x1 - x2)/4')\n",
"\n",
"for i = 1:4\n",
"    printf('\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",
"    printf(' \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",
"end\n",
""
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 8.2: Gauss_Seidel_Iterative_Method.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Example No. 8_02\n",
"//Gauss Seidel\n",
"//Page No.261\n",
"clear ; close ; clc ;\n",
"\n",
"A = [ 2  1  1 ; 3  5  2 ; 2  1  4];\n",
"B = [ 5  ;  15  ;  8];\n",
"x1old = 0 ,x2old = 0 , x3old = 0 //intial assumption\n",
"\n",
"disp('(x1 = 5 - x2 - x3)/2 ')\n",
"disp('(x2 = 15 - 3x1 - 2x3)/5 ')\n",
"disp('(x3 = 8 - 2x1 - x2)/4')\n",
" \n",
"for i = 1:2\n",
"     \n",
"    printf('\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",
"     printf(' \n x1 = %f\n x2 = %f\n x3 = %f\n',x1,x2,x3)\n",
"     \n",
"end"
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 8.3: Gauss_Seidel_Iterative_Method.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Example No. 8_03\n",
"//Gauss Seidel\n",
"//page no. 269\n",
"clear ; close ; clc ;\n",
"\n",
"A = [ 3 1 ; 1 -3]\n",
"B = [ 5 ; 5 ]\n",
"\n",
"disp('Using a matrix to display the results after each iteration, first row represents initial assumption')\n",
"X(1,1) = 0 , X(1,2) = 0 ;//initial assumption\n",
"\n",
"maxit = 1000;//Maximum number of iterations\n",
"err = 0.0003 ;\n",
"\n",
"disp('x1 = (5-x2)/3');\n",
"disp('x2 = (x1 - 5)/3');\n",
"\n",
"for i = 2:maxit\n",
"    \n",
"    X(i,1) = (5 - X(i-1,2))/3 ;\n",
"    X(i,2) = (X(i,1) - 5)/3 ;\n",
"    \n",
"    //Error Calculations\n",
"    err1 =abs((X(i,1) - X(i-1,1))/X(i,1)) \n",
"    err2 =abs((X(i,2)- X(i-1,2))/X(i,2))\n",
"    \n",
"    //Terminating Condition \n",
"    if err >= err1 & err >= err2  then\n",
"        printf('The system converges to the solution ( %f , %f ) in %d iterations\n',X(i,1),X(i,2),i-1 ) \n",
"        break\n",
"    end\n",
"       \n",
"end\n",
"//calcution of true error i.e. difference between final result and results from each iteration\n",
"trueerr1 = abs(X(:,1) - X(i,1)*ones(i,1)) ;\n",
"trueerr2 = abs(X(:,2) - X(i,2)*ones(i,1)) ;\n",
"\n",
"//displaying final results\n",
"D = [  X  trueerr1   trueerr2] ;\n",
"disp(D)"
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 8.4: Gauss_Seidel_Iterative_Method.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Example No. 8_04\n",
"//Gauss Seidel\n",
"//Page No.261\n",
"clear ; close ; clc ;\n",
"\n",
"A = [ 1 -3 ; 3 1 ];\n",
"B = [ 5  ;  5 ];\n",
"x1old = 0 ,x2old = 0  //intial assumption\n",
"\n",
"disp('x1 = 5 + 3*x2 ')\n",
"disp('x2 = 5 - 3*x1 ')\n",
" \n",
"for i = 1:3\n",
"    \n",
"    x1 = 5 + 3*x2old ;\n",
"    x1old = x1;   \n",
"    x2 = 5 - 3*x1old ;\n",
"    x2old = x2; \n",
"    \n",
"     printf('\n Iteration : %i  x1 = %i and x2 = %i\n',i,x1,x2)\n",
"     \n",
"end\n",
"disp('It is clear that the process do not converge towards the solution, rather it diverges.')"
   ]
   }
],
"metadata": {
		  "kernelspec": {
		   "display_name": "Scilab",
		   "language": "scilab",
		   "name": "scilab"
		  },
		  "language_info": {
		   "file_extension": ".sce",
		   "help_links": [
			{
			 "text": "MetaKernel Magics",
			 "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
			}
		   ],
		   "mimetype": "text/x-octave",
		   "name": "scilab",
		   "version": "0.7.1"
		  }
		 },
		 "nbformat": 4,
		 "nbformat_minor": 0
}