diff options
author | Prashant S | 2020-04-14 10:25:32 +0530 |
---|---|---|
committer | GitHub | 2020-04-14 10:25:32 +0530 |
commit | 06b09e7d29d252fb2f5a056eeb8bd1264ff6a333 (patch) | |
tree | 2b1df110e24ff0174830d7f825f43ff1c134d1af /Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb | |
parent | abb52650288b08a680335531742a7126ad0fb846 (diff) | |
parent | 476705d693c7122d34f9b049fa79b935405c9b49 (diff) | |
download | all-scilab-tbc-books-ipynb-master.tar.gz all-scilab-tbc-books-ipynb-master.tar.bz2 all-scilab-tbc-books-ipynb-master.zip |
Initial commit
Diffstat (limited to 'Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb')
-rw-r--r-- | Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb | 451 |
1 files changed, 451 insertions, 0 deletions
diff --git a/Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb b/Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb new file mode 100644 index 0000000..8588c3b --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/7-Numerical_Solutions_for_Matrix_Inversion.ipynb @@ -0,0 +1,451 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 7: Numerical Solutions for Matrix Inversion" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.1: Gauss_Jordan_Two_Array_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.1\n", +"//Gauss-Jordan Two Array Method\n", +"//Page no. 254\n", +"clc;clear;close;\n", +"\n", +"A=[2,6,1;3,9,2;0,-1,3]; //matrix\n", +"C=eye(3,3); //Unit Matrix\n", +"for i=1:3 //interchange of row 1 and 2\n", +" B(1,i)=A(1,i);\n", +" A(1,i)=A(2,i);\n", +" A(2,i)=B(1,i);\n", +" B(2,i)=C(1,i);\n", +" C(1,i)=C(2,i);\n", +" C(2,i)=B(2,i);\n", +"end\n", +"printf('\n')\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"\n", +"for i=1:3\n", +" A(1,i)=A(1,i)/3;\n", +" C(1,i)=C(1,i)/3;\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"for i=1:3\n", +" A(2,i)=A(2,i)-2*A(1,i);\n", +" C(2,i)=C(2,i)-2*C(1,i);\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"for i=1:3 //interchange of row 2 and 3\n", +" B(1,i)=A(2,i);\n", +" A(2,i)=A(3,i);\n", +" A(3,i)=B(1,i);\n", +" B(2,i)=C(2,i);\n", +" C(2,i)=C(3,i);\n", +" C(3,i)=B(2,i);\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"for i=1:3\n", +" A(2,i)=-1*A(2,i);\n", +" C(2,i)=-1*C(2,i);\n", +"end\n", +"for i=1:3\n", +" A(1,i)=A(1,i)-3*A(2,i);\n", +" C(1,i)=C(1,i)-3*C(2,i);\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"for i=1:3\n", +" A(3,i)=-3*A(3,i);\n", +" C(3,i)=-3*C(3,i);\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"for i=1:3\n", +" A(1,i)=A(1,i)-A(3,i)*(29/3);\n", +" C(1,i)=C(1,i)-29*C(3,i)/3;\n", +"end\n", +"for i=1:3\n", +" A(2,i)=A(2,i)+A(3,i)*3;\n", +" C(2,i)=C(2,i)+C(3,i)*3;\n", +"end\n", +"\n", +"//printing of matrices A and C\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%f\t',A(i,j))\n", +" end\n", +" printf('|\t');\n", +" for j=1:3\n", +" printf('%f\t',C(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n');\n", +"\n", +"disp(C,'Inverse Matrix of A')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.2: Inverse_in_Place_without_Pivoting.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.2\n", +"//Inverse in Place without Pivoting\n", +"//Page no. 256\n", +"clc;clear;close;\n", +"\n", +"A=[3,-6,7;9,0,-5;5,-8,6]; //matrix\n", +"B=[3,-6,7;9,0,-5;5,-8,6]; //copied matrix\n", +"for i=1:3\n", +" printf('\n\nStage %i',i);\n", +" for j=1:3\n", +" if(i==j)\n", +" B(i,j)=1/B(i,j);\n", +" else\n", +" B(i,j)=A(i,j)/A(i,i);\n", +" end,\n", +" end\n", +" disp(B)\n", +" for j=1:3\n", +" for k=1:3\n", +" if(i~=j)\n", +" B(j,k)=A(j,k)-A(j,i)*B(i,k);\n", +" end,\n", +" end\n", +" end\n", +" disp(B)\n", +" for j=1:3\n", +" if(i~=j)\n", +" B(j,i)=-1*A(j,i)*B(i,i);\n", +" end,\n", +" \n", +" end\n", +" disp(B)\n", +" A=B;\n", +"end\n", +"disp(B,'Inverse of Matrix A=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.3: Inverse_in_Place_with_Pivoting.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.3\n", +"//Inverse in Place with Pivoting\n", +"//Page no. 258\n", +"clc;clear;close;\n", +"\n", +"A=[3,-6,7;9,0,-5;5,-8,6]; //matrix\n", +"B=[3,-6,7;9,0,-5;5,-8,6]; //copied matrix\n", +"\n", +"for i=1:3\n", +" printf('\n\nStage %i',i)\n", +" if(i<3)\n", +" for j=1:3 //interchange of rows\n", +" C(i,j)=A(i,j);\n", +" A(i,j)=A(i+1,j);\n", +" A(i+1,j)=C(i,j);\n", +" C(i,j)=B(i,j);\n", +" B(i,j)=B(i+1,j);\n", +" B(i+1,j)=C(i,j);\n", +" end\n", +" end\n", +" disp(B)\n", +" for j=1:3\n", +" if(i==j)\n", +" B(i,j)=1/B(i,j);\n", +" else\n", +" B(i,j)=A(i,j)/A(i,i);\n", +" end,\n", +" end\n", +" for j=1:3\n", +" for k=1:3\n", +" if(i~=j)\n", +" B(j,k)=A(j,k)-A(j,i)*B(i,k);\n", +" end,\n", +" end\n", +" end\n", +" for j=1:3\n", +" if(i~=j)\n", +" B(j,i)=-1*A(j,i)*B(i,i);\n", +" end,\n", +" \n", +" end\n", +" disp(B)\n", +" A=B;\n", +"end\n", +"for j=1:3 //interchange of column 2 and 3\n", +" C(j,1)=A(j,2);\n", +" A(j,2)=A(j,3);\n", +" A(j,3)=C(j,1);\n", +"end\n", +"for j=1:3 //interchange of column 2 and 1\n", +" C(j,1)=A(j,2);\n", +" A(j,2)=A(j,1);\n", +" A(j,1)=C(j,1);\n", +"end\n", +"disp(A,'Inverse of Matrix A=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.4: Inverse_of_Triangular_Matrices.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.4\n", +"//Inverse of Triangular Matrices\n", +"//Page no. 260\n", +"clc;clear;close;\n", +"\n", +"R=[2,4,-4,0;0,3,-3,-3;0,0,4,2;0,0,0,3]; //matrix R\n", +"for i=4:-1:1\n", +" for j=4:-1:1\n", +" if(i>j)\n", +" Y(i,j)=0;\n", +" end\n", +" if(i==j)\n", +" Y(i,j)=1/R(i,j);\n", +" end\n", +" if(i<j)\n", +" l=0;\n", +" for k=i+1:j\n", +" l=l-R(i,k)*Y(k,j);\n", +" end\n", +" Y(i,j)=l/R(i,i);\n", +" end \n", +" end\n", +"end\n", +"disp(Y,'Inverse of Matrix R=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.5: Inverse_of_Complex_Matrices.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.5\n", +"//Inverse of Complex Matrices\n", +"//Page no. 262\n", +"clc;clear;close;\n", +"\n", +"A=[1,-1,0;2,3,4;0,1,2];\n", +"B=[1,1,3;1,3,-3;-2,-4,-4];\n", +"P=A+%i*B;\n", +"disp(P,'Matrix P=')\n", +"disp(A,'Matrix A=');disp(B,'Matrix B=');\n", +"A1=inv(A);B1=inv(B);\n", +"disp(A1,'Inverse of Matrix A=');\n", +"disp(B1,'Inverse of Matrix B=');\n", +"B1A=B1*A;disp(B1A,'Inverse(B)*A=');\n", +"AB1A_B=A*B1A+B;disp(AB1A_B,'A*Inverse(B)*A+B=');\n", +"AB1A_B1=inv(AB1A_B);disp(AB1A_B1,'Inverse(A*Inverse(B)*A+B)=');\n", +"X=B1A*AB1A_B1;disp(X,'X=');\n", +"Y=-1*AB1A_B1;disp(Y,'Y=');\n", +"Q=X+%i*Y;disp(Q,'Inverse of Matrix P=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7.6: Iterative_Procedure.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 7.6\n", +"//Iterative Procedure\n", +"//Page no. 265\n", +"clc;clear;close;\n", +"\n", +" A=[3,1,3/2;-5/4,-1/4,-3/4;-1/4,-1/4,-1/4];\n", +"disp(A,'Matrix A=');\n", +"B=[1,1,3.5;1,3,-3;-2,-3,-4];\n", +"disp(B,'Assumed Matrix B=');\n", +"e=0.1;\n", +"\n", +"//iterations\n", +"E1=e;k=1;\n", +"while(E1>=e)\n", +" printf('\n\n\nIteration %i\n',k)\n", +"C=B*(2*eye(3,3)-A*B);disp(C,'Matrix C=');\n", +"E=A*C-eye(3,3);disp(E,'Matrix E=');\n", +"B=C;printf('\nInverse of Matrix A after %i iterations=',k);disp(B);\n", +"E1=0;\n", +"for i=1:3\n", +" for j=1:3\n", +" E1=E1+E(i,j)^2;\n", +" end\n", +"end\n", +"E1=sqrt(E1);\n", +"k=k+1;\n", +"end" + ] + } +], +"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 +} |