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 | |
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')
21 files changed, 11808 insertions, 0 deletions
diff --git a/Numerical_Methods_Principles_by_Analysis/1-Background_to_Numerical_Methods.ipynb b/Numerical_Methods_Principles_by_Analysis/1-Background_to_Numerical_Methods.ipynb new file mode 100644 index 0000000..31fa91f --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/1-Background_to_Numerical_Methods.ipynb @@ -0,0 +1,1383 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 1: Background to Numerical Methods" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.10: Conversion_to_Binary_System_and_to_Base_N.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.10\n", +"//Conversion to Binary System and to Base N\n", +"//Page no. 9\n", +"clc;close;clear;\n", +"\n", +"b=dec2bin(oct2dec('1753'))\n", +"disp(b,'Binary form of 1753 is ='); //inbuilt function\n", +"b=dec2hex(oct2dec('1753'))\n", +"disp(b,'Hexadecimal form of 1753 is ='); //inbuilt function\n", +"//conversion from octal to binary\n", +"b=dec2bin(hex2dec('A478'))\n", +"disp(b,'Binary form of A478 is ='); //inbuilt function\n", +"b=dec2oct(hex2dec('A478'))\n", +"disp(b,'Octal form of A478 is ='); //inbuilt function\n", +"//conversion from octal to binary\n", +"s=dec2bin(oct2dec('3'));\n", +"s1=dec2bin(oct2dec('154')); //inbuilt function\n", +"printf('\n Octal form of 3.154 is = \n\n %s.00%s',s,s1)\n", +"s=dec2hex(oct2dec('3'));\n", +"s1=dec2hex(oct2dec('154')); //inbuilt function\n", +"printf('\n\n Hexadecimal form of 3.154 is = \n\n %s.%s',s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.13: 1s_compliment_and_2s_compliment.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.13\n", +"//1s compliment and 2s compliment\n", +"//Page no. 11\n", +"clc;close;clear;\n", +"\n", +"function [x1]=com1(x) //function for 1s compliment\n", +" for i=8:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" if(floor((xd*10)+0.1)==1)\n", +" x1(1,i)=0;\n", +" else\n", +" x1(1,i)=1;\n", +" end\n", +" x=x-xd;\n", +" end\n", +"endfunction\n", +"function [x1]=com2(x) //function for 2s compliment()\n", +" for i=8:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" if(int((xd*10)+0.1)==1)\n", +" x1(1,i)=0;\n", +" else\n", +" x1(1,i)=1;\n", +" end\n", +" end\n", +" for i=8:-1:1\n", +" if (x1(1,i)==0) then\n", +" x1(1,i)=1;\n", +" break;\n", +" else\n", +" x1(1,i)=0;\n", +" end\n", +" \n", +" end\n", +"endfunction\n", +"a=[00010011,01110110,11101101,10000001,10000000,00000000];\n", +"for i=1:6\n", +" printf('1s Compliment of %.8i=',a(i));\n", +" disp(com1(a(i)))\n", +" printf('2s Compliment of %.8i=',a(i));\n", +" disp(com2(a(i)))\n", +" printf('\n\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.14: 1s_compliment.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.14\n", +"//1s compliment\n", +"//Page no. 12\n", +"clc;close;clear;\n", +"\n", +"function [x1]=com1(x) //function for 1s compliment\n", +" for i=8:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" if(floor((xd*10)+0.1)==1)\n", +" x1(1,i)=0;\n", +" else\n", +" x1(1,i)=1;\n", +" end\n", +" x=x-xd;\n", +" end\n", +"endfunction\n", +"a=[00010011,01110110,11101101,10000001,10000000,00000000];\n", +"for i=1:6\n", +" printf('1s Compliment of %.8i=',a(i));\n", +" disp(com1(a(i)))\n", +" printf('\n\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.15: Addition_and_Subtraction.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.15\n", +"//Addition and Subtraction\n", +"//Page no. 13\n", +"clc;clear;close;\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" for i=1:10\n", +" x1(1,i)=0\n", +" end\n", +" for i=10:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=0;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" disp(x1,'Addition of 173 and 141= ')\n", +"endfunction\n", +"function [x1]=sub(x,y) //function for subtraction of binaries\n", +" c=0;\n", +" for i=1:10\n", +" x1(1,i)=0\n", +" end\n", +" for i=10:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" elseif floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" elseif floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==1\n", +" x1(1,i)=1;c=1;\n", +" end\n", +" end\n", +" end\n", +" disp(x1,'Subtraction of 45 from 228= ')\n", +"endfunction\n", +"add(10101101,10001101)\n", +"sub(11100100,00101101)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.16: Additio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.16\n", +"//Addition\n", +"//Page no. 14\n", +"clc;close;clear;\n", +"\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" printf('Addition of %.4i and %.4i= ',x,y)\n", +" for i=1:4\n", +" x1(1,i)=0\n", +" end\n", +" for i=4:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=1;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" \n", +" disp(x1)\n", +"endfunction\n", +"add(0010,0101);\n", +"add(1110,1011);\n", +"add(1110,0101);\n", +"add(0010,1011);\n", +"add(1110,0010);\n", +"add(0000,0000);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.17: Additio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.17\n", +"//Addition\n", +"//Page no. 14\n", +"clc;close;clear;\n", +"\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" printf('Addition of %.4i and %.4i= ',x,y)\n", +" for i=1:5\n", +" x1(1,i)=0\n", +" end\n", +" for i=5:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=1;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" \n", +" disp(x1)\n", +"endfunction\n", +"\n", +"add(0010,0101);\n", +"add(1101,1010);\n", +"add(1101,0101);\n", +"add(0010,1010);\n", +"add(1101,0010);\n", +"add(1111,0000);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.18: Additio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.18\n", +"//Addition\n", +"//Page no. 15\n", +"clc;close;clear;\n", +"\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" printf('Addition of %.4i and %.4i= ',x,y)\n", +" for i=1:5\n", +" x1(1,i)=0\n", +" end\n", +" for i=5:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=1;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" \n", +" disp(x1)\n", +"endfunction\n", +"\n", +"add(0100,0101);\n", +"add(1100,1011);\n", +"add(1000,1000);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.19: Additio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.19\n", +"//Addition\n", +"//Page no. 15\n", +"clc;close;clear;\n", +"\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" printf('Addition of %.4i and %.4i= ',x,y)\n", +" for i=1:5\n", +" x1(1,i)=0\n", +" end\n", +" for i=5:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=1;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" \n", +" disp(x1)\n", +"endfunction\n", +"\n", +"add(0010,0101);\n", +"add(11110,11011);\n", +"add(1000,0101);\n", +"add(00010,11011);\n", +"add(11110,00010);\n", +"add(11111,0000);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.1: Conversion_to_Decimal_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.1\n", +"//Conversion to Decimal System\n", +"//Page no. 4\n", +"clc;close;clear;\n", +"function [s]=bas2dec(x,b)\n", +" xi=int(x)\n", +" xd=x-int(x)\n", +" s=0\n", +" for i=1:10\n", +" xi=xi/10\n", +" s=s+(10*(xi-fix(xi))*b^(i-1))\n", +" xi=int(xi)\n", +" if(xi==0)\n", +" break\n", +" end\n", +" end\n", +" for i=1:1\n", +" xd=xd*10;\n", +" s=s+(ceil(xd)/b^(i))\n", +" xd=xd-fix(xd)\n", +" if(xd==0)\n", +" break\n", +" end\n", +" end\n", +"endfunction\n", +"\n", +"//conversion from hexadecimal to decimal system\n", +"disp(hex2dec('1A2C'),'1A2C='); //inbuit function\n", +"\n", +"//conversion from hexadecimal to decimal system\n", +"disp(bas2dec(428.5,8),'428.5=') //inline function\n", +"\n", +"//conversion from hexadecimal to decimal system\n", +"disp(bas2dec(120.1,3),'120.1=') //inline function" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.20: Subtractio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.20\n", +"//Subtraction\n", +"//Page no. 16\n", +"clc;close;clear;\n", +"function [x1]=add(x,y) //function for addition of binaries\n", +" c=0;\n", +" for i=1:5\n", +" x1(1,i)=0\n", +" end\n", +" for i=5:-1:1\n", +" x=x/10;\n", +" xd=x-fix(x)\n", +" x=x-xd;\n", +" y=y/10;\n", +" yd=y-fix(y)\n", +" y=y-yd;\n", +" if c==1 then\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=1;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=1;c=0;\n", +" else\n", +" x1(1,i)=0;c=0;\n", +" end\n", +" else\n", +" if floor((xd*10)+0.1)==1 & floor((yd*10)+0.1)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif floor((xd*10)+0.1)==0 & floor((yd*10)+0.1)==0\n", +" x1(1,i)=0;c=0;\n", +" else\n", +" x1(1,i)=1;c=0;\n", +" end\n", +" end\n", +" end\n", +" disp(x1,'Addition of 173 and 141= ')\n", +"endfunction\n", +"\n", +"add(0100,1011);\n", +"add(1100,0101);\n", +"add(1000,1000);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.23: Multiplicatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.23\n", +"//Multiplication\n", +"//Page no. 18\n", +"clc;clear;close;\n", +"\n", +"function [x1]=mul(x,y)\n", +" for i=1:8\n", +" x1(1,i)=0\n", +" end\n", +" printf('Multiplication of %.4i and %.4i = ',x,y)\n", +" x=x*y;\n", +" c=0;\n", +" for i=8:-1:1\n", +" x=x/10;\n", +" xd=floor((x-fix(x))*10+0.1)\n", +" if c==1 then\n", +" if xd==0 then\n", +" x1(1,i)=1;c=0\n", +" elseif xd==1\n", +" x1(1,i)=0;\n", +" c=1;\n", +" elseif xd==2\n", +" x1(1,i)=1;c=1;\n", +" end\n", +" else\n", +" if xd==0 | xd==1 then\n", +" x1(1,i)=xd;c=0\n", +" elseif xd==2\n", +" x1(1,i)=0;\n", +" i=i-1;c=1;\n", +" end\n", +" end\n", +" end\n", +" disp(x1)\n", +"endfunction\n", +"mul(1110,1011);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.24: Multiplicatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.24\n", +"//Multiplication\n", +"//Page no. 18\n", +"clc;clear;close;\n", +"\n", +"function [x1]=mul(x,y)\n", +" for i=1:8\n", +" x1(1,i)=0\n", +" end\n", +" printf('Multiplication of %.4i and %.4i = ',x,y)\n", +" x=x*y;\n", +" c=0;\n", +" for i=8:-1:1\n", +" x=x/10;\n", +" xd=floor((x-fix(x))*10+0.1)\n", +" if c==1 then\n", +" if xd==0 then\n", +" x1(1,i)=1;c=0\n", +" elseif xd==1\n", +" x1(1,i)=0;\n", +" c=1;\n", +" elseif xd==2\n", +" x1(1,i)=1;c=1;\n", +" end\n", +" else\n", +" if xd==0 | xd==1 then\n", +" x1(1,i)=xd;c=0\n", +" elseif xd==2\n", +" x1(1,i)=0;\n", +" i=i-1;c=1;\n", +" end\n", +" end\n", +" end\n", +" disp(x1)\n", +"endfunction\n", +"mul(1110,1011);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.25: Divisio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.25\n", +"//Division\n", +"//Page no. 19\n", +"clc;close;clear;\n", +"function [co]=com(x,y)\n", +" co=1;\n", +" for i=1:length(x)\n", +" if x(i)>y(i) then\n", +" break\n", +" elseif x(i)==y(i)\n", +" continue\n", +" else\n", +" co=0;break\n", +" end\n", +" end\n", +"endfunction\n", +"function [x1]=sub(x,y) //function for subtraction of binaries\n", +" c=0;m=0;\n", +" for i=1:5\n", +" x1(1,i)=0\n", +" end\n", +" for i=5:-1:1\n", +" if c==1 then\n", +" if x(i)==0 & y(i)==1 then\n", +" x1(1,i)=0;c=1;\n", +" elseif x(i)==0 & y(i)==0\n", +" x1(1,i)=1;c=0;\n", +" elseif x(i)==1 & y(i)==1\n", +" x1(1,i)=1;c=1;\n", +" elseif x(i)==1 & y(i)==0\n", +" x1(1,i)=0;c=0;\n", +" end\n", +" else\n", +" if x(i)==1 & y(i)==1 then\n", +" x1(1,i)=0;c=0;\n", +" elseif x(i)==0 & y(i)==0\n", +" x1(1,i)=0;c=0;\n", +" elseif x(i)==1 & y(i)==0\n", +" x1(1,i)=1;c=0;\n", +" elseif x(i)==0 & y(i)==1\n", +" x1(1,i)=1;c=1;\n", +" end\n", +" end\n", +" end\n", +" disp(x1,'Remainder = ')\n", +"endfunction\n", +"d1=11011001;d2=01011;d22=[0,0,0,0,0]\n", +"for i=8:-1:1\n", +" d3=d1/10;\n", +" div(1,i)=int(10*(d3-int(d3)))\n", +" d1=d1/10\n", +"end\n", +"for i=5:-1:1\n", +" d3=d2/10;\n", +" d21(1,i)=int(10*(d3-int(d3))+0.5)\n", +" d2=d2/10\n", +"end\n", +"div1(1,1)=0\n", +"for j=1:4\n", +" div1(1,j+1)=div(1,j)\n", +"end\n", +"for i1=1:5\n", +" printf('After Step %i : \n',i1)\n", +" if com(div1,d21)==1 then\n", +" dis(1,i1)=1\n", +" n=sub(div1,d21)\n", +" else\n", +" dis(1,i1)=0\n", +" n=sub(div1,d22)\n", +"end\n", +"disp(dis,'Divisor = ')\n", +"if i1==5 then\n", +" break\n", +"end\n", +" for j=1:5\n", +" if j<5 then\n", +" div1(1,j)=n(j+1)\n", +" else\n", +" div1(1,j)=div(1,i1+4)\n", +" end\n", +" end\n", +"\n", +"printf('\n\n\n\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.26: Multiplicatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.26\n", +"//Multiplication\n", +"//Page no. 19\n", +"clc;clear;close;\n", +"\n", +"\n", +"function [x1]=mul(x,y)\n", +" for i=1:8\n", +" x1(1,i)=0\n", +" end\n", +" printf('Multiplication of %.4i and %.4i = ',x,y)\n", +" x=x*y;\n", +" c=0;\n", +" for i=10:-1:1\n", +" x=x/10;\n", +" xd=floor((x-fix(x))*10+0.1)\n", +" if c==1 then\n", +" if xd==0 then\n", +" x1(1,i)=1;c=0\n", +" elseif xd==1\n", +" x1(1,i)=0;\n", +" c=1;\n", +" elseif xd==2\n", +" x1(1,i)=1;c=1;\n", +" end\n", +" else\n", +" if xd==0 | xd==1 then\n", +" x1(1,i)=xd;c=0\n", +" elseif xd==2\n", +" x1(1,i)=0;\n", +" i=i-1;c=1;\n", +" end\n", +" end\n", +" end\n", +" for i=1:10\n", +" if x1(1,i)==1 then\n", +" x1(1,i-1)=1;\n", +" break\n", +" end\n", +" end\n", +" disp(x1)\n", +"endfunction\n", +"mul(1110,1011);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.29: Normalized_Floating_Point_Representation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.29\n", +"//Normalized Floating Point Representation\n", +"//Page no. 23\n", +"clc;clear;close;\n", +"\n", +"function []=fp(x)\n", +" x1=x;\n", +" if x>0 then\n", +" for i=1:10\n", +" x=x/10\n", +" if int(x)==0 then\n", +" break\n", +" end\n", +" end\n", +" printf('\n %i\nNormalized Floating Point Representation of %g = %.4f x 10',i,x1,x)\n", +" else\n", +" for i=1:10\n", +" x=x*10\n", +" if ceil(x)~=0 then\n", +" break\n", +" end\n", +" end\n", +" x=x/10;i=i-1;\n", +" printf('\n -%i\nNormalized Floating Point Representation of %g = %.4f x 10',i,x1,x)\n", +" end\n", +" \n", +"endfunction\n", +"\n", +"x=[25.12,-0.00287,87000];\n", +"for i=1:3\n", +" fp(x(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.2: Conversion_Using_Shortcut_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.2\n", +"//Conversion Using Shortcut Method\n", +"//Page no. 4\n", +"clc;close;clear;\n", +"A=10;C=12;\n", +"d=(((1)*16+A)*16+2)*16+C;\n", +"disp(d,'Decimal form of 1A2C is =');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.30: Add.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.30\n", +"//Add\n", +"//Page no. 26\n", +"clc;clear;close;\n", +"a=0.4532e7;b=0.5427e7;\n", +"c=a+b\n", +"printf('Addition of %.6g and %.6g = %.6g',a,b,c)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.31: Add.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.31\n", +"//Add\n", +"//Page no. 26\n", +"clc;clear;close;\n", +"a=0.4532e5;b=0.5427e7;\n", +"c=a+b\n", +"printf('Addition of %.4g and %.6g = %.6g',a,b,c)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.32: Add.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.32\n", +"//Add\n", +"//Page no. 26\n", +"clc;clear;close;\n", +"a=0.4532e3;b=0.5427e7;\n", +"c=a+b\n", +"printf('Addition of %.2g and %.6g = %.4g',a,b,c)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.33: Add.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.33\n", +"//Add\n", +"//Page no. 27\n", +"clc;clear;close;\n", +"a=[0.4632e3,0.4632e99];b=[0.5427e3,0.5427e99];\n", +"\n", +"for i=1:2\n", +" c(i)=a(i)+b(i)\n", +" printf('\nAddition of %.2g and %.2g = %.5g\n',a(i),b(i),c(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.34: Subtractio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.34\n", +"//Subtraction\n", +"//Page no. 27\n", +"clc;clear;close;\n", +"a=[0.5427e-3,0.9627e4,0.9627e-99];b=[0.9632e-4,0.9622e4,0.9622e-99];\n", +"for i=1:3\n", +" c(i)=a(i)-b(i)\n", +" printf('\nSubtraction of %.2g from %.3g = %.6g\n',a(i),b(i),c(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.35: Multiplicatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.35\n", +"//Multiplication\n", +"//Page no. 28\n", +"clc;clear;close;\n", +"a=[0.9632e12,0.1132e12,0.1132e52,0.1132e-52];b=[0.5427e-15,0.1027e15,0.1027e50,0.1027e-50];\n", +"for i=1:4\n", +" c(i)=a(i)*b(i)\n", +" printf('\nMultiplication of %.3g and %.2g = %.6g\n',a(i),b(i),c(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.36: Divisio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.36\n", +"//Division\n", +"//Page no. 28\n", +"clc;clear;close;\n", +"a=[0.1132e1,0.1132e-6,0.1132e6];b=[0.1000e-99,0.1000e99,0.1000e3];\n", +"for i=1:3\n", +" c(i)=a(i)/b(i)\n", +" printf('\nDivision of %.2g by %.3g= %.3g\n',a(i),b(i),c(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.3: Conversion_to_Base_B_from_Decimal_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.3\n", +"//Conversion to Base B from Decimal System\n", +"//Page no. 5\n", +"clc;close;clear;\n", +"//conversion from binary to octal\n", +"disp(dec2oct(bin2dec('10101101110')),'Octal form of 10101101110 is ='); //inbuilt function\n", +"\n", +"//conversion from binary to hexadecimal\n", +"disp(dec2hex(bin2dec('10101101110')),'Hexadecimal form of 10101101110 is ='); //inbuilt function\n", +"\n", +"//conversion from binary to octal\n", +"s=dec2oct(bin2dec('1011'));\n", +"s1=dec2oct(bin2dec('110011010100')); //inbuilt function\n", +"printf('\n Octal form of 1011.1100110101 is = \n\n %s.%s',s,s1)\n", +"\n", +"//conversion from binary to hexadecimal\n", +"s=dec2hex(bin2dec('1011'));\n", +"s1=dec2hex(bin2dec('110011010100')); //inbuilt function\n", +"printf('\n\n Hexadecimal form of 1011.1100110101 is = \n\n %s.%s',s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.4: Conversion_to_Binary_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.4\n", +"//Conversion to Binary System\n", +"//Page no. 6\n", +"clc;close;clear;\n", +"//conversion from octal to binary\n", +"disp(dec2bin(oct2dec('1753')),'Binary form of 1753 is ='); //inbuilt function\n", +"\n", +"//conversion from octal to binary\n", +"disp(dec2bin(hex2dec('A478')),'Binary form of A478 is ='); //inbuilt function\n", +"\n", +"//conversion from octal to binary\n", +"s=dec2bin(oct2dec('3'));\n", +"s1=dec2bin(oct2dec('154')); //inbuilt function\n", +"printf('\n Octal form of 3.154 is = \n\n %s.00%s',s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.5: Conversion_to_Binary_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.5\n", +"//Conversion to Binary System\n", +"//Page no. 6\n", +"clc;close;clear;\n", +"//conversion from octal to binary\n", +"b=dec2bin(oct2dec('1753'))\n", +"disp(b,'Binary form of 1753 is ='); //inbuilt function\n", +"b=dec2hex(oct2dec('1753'))\n", +"disp(b,'Hexadecimal form of 1753 is ='); //inbuilt function\n", +"//conversion from octal to binary\n", +"b=dec2bin(hex2dec('A478'))\n", +"disp(b,'Binary form of A478 is ='); //inbuilt function\n", +"b=dec2oct(hex2dec('A478'))\n", +"disp(b,'Octal form of A478 is ='); //inbuilt function\n", +"//conversion from octal to binary\n", +"s=dec2bin(oct2dec('3'));\n", +"s1=dec2bin(oct2dec('154')); //inbuilt function\n", +"printf('\n Octal form of 3.154 is = \n\n %s.00%s',s,s1)\n", +"s=dec2hex(oct2dec('3'));\n", +"s1=dec2hex(oct2dec('154')); //inbuilt function\n", +"printf('\n\n Hexadecimal form of 3.154 is = \n\n %s.%s',s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.6: Conversion_to_Decimal_Number.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.6\n", +"//Conversion to Decimal Number\n", +"//Page no. 7\n", +"clc;close;clear;\n", +"\n", +"disp(dec2bin(182),'Binary of 182=') //inbuilt function" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.7: Conversion_to_Decimal_Number.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.7\n", +"//Conversion to Decimal Number\n", +"//Page no. 7\n", +"clc;close;clear;\n", +"\n", +"disp(dec2oct(467),'Octal of 467=') //inbuilt function" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.8: Conversion_to_Base_B_from_Binary_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.8\n", +"//Conversion to Base B from Binary System\n", +"//Page no. 8\n", +"clc;close;clear;\n", +"//conversion from binary to octal\n", +"disp(dec2oct(bin2dec('10101101110')),'Octal form of 10101101110 is ='); //inbuilt function\n", +"\n", +"//conversion from binary to hexadecimal\n", +"disp(dec2hex(bin2dec('10101101110')),'Hexadecimal form of 10101101110 is ='); //inbuilt function\n", +"\n", +"//conversion from binary to octal\n", +"s=dec2oct(bin2dec('1011'));\n", +"s1=dec2oct(bin2dec('110011010100')); //inbuilt function\n", +"printf('\n Octal form of 1011.1100110101 is = \n\n %s.%s',s,s1)\n", +"\n", +"//conversion from binary to hexadecimal\n", +"s=dec2hex(bin2dec('1011'));\n", +"s1=dec2hex(bin2dec('110011010100')); //inbuilt function\n", +"printf('\n\n Hexadecimal form of 1011.1100110101 is = \n\n %s.%s',s,s1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1.9: Conversion_to_Binary_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 1.9\n", +"//Conversion to Binary System\n", +"//Page no. 8\n", +"clc;close;clear;\n", +"//conversion from octal to binary\n", +"disp(dec2bin(oct2dec('1753')),'Binary form of 1753 is ='); //inbuilt function\n", +"\n", +"//conversion from octal to binary\n", +"disp(dec2bin(hex2dec('A478')),'Binary form of A478 is ='); //inbuilt function\n", +"\n", +"//conversion from octal to binary\n", +"s=dec2bin(oct2dec('3'));\n", +"s1=dec2bin(oct2dec('154')); //inbuilt function\n", +"printf('\n Octal form of 3.154 is = \n\n %s.00%s',s,s1)" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/10-Numerical_Solutions_of_System_of_Non_Linear_Equations.ipynb b/Numerical_Methods_Principles_by_Analysis/10-Numerical_Solutions_of_System_of_Non_Linear_Equations.ipynb new file mode 100644 index 0000000..72555ea --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/10-Numerical_Solutions_of_System_of_Non_Linear_Equations.ipynb @@ -0,0 +1,347 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 10: Numerical Solutions of System of Non Linear Equations" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.1: System_of_Non_Linear_Equations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.1\n", +"//System of Non Linear Equations\n", +"//Page no. 311\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^2-exp(2*x)-4')\n", +"deff('y=f1(x)','y=2*x-2*exp(2*x)')\n", +"x0=0;e=0.00001\n", +"for i=1:10\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations by newton raphshon method is %.10f',i,x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.2: Contraction_Method_and_Seidel_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.2\n", +"//Contraction Method and Seidel Method\n", +"//Page no. 315\n", +"clc;clear;close;\n", +"x(1)=0;y(1)=0\n", +"printf('(a) Contraction Mapping\n\nn\txn\t\tyn\n--------------------------------\n 0\t%f\t%f\n',x(1),y(1))\n", +"for i=2:9\n", +" x(i)=sin(x(i-1)+y(i-1))\n", +" y(i)=cos(x(i-1)-y(i-1))\n", +" printf(' %i\t%f\t%f\n',i-1,x(i),y(i))\n", +"end\n", +"printf('\n\n\n(b) Seidel Method\n\nn\txn\t\tyn\n--------------------------------\n 0\t%f\t%f\n',x(1),y(1))\n", +"for i=2:9\n", +" x(i)=sin(x(i-1)+y(i-1))\n", +" y(i)=cos(x(i)-y(i-1))\n", +" printf(' %i\t%f\t%f\n',i-1,x(i),y(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.3: Non_Linear_System_of_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.3\n", +"//Non Linear System of Equation\n", +"//Page no. 315\n", +"clc;clear;close;\n", +"x(1,1)=1;x(1,2)=0;x(1,3)=0;\n", +"y(1,1)=0;y(1,2)=2;y(1,3)=2;\n", +"printf('Case -->\t\tI\t\t\t\t\II\t\t\t\tIII\n----------------------------------------------------------------------------------------------------------\nIteration\tx\t\ty\t\tx\t\ty\t\tx\t\ty\n\n 0\t\t%f\t%f\t%f\t%f\t%f\t%f\n',x(1,1),y(1,1),x(1,2),y(1,2),x(1,3),y(1,3))\n", +"for i=2:9\n", +" printf(' %i\t',i-1)\n", +" for j=1:3\n", +" if j==1 | j==2 then\n", +" x(i,j)=(-y(i-1,j)^2-4*x(i-1,j)^2+8*x(i-1,j)+4)/8\n", +" y(i,j)=(2*y(i-1,j)^2-2*x(i-1,j)+1)/4\n", +" printf('\t%f\t%f',x(i,j),y(i,j))\n", +" end\n", +" if j==3 then\n", +" x(i,j)=(-y(i-1,j)^2-4*x(i-1,j)^2+11*x(i-1,j)+4)/11\n", +" y(i,j)=(-2*y(i-1,j)^2+8*y(i-1,j)-2*x(i-1,j)-1)/4\n", +" printf('\t%f\t%f',x(i,j),y(i,j))\n", +" end\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\n\n\nNote : There are computational errors in this example given by the book')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.4: Newton_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.4\n", +"//Newton Method\n", +"//Page no. 317\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x1,x2)','y=x1+3*log10(x1)-x2^2')\n", +"deff('y=f2(x1,x2)','y=2*x1^2-x1*x2-5*x1+1')\n", +"deff('y=f11(x1,x2)','y=1+3/(log(10)*x1)')\n", +"deff('y=f12(x1,x2)','y=-2*x2')\n", +"deff('y=f21(x1,x2)','y=4*x1-x2-5')\n", +"deff('y=f22(x1,x2)','y=-x1')\n", +"x=[3.4;2.2];\n", +"disp(x,'x(0) = ')\n", +"for i=1:3\n", +" fx=[f1(x(1),x(2));f2(x(1),x(2))]\n", +" printf('\n fx(%i) = \n',i)\n", +" disp(fx)\n", +" A=[f11(x(1),x(2)),f12(x(1),x(2));f21(x(1),x(2)),f22(x(1),x(2)),]\n", +" disp(A,'A = ')\n", +" A_1=inv(A)\n", +" disp(A_1,'Inverse of A = ')\n", +" x=x-A_1*fx\n", +" printf('\n x(%i) = \n',i)\n", +" disp(x)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.5: Newton_Raphshon_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.5\n", +"//Newton Raphshon Method\n", +"//Page no. 320\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x,y)','y=x^3-3*x*y^2+1')\n", +"deff('y=f2(x,y)','y=3*x^2*y-y^3')\n", +"deff('y=f11(x,y)','y=3*x^2-6*y^2')\n", +"deff('y=f12(x,y)','y=-6*x*y')\n", +"deff('y=f21(x,y)','y=6*x*y')\n", +"deff('y=f22(x,y)','y=3*x^2-3*y^2')\n", +"x=[0;1];\n", +"printf('\nx(0) = %g\ny(0) = %g\n',x(1),x(2))\n", +"for i=1:3\n", +" fx=[f1(x(1),x(2));f2(x(1),x(2))]\n", +" printf('\n fx(%i) = \n',i)\n", +" disp(fx)\n", +" J=[f11(x(1),x(2)),f12(x(1),x(2));f21(x(1),x(2)),f22(x(1),x(2)),]\n", +" disp(J,'J = ')\n", +" d=det(J);\n", +" if d==0 then\n", +" dx1=0;dx2=0;\n", +" else\n", +" dx1=(fx(1)*J(2,2)-fx(2)*J(1,2))/d;\n", +" dx2=(fx(2)*J(1,1)-fx(1)*J(2,1))/d;\n", +" end\n", +" x(1)=x(1)+dx1;\n", +" x(2)=x(2)+dx2;\n", +" printf('\nx(%i) = %g\ny(%i) = %g\n',i,x(1),i,x(2))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.6: Newton_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.6\n", +"//Newton Method\n", +"//Page no. 322\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x,y,z)','y=x-0.1*y^2+0.05*z^2-0.7')\n", +"deff('y=f2(x,y,z)','y=y+0.3*x^2-0.1*x*z-0.5')\n", +"deff('y=f3(x,y,z)','y=z+0.4*y^2+0.1*x*y-1.2')\n", +"deff('y=f11(x,y,z)','y=1')\n", +"deff('y=f12(x,y,z)','y=-0.2*y')\n", +"deff('y=f13(x,y,z)','y=0.1*z')\n", +"deff('y=f21(x,y,z)','y=0.6*x-0.1*z')\n", +"deff('y=f22(x,y,z)','y=1')\n", +"deff('y=f23(x,y,z)','y=-0.1*x')\n", +"deff('y=f31(x,y,z)','y=0.1*y')\n", +"deff('y=f32(x,y,z)','y=0.8*y+0.1*x')\n", +"deff('y=f33(x,y,z)','y=1')\n", +"x=[0;0;0];\n", +"printf('n\txn\t\tyn\t\tzn\n---------------------------------------------------------\n')\n", +"for i=1:6\n", +" fx=[f1(x(1),x(2),x(3));f2(x(1),x(2),x(3));f3(x(1),x(2),x(3))]\n", +" J=[f11(x(1),x(2),x(3)),f12(x(1),x(2),x(3)),f13(x(1),x(2),x(3));f21(x(1),x(2),x(3)),f22(x(1),x(2),x(3)),f23(x(1),x(2),x(3));f31(x(1),x(2),x(3)),f32(x(1),x(2),x(3)),f33(x(1),x(2),x(3))]\n", +" J_1=inv(J)\n", +" printf(' %i\t%f\t%f\t%f\n',i-1,x(1),x(2),x(3))\n", +" x=x-J_1*fx\n", +"end\n", +"printf('\n\nThe solution is x = %f, y = %f and z = %f',x(1),x(2),x(3))\n", +"\n", +"printf('\n\n\nNote : There are computation errors in calculation given by the book')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.7: Iterative_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.7\n", +"//Iterative Method\n", +"//Page no. 326\n", +"clc;clear;close;\n", +"\n", +"x=[0;0;0];\n", +"printf('n\txn\t\tyn\t\tzn\n---------------------------------------------------------\n')\n", +"for i=1:7\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\n',i-1,x(1),x(2),x(3))\n", +" x(1)=0.7+0.1*x(2)^2-0.05*x(3)^2\n", +" x(2)=0.5-0.3*x(1)^2+0.1*x(1)*x(3)\n", +" x(3)=1.2-0.4*x(2)^2-0.1*x(1)*x(2)\n", +"end\n", +"printf('\n\nThe solution is x = %.10f, y = %.10f and z = %.10f',x(1),x(2),x(3))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 10.8: Steepest_Descent.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 10.8\n", +"//Steepest Descent\n", +"//Page no. 328\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x1,x2)','y=(x1-2)^4+3*(x2+3)^2')\n", +"x=[1;-2];\n", +"printf('n\t x1\t\t x2\t\t F(x1,x2)\n-----------------------------------------------------\n')\n", +"for i=1:11\n", +" Fx=[f(x(1),x(2))];\n", +" J=[4*(x(1)-2)^3,6*(x(2)+3)];\n", +" u=(Fx*J*J'*Fx)/(J*J'*Fx*J*J'*Fx)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\n',i-1,x(1),x(2),Fx)\n", +" x=x-u*J'*Fx \n", +"end\n", +"printf('\n\nThis shows that the solution tends to x1 = %i and x2 = %i',ceil(x(1)),floor(x(2)))" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/11-Eigenvalues_and_Eigenvectors.ipynb b/Numerical_Methods_Principles_by_Analysis/11-Eigenvalues_and_Eigenvectors.ipynb new file mode 100644 index 0000000..d2208de --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/11-Eigenvalues_and_Eigenvectors.ipynb @@ -0,0 +1,786 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 11: Eigenvalues and Eigenvectors" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.10: LU_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.10\n", +"//LU Method\n", +"//Page no. 363\n", +"clc;close;clear;\n", +"\n", +"A=[120,80,40,-16;80,120,16,-40;40,16,120,-80;-16,-40,-80,120];\n", +"disp(A,'A =')\n", +"L=eye(4,4);\n", +"for l=1:20\n", +"for j=1:4\n", +" for i=1:j\n", +" k=0\n", +" for p=1:i-1\n", +" k=k-A(i,p)*A(p,j)\n", +" end\n", +" A(i,j)=A(i,j)+k\n", +" end\n", +" for i=j+1:4\n", +" k=0;\n", +" for p=1:j-1\n", +" k=k-A(i,p)*A(p,j)\n", +" end\n", +" A(i,j)=(A(i,j)+k)/A(j,j) \n", +" end\n", +"end\n", +"disp(A,'Modified A = ')\n", +" for i=1:4\n", +" for j=1:4\n", +" if i>j then\n", +" L(i,j)=A(i,j)\n", +" else\n", +" U(i,j)=A(i,j)\n", +" end\n", +" end\n", +"end\n", +"disp(U,'U =',L,'L =')\n", +"A=U*L;\n", +"printf('\n\nAfter %i iterations, matrix A =\n\n',l)\n", +"for i=1:4\n", +" for j=1:4\n", +" printf(' %.2f\t',A(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"end\n", +"printf('\n\nTherefore the eigenvalues are the diagonal elements f the transformed triangular matrix are:\n\n')\n", +"for i=1:4\n", +" printf(' %.2f,',A(i,i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.11: Generalized_Eigenvalue_Problem.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.11\n", +"//Generalized Eigenvalue Problem\n", +"//Page no. 365\n", +"clc;close;clear;\n", +"\n", +"A=[1,1,0.5;1,1,0.25;0.5,0.25,2]\n", +"B=[2,2,2;2,5,5;2,5,11]\n", +"disp(B,'B =',A,'A =')\n", +"for i=1:3\n", +" G(i,i)=sqrt(B(i,i))\n", +"end\n", +"G=[B;eye(3,3)];\n", +"\n", +"//transformation to frobenius matrix\n", +"for k=3:-1:2\n", +" g(k)=0;\n", +" for j=1:k-1\n", +" if(g(k)<G(k,j))\n", +" g(k)=G(k,j)\n", +" p=j;\n", +" end\n", +" end\n", +" if(g(k)~=0)\n", +" for j=1:3\n", +" r(1,j)=G(k,j)\n", +" end\n", +" for i=1:6\n", +" G(i,k-1)=G(i,k-1)/g(k)\n", +" end\n", +" for j=1:3\n", +" if(j~=k-1)\n", +" l=G(k,j)\n", +" for i=1:6\n", +" G(i,j)=G(i,j)-l*G(i,k-1)\n", +" end\n", +" end\n", +" end\n", +" end\n", +" for j=1:3\n", +" for i=1:3\n", +" c(i,1)=G(i,j)\n", +" end\n", +" G(k-1,j)=0\n", +" for i=1:3\n", +" G(k-1,j)=G(k-1,j)+r(1,i)*c(i,1)\n", +" end\n", +" end\n", +"end\n", +"\n", +"//partition g\n", +"for i=4:6\n", +" for j=1:3\n", +" T(i-3,j)=G(i,j)\n", +" end\n", +"end\n", +"\n", +"//eigenvalues computation\n", +"p=poly(B,'x')\n", +"a=roots(p)\n", +"printf('\n\nDiagonalized Matrix B = \n\n')\n", +"for i=1:3\n", +" for j=1:3\n", +" if i~=j then\n", +" B(i,j)=0\n", +" else\n", +" B(i,j)=a(i)\n", +" end\n", +" end\n", +"end\n", +"disp(B)\n", +"//eigenvectors computation\n", +"for k=1:3\n", +" m=2\n", +" for l=1:3\n", +" y(l,k)=a(k)^(m)\n", +" m=m-1;\n", +" end\n", +"end\n", +"printf('\n\n')\n", +"\n", +"\n", +"for k=1:3\n", +" for l=1:3\n", +" y1(l,1)=y(l,1)\n", +" y2(l,1)=y(l,2)\n", +" y3(l,1)=y(l,3)\n", +" end\n", +" x1=T*y3;\n", +" x2=T*y2;\n", +" x3=T*y1;\n", +"end\n", +"printf('\n\nEigenvectors of B are :\n\n')\n", +"for i=1:3\n", +" printf('|%.5f|\t\t|%.5f|\t\t|%.5f|',x3(i,1),x2(i,1),x1(i,1))\n", +" printf('\n')\n", +"end\n", +"x=[x3,x2,x1]\n", +"\n", +"\n", +"\n", +"\n", +"\n", +"B=[2,2,2;2,5,5;2,5,11]\n", +"G=0\n", +"for i=1:3\n", +" for j=1:3\n", +" if i==j then\n", +" G(i,j)=sqrt(B(i,j))\n", +" else\n", +" G(i,j)=0;\n", +" end\n", +" end\n", +"end\n", +"\n", +"B=inv(G)*x'*A*x*inv(G)\n", +"disp(B,'Eigenvectors of A =')\n", +"\n", +"printf('\n\n\nNote : Computation Error in book in caculation of eigenvector of B thus for A')\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.1: Eigenvalues_and_Eigenvectors.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.1\n", +"//Eigenvalues and Eigenvectors\n", +"//Page no. 333\n", +"clc;clear;close;\n", +"\n", +"A1=[0.6;0.2];A2=[-0.2;0.6];A3=[-0.6;-0.2];A4=[0.2;-0.6];\n", +"T=[1.1,-0.3;-0.3,1.9];\n", +"B1=T*A1;B2=T*A2;B3=T*A3;B4=T*A4;\n", +"disp(B4,B3,B2,B1,'The transformed vectors are :')\n", +"disp('These points lie on the ellipse:')\n", +"printf(' 2 2\n(x-3y)+(3x+y)\n------ ------\n 16 4\n\n')\n", +"A5=[0;2/sqrt(10)];\n", +"disp('The vector (0,2/10^(1/2)) lies on the circle:')\n", +"printf(' 2 2\nx + y = 4\n -\n 10\n\n')\n", +"B5=T*A5;\n", +"disp('Also lies on the same ellipse',B5)\n", +"printf('\n\nWe can see that there is a linear relationship between the first 4 vectors and their respective transformend vectors through the scalars known as eigenvectors and eigenvalues respectively')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.2: Leverriers_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.2\n", +"//Leverrier's Method\n", +"//Page no. 337\n", +"clc;close;clear;\n", +"\n", +"A=[2,2,2;2,5,5;2,5,1];\n", +"A1=A;\n", +"C(1)=0;\n", +" for j=1:3\n", +" for k=1:3\n", +" if(j==k)\n", +" C(1)=C(1)+A1(j,k)\n", +" end\n", +" end\n", +" end\n", +" disp(A,'A=')\n", +" disp(A1,'A1=')\n", +" printf('\nC1=')\n", +" disp(C(1));\n", +"for i=2:3\n", +" A2=A*(A1-C(i-1)*eye(3,3));\n", +" printf('\n\n\nA%i=',i)\n", +" disp(A2);\n", +" C(i)=0;\n", +" for j=1:3\n", +" for k=1:3\n", +" if(j==k)\n", +" C(i)=C(i)+A2(j,k)/i\n", +" end\n", +" end\n", +" end\n", +" printf('\nC%i=',i)\n", +" disp(C(i))\n", +" A1=A2;\n", +"end\n", +"printf('\n\n\nTherefore the characteristic polynomial is:\n 3 2\nx - %ix - %ix %i = 0',C(1),C(2),C(3))\n", +"\n", +"//verification\n", +"printf('\n\nVerification:')\n", +"s=poly(0,'s');\n", +"p=poly(A,'x');\n", +"A=A-eye(3,3)*%s;\n", +"disp(p,'=',A)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.3: Danilevsky_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.3\n", +"//Danilevsky Method\n", +"//Page no. 341\n", +"clc;close;clear;\n", +"\n", +"A=[-1,0,0;1,-2,3;0,2,-3];\n", +"G=[A;eye(3,3)];\n", +"disp(G);\n", +"//transformation to frobenius matrix\n", +"for k=3:-1:2\n", +" g(k)=0;\n", +" for j=1:k-1\n", +" if(g(k)<G(k,j))\n", +" g(k)=G(k,j)\n", +" p=j;\n", +" end\n", +" end\n", +" if(g(k)~=0)\n", +" for j=1:3\n", +" r(1,j)=G(k,j)\n", +" end\n", +" for i=1:6\n", +" G(i,k-1)=G(i,k-1)/g(k)\n", +" end\n", +" disp(G)\n", +" for j=1:3\n", +" if(j~=k-1)\n", +" l=G(k,j)\n", +" for i=1:6\n", +" G(i,j)=G(i,j)-l*G(i,k-1)\n", +" end\n", +" end\n", +" end\n", +" disp(G)\n", +" end\n", +" for j=1:3\n", +" for i=1:3\n", +" c(i,1)=G(i,j)\n", +" end\n", +" G(k-1,j)=0\n", +" for i=1:3\n", +" G(k-1,j)=G(k-1,j)+r(1,i)*c(i,1)\n", +" end\n", +" end\n", +" disp(G)\n", +"end\n", +"\n", +"//partition g\n", +"for i=4:6\n", +" for j=1:3\n", +" T(i-3,j)=G(i,j)\n", +" end\n", +"end\n", +"disp(T,'T=')\n", +"\n", +"//eigenvalues computation\n", +"printf('\n\n\nCharateristic polynomial:')\n", +"p=poly(A,'x')\n", +"disp(p)\n", +"printf('\n\n\nEigenvalues:')\n", +"a=roots(p)\n", +"disp(a')\n", +"//eigenvectors computation\n", +"for k=1:3\n", +" m=2\n", +" for l=1:3\n", +" y(l,k)=a(k,1)^(m)\n", +" m=m-1;\n", +" end\n", +"end\n", +"printf('\n\n')\n", +"disp(y,'y=')\n", +"\n", +"//eigenvector computation\n", +"\n", +"for k=1:3\n", +" for l=1:3\n", +" y1(l,1)=y(l,1)\n", +" y2(l,1)=y(l,2)\n", +" y3(l,1)=y(l,3)\n", +" end\n", +" x1=T*y3;\n", +" x2=T*y2;\n", +" x3=T*y1;\n", +"end\n", +"printf('\n\nEigenvectors :\n')\n", +"for i=1:3\n", +" printf('|%.1f|\t\t|%.1f|\t\t|%.1f|',x1(i,1),x2(i,1),x3(i,1))\n", +" printf('\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.4: Power_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.4\n", +"//Power Method\n", +"//Page no. 345\n", +"clc;close;clear;\n", +"\n", +"A=[1,2;3,4];\n", +"e=0.001;\n", +"q0=[1;1];\n", +"for i=1:5\n", +" q1=A*q0;\n", +" a=max(q1)\n", +" for j=1:2\n", +" q2(j)=q1(j)/a;\n", +" end\n", +" printf('\nq(%i) = %.4f a = %.4f Scaled q(%i) = %.4f\n %.4f %i\n\n',i,q1(1),a,i,q2(1),q1(2),q2(2))\n", +" q1=q2;\n", +" q0=q1;\n", +"end\n", +"printf('Hence the largest eigenvalue is %.4f with the corresponding eigenvector as %.4f\n %i',a,q0(1),q0(2))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.5: Inverse_Power_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.5\n", +"//Inverse Power Method\n", +"//Page no. 347\n", +"clc;close;clear;\n", +"\n", +"A=[7,6,-3;-12,-20,24;-6,-12,16];\n", +"e=10^-6;\n", +"X=[1;1;1];\n", +"B=0;\n", +"Y=[0;0;0]\n", +"a=0;l=0;\n", +"for i=1:2\n", +" printf('When a=%i\n',a);\n", +" C=A-a*eye();\n", +" disp(C,'C=')\n", +" C_1=inv(C);\n", +" disp(C_1,'Inverse of C=');\n", +" printf('\n\nItr lambda X')\n", +" printf('\n------------------------------------------------------------------\n')\n", +" for j=1:10\n", +" printf('\n%i %f %f %f %f',j-1,l,X(1),X(2),X(3));\n", +" Y=C_1*X;\n", +" B=max(Y);\n", +" e1=abs(l-B);\n", +" X=Y/B;\n", +" m=0;\n", +" for k=1:3\n", +" m=m+(Y(k)-X(k))^2;\n", +" end\n", +" e2=sqrt(m);\n", +" er=max(e1,e2);\n", +" if(er<e)\n", +" break\n", +" end\n", +" l=B;\n", +" \n", +" end\n", +" a=-3;\n", +" printf('\n\n\n\n')\n", +"end\n", +"printf('\n\n\nNote : Computation of Y is wrong given in the book')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.6: Rayleigh_Quotient.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.6\n", +"//Rayleigh Quotient\n", +"//Page no. 348\n", +"clc;close;clear;\n", +"\n", +"A=[10,7,8,7;7,5,6,5;8,6,10,9;7,5,9,10];\n", +"q0=[1;1;1;1];\n", +"for i=0:4\n", +" X=(A^i)*q0;\n", +" l=(X'*A*X)/(X'*X)\n", +" printf('\nLambda(%i) = %f\n',i+1,l)\n", +"end\n", +"printf('\n\nDominant Eigenvalue = %f\n\n\n',l)\n", +"\n", +"e=0.001;\n", +"for i=1:5\n", +" q1=A*q0;\n", +" a=max(q1)\n", +" for j=1:4\n", +" q2(j)=q1(j)/a;\n", +" end\n", +"\n", +" q1=q2;\n", +" q0=q1;\n", +"end\n", +"disp(q2,'Corresponding Eigenvector = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.7: Jacobi_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.7\n", +"//Jacobi's Method\n", +"//Page no. 355\n", +"clc;close;clear;\n", +"\n", +"A=[1,1,1/2;1,1,1/4;1/2,1/4,2];\n", +"C=A;\n", +"V=[sqrt(2),0,1/2;sqrt(2),0,1/4;3/(4*sqrt(2)),-1/(4*sqrt(2)),2]\n", +"S=eye(3,3)\n", +"disp(A,'A =')\n", +"VI=0;\n", +"for i=1:3\n", +" for j=1:3\n", +" if(i~=j)\n", +" VI=VI+A(i,j)^2 //initial off diag norm\n", +" end\n", +" end\n", +"end\n", +"VI=sqrt(VI);\n", +"VF=VI*10^-7; //final threshold\n", +"V1=VI/3;\n", +"o=poly(0,'o');\n", +"for i=1:3\n", +"for q=2:3\n", +" for p=q-1:-1:1\n", +" if(A(p,q)>V1)\n", +" a=-A(p,q);\n", +" b=(A(p,p)-A(q,q))/2\n", +" if(b~=0)\n", +" w=b*abs(1/b)*(a/sqrt(a^2+b^2));\n", +" else\n", +" w=(a/sqrt(a^2+b^2));\n", +" end\n", +" sin0=w/sqrt(2*(1+sqrt(1-w^2)));\n", +" cos0=sqrt(1-sin0^2)\n", +" end\n", +" B(p,p)=A(p,p)*cos0^2+A(q,q)*sin0^2-2*A(p,q)*sin0*cos0\n", +" B(q,q)=A(p,p)*sin0^2+A(q,q)*cos0^2+2*A(p,q)*sin0*cos0\n", +" B(p,q)=(A(p,p)-A(q,q))*sin0*cos0+A(p,q)*(cos0^2-sin0^2)\n", +" S(i,i)=S(i,i)\n", +" S(i,p)=S(i,p)*cos0-S(i,q)*sin0\n", +" S(i,q)=S(i,p)*sin0+S(i,q)*cos0\n", +" \n", +" end\n", +"end\n", +"end\n", +"disp(B,'B =')\n", +"disp(S,'S =')\n", +"printf('\n\n\nComputation error in the solution provided by book')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.8: Recursive_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.8\n", +"//Recursive Formula\n", +"//Page no. 357\n", +"clc;close;clear;\n", +"\n", +"A=[2,-1,0,0;-1,2,-1,0;0,-1,2,-1;0,0,-1,2];\n", +"l=poly(0,'l');\n", +"p0=1;\n", +"p1=A(1,1)-l;\n", +"for i=2:4\n", +" p2=(A(i,i)-l)*p1-A(i,i-1)^2*p0;\n", +" p0=p1;\n", +" p1=p2;\n", +" printf('\n\np%i(l) = ',i);\n", +" disp(p2)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 11.9: QR_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 11.9\n", +"//QR Method\n", +"//Page no. 360\n", +"clc;close;clear;\n", +"\n", +"A=[2,-1,0;-1,2,-1;0,-1,2];\n", +"deff('y=c(i,j)','y=A(j,j)/sqrt((A(i,j)^2+A(j,j)^2))')\n", +"deff('y=s2(i,j)','y=A(i,j)/sqrt((A(i,j)^2+A(j,j)^2))')\n", +"disp(A,'A=')\n", +"l0=0;f=1;m=0;s=0;w=0;\n", +"for n=1:5\n", +" for j=1:2\n", +" for k=1:2\n", +" V(j,k)=A(j,k)\n", +" end\n", +" end\n", +" disp(V,'V=')\n", +" p=poly(V,'x');\n", +" disp('=0',p);\n", +" a=roots(p);\n", +" for j=1:2\n", +" printf('\na(%i) = %f',j,a(j))\n", +" end\n", +" if(abs(a(1)-V(1,1))<=abs(a(2)-V(1,1)))\n", +" a=a(1)\n", +" else\n", +" a=a(2)\n", +" end\n", +" printf('\na = %f\n',a)\n", +" s=s+a;\n", +" A=A-a*eye()\n", +" R=A;Q=eye(3,3);\n", +" \n", +" for j=1:2\n", +" for i=j+1:3\n", +" for k=1:3 //C matrix evaluation\n", +" for l=1:3\n", +" if(k==l)\n", +" if(k==i | k==j)\n", +" C(k,l)=c(i,j)\n", +" else\n", +" C(k,l)=1\n", +" end\n", +" end\n", +" if(k>l)\n", +" if(k==i & l==j)\n", +" C(k,l)=-1*s2(i,j)\n", +" else\n", +" C(k,l)=0\n", +" end\n", +" end\n", +" if(k<l)\n", +" if(k==j & l==i)\n", +" C(k,l)=s2(i,j)\n", +" else\n", +" C(k,l)=0\n", +" end\n", +" end\n", +" end\n", +" end\n", +" \n", +" R=C*R;\n", +" Q=Q*C';\n", +" \n", +" end\n", +" end\n", +"disp(Q,'Q=',R,'R=')\n", +"disp(Q*R,'Q*R=')\n", +"A=R*Q;\n", +"disp(A,'A=')\n", +"end\n", +"l1=l0+s;\n", +"for i=2:3\n", +" for j=2:3\n", +" V(i-1,j-1)=A(i,j)\n", +" end\n", +"end\n", +"disp(V,'V=')\n", +" p=poly(V,'x');\n", +" disp('=0',p);\n", +" a=roots(p);\n", +" for j=1:2\n", +" printf('\na(%i) = %f',j,a(j))\n", +" end\n", +" l2=l1+a(1)\n", +" l3=l1+a(2)\n", +" disp(l3,'l3=',l2,'l2=',l1,'l1=')\n", +"printf('\n\n\nNote : Values of V varies in each step resulting in different results due to error in book calculation')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/12-Interpolation_and_Extrapolation.ipynb b/Numerical_Methods_Principles_by_Analysis/12-Interpolation_and_Extrapolation.ipynb new file mode 100644 index 0000000..04b0fab --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/12-Interpolation_and_Extrapolation.ipynb @@ -0,0 +1,501 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 12: Interpolation and Extrapolation" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.1: Linear_Interpolation_Technique.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.1\n", +"//Linear Interpolation Technique\n", +"//Page no. 372\n", +"clc;close;clear;\n", +"\n", +"printf('x: ')\n", +"f=[1,4,9,16,25];\n", +"for i=1:5\n", +" printf('%i\t',i)\n", +"end\n", +"printf('\nf(x): ')\n", +"for i=1:5\n", +" printf('%i\t',f(i))\n", +"end\n", +"x=2.5;\n", +"x1=2;x2=3;printf('\n\nfor (2,4) and (3,9)')\n", +"f(2.5)=f(x1)+(f(x2)-f(x1))*(x-x1)/(x2-x1)\n", +"printf('\nf(2.5) = %.1f',f(2.5))\n", +"\n", +"x=2.5;\n", +"x1=2;x2=4;printf('\n\nfor (2,4) and (4,16)')\n", +"f(2.5)=f(x1)+(f(x2)-f(x1))*(x-x1)/(x2-x1)\n", +"printf('\nf(2.5) = %.1f',f(2.5))\n", +"\n", +"x=2.5;\n", +"x1=1;x2=3;printf('\n\nfor (1,1) and (3,9)')\n", +"f(2.5)=f(x1)+(f(x2)-f(x1))*(x-x1)/(x2-x1)\n", +"printf('\nf(2.5) = %.1f',f(2.5))\n", +"\n", +"printf('\n\nExact value = %.2f',2.5^2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.2: Lagarangian_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.2\n", +"//Lagarangian Method\n", +"//Page no. 373\n", +"clc;close;clear;\n", +"\n", +"xk=[-1,0,2,5];\n", +"yk=[10,7,7,22];\n", +"\n", +"P=0;\n", +"x=poly(0,'x');\n", +"for k=0:3\n", +" p=yk(k+1)\n", +" for j=0:3\n", +" if(j~=k)\n", +" p=p*((x-xk(j+1))/(xk(k+1)-xk(j+1)))\n", +" end\n", +" end\n", +" P=P+p;\n", +"end\n", +"disp(P,'P=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.3: Aitken_Nevilles_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.3\n", +"//Aitken-Neville's Method\n", +"//Page no. 378\n", +"clc;close;clear;\n", +"\n", +"function [x,y,z]=tran(a,b) // function for exchanging values\n", +" z=a;y=b;x=z;\n", +"endfunction\n", +"deff('y=P(a,b,c,d,e)','y=(c(d)*b(d+1)-c(d+e)*b(d))/(a(d+e)-a(d))') //function for finding polynomials\n", +"xi=[0.8,1,1.2,1.4,1.6];\n", +"yi=[2.2255,2.7183,3.3201,4.0552,4.9530];\n", +"x=1.23\n", +"[xi(5),xi(1),a]=tran(xi(1),xi(5))\n", +"[xi(4),xi(1),a]=tran(xi(1),xi(4))\n", +"[xi(3),xi(2),a]=tran(xi(2),xi(3))\n", +"[xi(2),xi(1),a]=tran(xi(1),xi(2))\n", +"[yi(5),yi(1),a]=tran(yi(1),yi(5))\n", +"[yi(4),yi(1),a]=tran(yi(1),yi(4))\n", +"[yi(3),yi(2),a]=tran(yi(2),yi(3))\n", +"[yi(2),yi(1),a]=tran(yi(1),yi(2))\n", +"for i=1:5\n", +" x_xi(i)=x-xi(i);\n", +"end\n", +"printf('xi x-xi yi\n')\n", +"printf('------------------------\n')\n", +"for i=1:5\n", +" printf('%.1f %.2f\t%f\n',xi(i),x_xi(i),yi(i))\n", +"end\n", +"printf('\n\nPolynomials\n')\n", +"printf('-----------\n')\n", +"for i=1:4\n", +" for j=1:5-i\n", +" printf('%f\n',P(xi,yi,x_xi,j,i))\n", +" yi(j)=P(xi,yi,x_xi,j,i)\n", +" end\n", +" printf('\n\n\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.4: Newtons_Divided_Difference_Interpolation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.4\n", +"//Newton's Divided Difference Interpolation\n", +"//Page no. 381\n", +"clc;close;clear;\n", +"\n", +"x=[0,1,2,3,4,5]\n", +"y=[1,2,5,10,17,26];\n", +"y1=y;\n", +"deff('yi=P(a,b,d,e)','yi=(b(d+1)-b(d))/(a(d+e)-a(d))') //function for finding polynomials\n", +"for i=1:3\n", +" for j=1:6-i\n", +" z(j,i)=P(x,y,j,i)\n", +" y(j)=z(j,i)\n", +" end\n", +"end\n", +"z(6,1)=0;\n", +"printf('x y f(x0,x1) f(x0,x1,x3) f(x0,x1,x2,x3)\n')\n", +"printf('---------------------------------------------------------\n')\n", +" for j=1:6\n", +" printf(' %i %i \t%i\t\t%i\t\t%i\n',x(1,j),y1(1,j),z(j,1),z(j,2),z(j,3))\n", +" end\n", +"x1=2.6;\n", +"f=y1(4)+(x1-x(4))*(z(4,1))+(x1-x(4))*(x1-x(5))*z(4,2)\n", +"printf('\n\nf(2.6)=%.2f',f)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.5: Interpolation_Methods.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.5\n", +"//Interpolation Methods\n", +"//Page no. 403\n", +"clc;close;clear;\n", +"\n", +"x=[0,1,2,3,4];\n", +"y=[0,1,8,27,64];\n", +"\n", +"//Inverse lagrange Method\n", +"P=0;\n", +"y1=20;\n", +"for k=0:4\n", +" p=x(k+1)\n", +" for j=0:4\n", +" if(j~=k)\n", +" p=p*((y1-y(j+1))/(y(k+1)-y(j+1)))\n", +" end\n", +" end\n", +" P=P+p;\n", +"end\n", +"disp(P,'Inverse Lagrange interpolation x=')\n", +"\n", +"\n", +"//Newton's divide difference interpolation\n", +"x1=x;\n", +"deff('xi=P(a,b,d,y)','xi=(b(d+1)-b(d))/(a(d+y)-a(d))') //function for finding polynomials\n", +"for i=1:2\n", +" for j=1:5-i\n", +" z(j,i)=P(y,x,j,i)\n", +" x(j)=z(j,i)\n", +" end\n", +"end\n", +"z(5,1)=0;\n", +"printf('\n\n y\tx f(y0,y1) f(y0,y1,y3)\n')\n", +"printf('------------------------------------------\n')\n", +" for j=1:5\n", +" printf(' %i\t%i \t%i\t\t%i\t\n',y(1,j),x1(1,j),z(j,1),z(j,2))\n", +" end\n", +"y1=20;\n", +"f=x1(4)+(y1-y(4))*(z(4,1))+(y1-y(4))*(y1-y(5))*z(4,2)\n", +"printf('\n\nNewton Divide Difference x(20)=%.2f',f)\n", +"\n", +"x=x1;\n", +"//Iterated Linear Interpolation\n", +"function [x,y,z]=tran(a,b) // function for exchanging values\n", +" z=a;y=b;x=z;\n", +"endfunction\n", +"deff('y=P(a,b,c,d,e)','y=(c(d)*b(d+1)-c(d+e)*b(d))/(a(d+e)-a(d))') //function for finding polynomials\n", +"y1=20\n", +"\n", +"[y(4),y(1),a]=tran(y(1),y(4))\n", +"[y(3),y(2),a]=tran(y(2),y(3))\n", +"[x(4),x(1),a]=tran(x(1),x(4))\n", +"[x(3),x(2),a]=tran(x(2),x(3))\n", +"for i=1:5\n", +" y1_y(i)=y1-y(i);\n", +"end\n", +"printf('y\ty1-y\tx\n')\n", +"printf('------------------\n')\n", +"for i=1:5\n", +" printf('%.1f\t%i\t%i\n',y(i),y1_y(i),x(i))\n", +"end\n", +"printf('\n\nPolynomials\n')\n", +"printf('-----------\n')\n", +"for i=1:4\n", +" for j=1:5-i\n", +" printf('%f\n',P(y,x,y1_y,j,i))\n", +" x(j)=P(y,x,y1_y,j,i)\n", +" end\n", +" printf('\n\n')\n", +"end\n", +"printf('Iterated Linear Interpolation x(20) = %f',x(j))\n", +"\n", +"x=[0,1,2,3,4];\n", +"y=[0,1,8,27,64];\n", +"y1=y;\n", +"//Suggested Interpolation\n", +"\n", +"for i=1:4\n", +" for j=1:5-i\n", +" z(j,i)=y(j+1)-y(j);\n", +" y(j)=z(j,i)\n", +" end\n", +"end\n", +"printf('\n\n\n x\ty\tdy\td2y\td3y\td4y\n')\n", +"printf('--------------------------------------------\n')\n", +"for i=1:5\n", +" printf(' %i\t%i\t%i\t%i\t%i\t%i\n',x(i),y1(i),z(i,1),z(i,2),z(i,3),z(i,4))\n", +"end\n", +"s=poly(0,'s')\n", +"p=y1(4);k=3;\n", +"for i=1:3\n", +" r=1;\n", +" for j=1:i\n", +" r=r*(s+(j-1))\n", +" end\n", +" r=r*z(k,i)/factorial(j);\n", +" k=k-1;\n", +" p=p+r;\n", +" printf('\n\nStage %i :',i)\n", +" disp(p)\n", +"end\n", +"s0=-7/19;\n", +"disp(s0,'s0=');\n", +"s1=(-7-s0*(s0+1)*6)/19\n", +"disp(s1,'s1=')\n", +"disp(3+s1,'x1=')\n", +"s2=(-7-s1*(s1+1)*6-s1*(s1+1)*(s1+2))/19\n", +"disp(s2,'s2=')\n", +"x2=3+s2;\n", +"disp(x2,'Suggested Interpolation x(20)=');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.6: Chebyshev_Interpolating_Polynomial.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.6\n", +"//Chebyshev Interpolating Polynomial\n", +"//Page no. 407\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=1/(1+exp(-x))');\n", +"a=-2;b=2;n=3;\n", +"D=%pi/(2*n+2)\n", +"for k=0:n\n", +" t(k+1)=-cos(D*(2*k+1))\n", +" x(k+1)=((a+b)/2)+(b-a)*t(k+1)/2\n", +" y(k+1)=f(x(k+1))\n", +" C(k+1)=0\n", +"end\n", +"for j=0:n\n", +" for k=0:n\n", +" L=(2*k+1)*D\n", +" C(j+1)=C(j+1)+y(k+1)*cos(j*L)\n", +" end\n", +"end\n", +"C(1)=C(1)/(n+1);\n", +"for j=1:n\n", +" C(j+1)=2*C(j+1)/(n+1)\n", +"end\n", +"\n", +"x=poly(0,'x')\n", +"T(1)=1;T(2)=x;\n", +"for j=1:n-1\n", +" T(j+2)=2*x*T(j+1)-T(j)\n", +"end\n", +"P=C(1)*T(1)\n", +"for j=1:n\n", +" P=P+C(j+1)*T(j+1)\n", +"end\n", +"disp(P,'P3(x)=')\n", +"printf('\n\n\nNote : Book has Calculation errors in calculation of coefficients')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.7: Double_Interpolation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.7\n", +"//Double Interpolation\n", +"//Page no. 409\n", +"clc;close;clear;\n", +"\n", +"x=[0,1,2,3,4];\n", +"y=[0,1,2,3,4];\n", +"z=[0,1,8,27,64;1,3,11,31,69;4,7,16,37,76;9,13,23,45,85;16,21,32,55,96];\n", +"printf('y / x')\n", +"for i=1:5\n", +" printf('\t%i',x(i))\n", +"end\n", +"for i=1:5\n", +" printf('\n %i',y(i))\n", +" for j=1:5\n", +" printf('\t%i',z(j,i))\n", +" end\n", +"end\n", +"printf('\n\n\n')\n", +"for i=1:5\n", +" x=2.5;\n", +" x1=2;x2=3;\n", +" z1(1,i)=z(i,x1+1)+(z(i,x2+1)-z(i,x1+1))*(x-x1)/(x2-x1)\n", +"end\n", +"printf('Values of z at x=2.5:\n\n y')\n", +"for i=1:5\n", +" printf('\t%i',y(i))\n", +"end\n", +"printf('\n z')\n", +"for i=1:5\n", +" printf('\t%g',z1(i))\n", +"end\n", +"y=1.5;\n", +"y1=1;y2=2;\n", +"z2=z1(y1+1)+(z1(y2+1)-z1(y1+1))*(y-y1)/(y2-y1)\n", +"printf('\n\nValue of z at x=2.5 and y=1.5 : %g',z2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 12.8: Spline_Interpolation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 12.8\n", +"//Spline Interpolation\n", +"//Page no. 414\n", +"clc;close;clear;\n", +"\n", +"xi=[0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17];\n", +"yi=[0.1110,0.1234,0.1361,0.1491,0.1623,0.1759,0.1897,0.2038];\n", +"h=0.01;\n", +"\n", +"pi(1)=0;qi(1)=0;pi(8)=0;qi(8)=0;\n", +"for i=2:7\n", +" pi(i)=-1/(4+pi(i-1))\n", +" qi(i)=((6/h^2)*(yi(i+1)-2*yi(i)+yi(i-1))-qi(i-1))/(4+pi(i-1))\n", +"end\n", +"si2(8)=0;\n", +"si2(1)=0;si1(8)=0;\n", +"si1(1)=0;\n", +"for i=7:-1:2\n", +" si2(i)=pi(i)*si2(i+1)+qi(i)\n", +"end\n", +"for i=2:8\n", +" si1(i)=si1(i-1)+h*(si2(i)+si2(i-1))/2\n", +"end\n", +"printf('\n i\t xi\t fi\t pi\t\t qi\t\t si2\t\t si1')\n", +"printf('\n---------------------------------------------------------------------------------')\n", +"for i=1:8\n", +" printf('\n %i\t%g\t%g\t%f\t%f\t%f\t%f',i,xi(i),yi(i),pi(i),qi(i),si2(i),si1(i))\n", +"end\n", +"x=0.1325;\n", +"i=4;\n", +"s=yi(i)+(x-xi(i))*si1(i)+(si2(i)*(x-xi(i))^2)/2+((si2(i+1)-si2(i))/(xi(i+1)-xi(i)))*((x-xi(i))^3)/6\n", +"printf('\n\nSpline Interpolated Value of s(0.1325) is : %f',s)" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/13-Numerical_Differentiation.ipynb b/Numerical_Methods_Principles_by_Analysis/13-Numerical_Differentiation.ipynb new file mode 100644 index 0000000..7c54b30 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/13-Numerical_Differentiation.ipynb @@ -0,0 +1,398 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 13: Numerical Differentiation" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.1: Differentiation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.1\n", +"//Differentiation\n", +"//Page no. 420\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=x^2+5')\n", +"deff('y=f1(x,h)','y=(f(x+h)-f(x))/h')\n", +"h=0.01;x=2.4\n", +"d=f1(x,h)\n", +"d1=(f1(x+h,h)-f1(x))/h\n", +"printf('dy\n -- = %g\n dx',d)\n", +"printf('\n\n\n d2y\n --- = %g\n dx2',d1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.2: Calculation_of_x_coordinate_of_Minimum_Point.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.2\n", +"//Calculation of x-coordinate of Minimum Point \n", +"//Page no. 422\n", +"clc;close;clear;\n", +"\n", +"for i=1:7\n", +" for j=1:6\n", +" z(i,j)=0\n", +" end\n", +"end\n", +"h=0.2\n", +"printf(' x y d d2 d3 d4\n')\n", +"printf('--------------------------------------------------------------')\n", +"for i=1:7\n", +" z(i,1)=i/5;\n", +"end\n", +"z(1,2)=2.10022\n", +"z(2,2)=1.98730\n", +"z(3,2)=1.90940\n", +"z(4,2)=1.86672\n", +"z(5,2)=1.85937\n", +"z(6,2)=1.88755\n", +"z(7,2)=1.95147\n", +"for i=3:6\n", +" for j=1:9-i\n", +" z(j,i)=z(j+1,i-1)-z(j,i-1)\n", +" end\n", +"end\n", +"disp(z)\n", +"\n", +"s=poly(0,'s')\n", +"p=z(5,2);k=4;\n", +"for i=3:5\n", +" r=1;\n", +" for j=1:i-2\n", +" r=r*(s+(j-1))\n", +" end\n", +" r=r*z(k,i)/factorial(j);\n", +" k=k-1;\n", +" p=p+r;\n", +" \n", +"end\n", +"disp(p)\n", +"s=(-z(4,3)+z(3,4)/2)/z(3,4)\n", +"disp(s,'s=')\n", +"x=z(5,1)+s*h\n", +"disp(x,'x=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.3: Newton_Forward_Difference_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.3\n", +"//Newton's Forward Difference Formula\n", +"//Page no. 423\n", +"clc;close;clear;\n", +"printf(' x\t\t y\t\t d\t\t d2\t\t d3\t\t d4\n')\n", +"printf('------------------------------------------------------------------------------------------')\n", +"h=0.05;\n", +"z=[1.00,1.00000;1.05,1.02470;1.10,1.04881;1.15,1.07238;1.20,1.09544;1.25,1.11803;1.30,1.14018]\n", +"deff('y=f1(x,s)','y=(z(x,3)+(s-1/2)*z(x,4)+z(x,5)*(3*s^2-6*s+2)/6)/h')\n", +"deff('y=f2(x,s)','y=(z(x,4)+z(x,5)*(s-1))/h^2')\n", +"deff('y=f3(x,s)','y=z(x,5)/h^3')\n", +"for i=3:6\n", +" for j=1:9-i\n", +" z(j,i)=z(j+1,i-1)-z(j,i-1)\n", +" end\n", +"end\n", +"printf('\n')\n", +"for i=1:7\n", +" for j=1:6\n", +" if z(i,j)==0 then\n", +" printf(' \t')\n", +" else\n", +" printf('%.7f\t',z(i,j))\n", +" end\n", +" end\n", +" printf('\n')\n", +"end\n", +"s=poly(0,'s')\n", +"p=z(5,2);k=4;\n", +"for i=3:5\n", +" r=1;\n", +" for j=1:i-2\n", +" r=r*(s+(j-1))\n", +" end\n", +" r=r*z(k,i)/factorial(j);\n", +" k=k-1;\n", +" p=p+r;\n", +" \n", +"end\n", +"disp(p,'y(s) = ')\n", +"printf('\n\ny1(1) = %g',f1(1,0))\n", +"printf('\n\ny2(1) = %g',f2(1,0))\n", +"printf('\n\ny3(1) = %g',f3(1,0))\n", +"printf('\n\ny1(1.025) = %g',f1(1,0.5))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.4: Newton_Backward_Difference_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.4\n", +"//Newton's Backward Difference Formula\n", +"//Page no. 425\n", +"clc;close;clear;\n", +"printf(' x\t\t y\t\t d\t\t d2\t\t d3\t\t d4\n')\n", +"printf('------------------------------------------------------------------------------------------')\n", +"h=0.02;\n", +"z=[0.96,1.8025;0.98,1.7939;1.00,1.7851;1.02,1.7763;1.04,1.7673];\n", +"deff('y=f1(x,s)','y=(z(x,3)+(s+1/2)*z(x,4))/h')\n", +"for i=3:6\n", +" for j=1:7-i\n", +" z(j,i)=z(j+1,i-1)-z(j,i-1)\n", +" end\n", +"end\n", +"printf('\n')\n", +"for i=1:5\n", +" for j=1:6\n", +" if z(i,j)==0 then\n", +" printf(' \t')\n", +" else\n", +" printf('%.7f\t',z(i,j))\n", +" end\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\ny1(1) = %g',f1(2,0))\n", +"printf('\n\ny1(1.03) = %g',f1(4,0.5))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.5: Stirlings_Central_Difference_Derivatives.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.5\n", +"//Stirlings Central Difference Derivatives\n", +"//Page no. 426\n", +"clc;close;clear;\n", +"printf(' x\t\t y\t\t d\t\t d2\t\t d3\n')\n", +"printf('---------------------------------------------------------------------------')\n", +"h=0.01;s=0.5;\n", +"deff('y=f1(x,s)','y=((z(x,3)+z(x-1,3))/2+s*z(x-1,4)+(z(x-1,5)+z(x-2,5))*(3*s^2-1)/12)/h')\n", +"deff('y=f2(x,s)','y=(z(x-1,4))/h^2')\n", +"deff('y=f3(x,s)','y=(z(x-1,5)+z(x-2,5))/(2*h^3)')\n", +"z=[1.00,1.00000;1.01,1.00499;1.02,1.00995;1.03,1.01489;1.04,1.01980;1.05,1.02470;1.06,1.02956;1.07,1.03441;1.08,1.03923;1.09,1.04403;1.10,1.04881;1.11,1.05357;1.12,1.05830;1.13,1.06301;1.14,1.06771;1.15,1.07238;1.16,1.07703];\n", +"for i=3:5\n", +" for j=1:19-i\n", +" z(j,i)=z(j+1,i-1)-z(j,i-1)\n", +" end\n", +"end\n", +"printf('\n')\n", +"for i=1:17\n", +" for j=1:5\n", +" if z(i,j)==0 then\n", +" printf(' \t')\n", +" else\n", +" printf('%.7f\t',z(i,j))\n", +" end\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\n\ny1(1.125) = %g (exact value = 0.4771404)',f1(13,0.5))\n", +"printf('\n\ny2(1.125) = %g (exact value = -0.20951)',f2(13,0.5))\n", +"printf('\n\ny3(1.125) = %g (exact value = 0.27935)',f3(13,0.5))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.6: Extrapolatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.6\n", +"//Extrapolation\n", +"//Page no. 430\n", +"clc;close;clear;\n", +"x=[-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8];\n", +"y=[0.2019,0.30119,0.44933,0.67032,1,1.49182,2.22554,3.32012,4.95303]\n", +"for i=1:4\n", +" printf('\nh = %g\n',x(10-i))\n", +" y1=(y(10-i)-y(i))/(2*x(10-i))\n", +" printf('f1(0) = %g\n\n',y1)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.7: Richardson_Extrapolation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.7\n", +"//Richardson Extrapolation\n", +"//Page no. 431\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=exp(2*x)')\n", +"e=10^-4;h=0.8;\n", +"D1=0;\n", +"for i=1:4\n", +" printf('\n')\n", +" for j=1:i\n", +" if j==1 then\n", +" D(i,j)=(f(h)-f(-h))/(2*h)\n", +" else\n", +" D(i,j)=D(i,j-1)+(D(i,j-1)-D(i-1,j-1))/(2^(2*(j-1))-1)\n", +" end\n", +" printf('%g\t\t',D(i,j))\n", +" end\n", +" h=h/2\n", +"end\n", +"printf('\n\n\t\t\t\t\t\t 2x\nHence, the derivative of the function y = f(x) = e at x=0 is D(3,3) = %g',D(i,j))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 13.8: Applicatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 13.8\n", +"//Application\n", +"//Page no. 433\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=2/x^2')\n", +"a=1;b=2;a1=1;b1=0;\n", +"N=4;\n", +"h=(b-a)/(N+1);\n", +"for j=1:N\n", +" s(j)=f(a+j*h)\n", +"end\n", +"for i=1:N\n", +" for j=1:N\n", +" if abs(i-j)==1 then\n", +" A(i,j)=-1\n", +" end\n", +" if i==j then\n", +" A(i,j)=2+s(i)*h^2\n", +" end\n", +" end\n", +" if i==1 then\n", +" k(i,1)=s(i)+a1/h^2\n", +" elseif i==N\n", +" k(i,1)=s(i)+b1/h^2\n", +" else\n", +" k(i,1)=s(i)\n", +" end\n", +"end\n", +"disp(A,'A = ')\n", +"disp(k,'k = ')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/14-Numerical_Integration.ipynb b/Numerical_Methods_Principles_by_Analysis/14-Numerical_Integration.ipynb new file mode 100644 index 0000000..a8bb9cc --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/14-Numerical_Integration.ipynb @@ -0,0 +1,587 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 14: Numerical Integration" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.10: Spline_Integration_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.10\n", +"//Spline Integration Method\n", +"//Page no. 478\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=sind(%pi*x)')\n", +"deff('y=f1(x,h)','y=(f(x+h)-f(x))/h')\n", +"h=0.01;\n", +"n=2;h=0.5;a=0;b=1;\n", +"disp(integrate('f(x)','x',0,1),'I = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.11: Trapezoidal_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.1\n", +"//Trapezoidal Rule\n", +"//Page no 440\n", +"clc;clear;close;\n", +"x1=1.46\n", +"for i=1:6\n", +" x(1,i)=x1+i/100\n", +"end\n", +"y=[3.86,3.90,3.96,4.02,4.06,4.12]\n", +"\n", +"//trapezoidal rule\n", +"S=0;\n", +"h=(x(6)-x1)/6\n", +"for i=1:6\n", +" if(i==1 | i==6)\n", +" S=S+y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +"end\n", +"S=S*h/2\n", +"printf('\n I = %g',S)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.14: Trapezoidal_and_Simpsons_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.14\n", +"//Trapezoidal and Simpsons Rule\n", +"//Page no. 486\n", +"clc;close;clear;\n", +"\n", +"x(1)=0.5;y(1)=0.5;h=0.25\n", +"for i=2:3\n", +" x(i)=x(i-1)+h\n", +" y(i)=y(i-1)+h\n", +"end\n", +"printf(' y/x\t|\t%g\t%g\t%g',x(1),x(2),x(3))\n", +"printf('\n--------|---------------------------')\n", +"for i=1:3\n", +" printf('\n%g\t|\t',y(i))\n", +" for j=1:3\n", +" z(i,j)=x(j)*y(i)\n", +" printf('%g\t',z(i,j))\n", +" end\n", +"end\n", +"\n", +"//trapezoidal rule\n", +"s=0;\n", +"for i=1:3\n", +" for j=1:3\n", +" if i==1 & j==1 then\n", +" s=s+z(i,j)\n", +" elseif i==3 & j==3\n", +" s=s+z(i,j)\n", +" else\n", +" s=s+2*z(i,j)\n", +" end\n", +" end\n", +"end\n", +"s=(s*(h^2))/4\n", +"printf('\n\n')\n", +"disp(s,'Trapezoidal Rule Sum = ')\n", +"printf('\n\n')\n", +"//simpsons rule\n", +"s=0;\n", +"for i=1:3\n", +" for j=1:3\n", +" if i/2-int(i/2)==0 & j/2-int(j/2)==0 then\n", +" s=s+16*z(i,j)\n", +" elseif i/2-int(i/2)~=0 & j/2-int(j/2)~=0\n", +" s=s+z(i,j)\n", +" else\n", +" s=s+4*z(i,j)\n", +" end\n", +" end\n", +"end\n", +"s=(s*(h^2))/9\n", +"disp(s,'Simpsons Rule Sum = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.15: Trapezoidal_and_Simpsons_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.15\n", +"//Trapezoidal and Simpsons Rule\n", +"//Page no. 487\n", +"clc;close;clear;\n", +"\n", +"x(1)=0;y(1)=0;h=0.25\n", +"for i=2:5\n", +" x(i)=x(i-1)+h\n", +" y(i)=y(i-1)+h\n", +"end\n", +"printf(' y/x\t|\t%g\t%g\t%g\t%g\t%g',x(1),x(2),x(3),x(4),x(5))\n", +"printf('\n--------|-------------------------------------------')\n", +"for i=1:5\n", +" printf('\n%g\t|\t',y(i))\n", +" for j=1:5\n", +" z(i,j)=x(j)*y(i)\n", +" printf('%g\t',z(i,j))\n", +" end\n", +"end\n", +"\n", +"//trapezoidal rule\n", +"s=0;\n", +"for i=1:5\n", +" for j=1:5\n", +" if i==1 & j==1 then\n", +" s=s+z(i,j)\n", +" elseif i==5 & j==5\n", +" s=s+z(i,j)\n", +" else\n", +" s=s+2*z(i,j)\n", +" end\n", +" end\n", +"end\n", +"s=(s*(h^2))/4\n", +"printf('\n\n')\n", +"disp(s,'Trapezoidal Rule Sum = ')\n", +"printf('\n\n')\n", +"\n", +"//simpsons rule\n", +"s=0;\n", +"for i=1:5\n", +" for j=1:5\n", +" if i/2-int(i/2)==0 & j/2-int(j/2)==0 then\n", +" if i==j then\n", +" s=s+16*z(i,j)\n", +" else\n", +" s=s+4*z(i,j)\n", +" end\n", +" \n", +" elseif i/2-int(i/2)~=0 & j/2-int(j/2)~=0\n", +" s=s+z(i,j)\n", +" else\n", +" s=s+4*z(i,j)\n", +" end\n", +" end\n", +"end\n", +"s=(s*(h^2))/9\n", +"disp(s,'Simpsons Rule Sum = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.16: Multiple_Integration_with_Variable_Limits.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.16\n", +"//Multiple Integration with Variable Limits\n", +"//Page no. 491\n", +"clc;close;clear;\n", +"\n", +"deff('z=f(x)','z=x+1')\n", +"deff('z=f1(y)','z=(y+1)^3*(y+3)^2')\n", +"s=5/9*f(-sqrt(3/5))+8/9*f(0)+5/9*f(sqrt(3/5))\n", +"s=s*5/9*f1(-sqrt(3/5))+8/9*f1(0)+5/9*f1(sqrt(3/5))\n", +"s=s/256;\n", +"disp(s,'I = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.18: Integration.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.18\n", +"//Integration\n", +"//Page no. 494\n", +"clc;close;clear;\n", +"\n", +"s=integrate('x^2*sin(x^2)','x',0,1)\n", +"disp(s,'I = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.19: Integration.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.19\n", +"//Integration\n", +"//Page no. 494\n", +"clc;close;clear;\n", +"\n", +"s=integrate('sin(t)/t','t',1,999)\n", +"disp(s,'I = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.2: Simpsons_1_3rd_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.2\n", +"//Simpsons 1/3rd Rule\n", +"//Page no 442\n", +"clc;clear;close;\n", +"x(1,1)=0\n", +"for i=2:9\n", +"\n", +" x(1,i)=x(1,1)+(i-1)*10\n", +"end\n", +"y=[30,31.63,33.44,35.47,37.75,40.33,43.29,46.69,50.67]\n", +"\n", +"//trapezoidal rule\n", +"S=0;\n", +"h=(x(9)-x(1))/8\n", +"for j=1:9\n", +" S=0;\n", +" for i=1:j\n", +" if(i==1 | i==j)\n", +" S=S+y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +" end\n", +" S=S*h/2\n", +" printf('\n Velocity at t (%i) = %.2f',x(j),S)\n", +" y1(j)=S\n", +"end\n", +"\n", +"y1(1)=0;\n", +"//Simpsons 1/3rd Rule\n", +"S=0;\n", +"h=(x(9)-x(1))/8\n", +"for i=1:9\n", +" if(i==1 | i==9)\n", +" S=S+y1(i)\n", +" elseif(((i)/2)-fix((i)/2)==0)\n", +" S=S+4*y1(i)\n", +" else\n", +" S=S+2*y1(i)\n", +" end\n", +"end\n", +"S=S*h/3;\n", +"S=S/1000\n", +"printf('\n\nSimpsons 1/3rd Rule Sum = %g km',S)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.3: Trapezoidal_Rule_and_Simpsons_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.3\n", +"//Trapezoidal Rule and Simpsons Rule\n", +"//Page no. 442\n", +"clc;close;clear;\n", +"n=2;a=0;b=1;\n", +"h=(b-a)/n\n", +"deff('y=f(x)','y=1/(1+x)')\n", +"for i=0:2\n", +" x(i+1)=i/2;\n", +" y(i+1)=f(x(i+1))\n", +"end\n", +"printf('xi\t ')\n", +"for i=1:3\n", +" printf('%g\t ',x(i))\n", +"end\n", +"printf('\n yi\t')\n", +"for i=1:3\n", +" printf('1/%g\t',1+(i-1)/2)\n", +"end\n", +"\n", +"//trapezoidal rule\n", +"S=0;\n", +"for i=1:3\n", +" if(i==1 | i==3)\n", +" S=S+y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +"end\n", +"S=S*h/2\n", +"printf('\n\nTrapezoidal Rule Sum = %g',S)\n", +"\n", +"//Simpsons 1/3rd Rule\n", +"S=0;\n", +"for i=1:3\n", +" if(i==1 | i==3)\n", +" S=S+y(i)\n", +" elseif(((i)/2)-fix((i)/2)==0)\n", +" S=S+4*y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +"end\n", +"S=S*h/3\n", +"printf('\n\nSimpsons 1/3rd Rule Sum = %g',S)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.5: Romberg_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.5\n", +"//Romberg Method\n", +"//Page no. 457\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=1/(1+x)')\n", +"\n", +"h=[0.5,0.25,0.125]\n", +"for k=1:3\n", +" for i=0:h(k):1\n", +" x(i/h(k)+1)=i;\n", +" y(i/h(k)+1)=f(x(i/h(k)+1))\n", +" end\n", +" n=1+(1/h(k))\n", +" //trapezoidal rule\n", +" S=0;\n", +" for i=1:n\n", +" if(i==1 | i==n)\n", +" S=S+y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +" end\n", +" S=S*h(k)/2\n", +" printf('\n\nI(%g) = %g',h(k),S)\n", +" z(2*k-1,1)=S\n", +"end\n", +"for i=2:3\n", +" for k=1:4-i\n", +" z(k*2+i-2,i)=z(2*k-1+i,i-1)+(z(2*k-1+i,i-1)-z(2*k-3+i,i-1))/3\n", +"end\n", +"end\n", +"\n", +"printf('\n\n')\n", +"disp(z,'The Table of values:')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.7: Gaussian_Quadrature_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.7\n", +"//Gaussian Quadrature Formula\n", +"//Page no. 463\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=cos(x)*log(x)')\n", +"s=0;\n", +"for i=0:2:2000\n", +" s=s+integrate('((-1)^(i/2))*(x^i)/factorial(i)*log(x)','x',0,1)\n", +"end\n", +"disp(s,'Till 1000 terms .... I =')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.8: Gauss_Legendre_Two_Point_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.8\n", +"//Gauss Legendre Two Point Rule\n", +"//Page no. 472\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=1/(x+3)')\n", +"s=integrate('f(x)','x',-1,1)\n", +"printf('By Direct Method, I = %g',s)\n", +"s=f(-1/sqrt(3))+f(1/sqrt(3))\n", +"printf('\n\n By Gauss-Legendre 2 point rule, I = %g',s)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 14.9: Gauss_Legendre_Three_Point_Rule.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 14.9\n", +"//Gauss Legendre Three Point Rule\n", +"//Page no. 473\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=1/(x+3)')\n", +"s=integrate('f(x)','x',-1,1)\n", +"printf('By Direct Method, I = %g',s)\n", +"s=5/9*f(-sqrt(3/5))+8/9*f(0)+5/9*f(sqrt(3/5))\n", +"printf('\n\n By Gauss-Legendre 3 point rule, I = %g',s)" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/15-Numerical_Solutions_of_Ordinary_Differential_Equations_Initial_Value_Problem.ipynb b/Numerical_Methods_Principles_by_Analysis/15-Numerical_Solutions_of_Ordinary_Differential_Equations_Initial_Value_Problem.ipynb new file mode 100644 index 0000000..6ec0ca2 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/15-Numerical_Solutions_of_Ordinary_Differential_Equations_Initial_Value_Problem.ipynb @@ -0,0 +1,948 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 15: Numerical Solutions of Ordinary Differential Equations Initial Value Problem" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.10: Heun_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.10\n", +"//Heun Method\n", +"//Page no. 517\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=y*2/x')\n", +"y=2;\n", +"h=0.25;\n", +"for i=1:4\n", +" x=1+(i-1)*h\n", +" x1=x+h\n", +" ye=y+h*f(x,y)\n", +" y=y+h*(f(x,y)+f(x1,ye))/2\n", +" printf('\n y(%g) = %g\n',x1,y)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.11: Midpoint_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.11\n", +"//Midpoint Method\n", +"//Page no. 518\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=y+x')\n", +"y=1;\n", +"h=0.2;\n", +"printf('i\txi\tyi\tslope1\tslope2\ty(i+1)\n-----------------------------------------------\n')\n", +"for i=1:3\n", +" x=(i-1)*h\n", +" s1=f(x,y);\n", +" s2=f(x+h/2,y+s1*h/2);\n", +" printf(' %i\t%g\t%g\t%g\t%g',i-1,x,y,s1,s2)\n", +" y=y+s2*h;\n", +" printf('\t%g\n',y)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.12: Modified_Midpoint_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.12\n", +"//Modified Midpoint Method\n", +"//Page no. 519\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=y+x')\n", +"y=1;\n", +"h=0.2;\n", +"Z(1)=y;\n", +"Z(2)=Z(1)+h*f(0,Z(1))\n", +"printf('Z(%i) = %g',1,Z(2))\n", +"for i=2:5\n", +" x=(i-1)*h;\n", +" Y(i-1)=(Z(i)+Z(i-1)+h*f(x,Z(i)))/2\n", +" Z(i+1)=Z(i-1)+2*h*f(x,Z(i))\n", +" printf('\n Y(%i) = %g\n\n\n Z(%i) = %g',i-1,Y(i-1),i,Z(i+1))\n", +"end\n", +"printf('\n\n\n y4 = %g',(4*Y(4)-Y(2))/3)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.13: Single_Step_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.13\n", +"//Single Step Method\n", +"//Page no. 521\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^2')\n", +"deff('y=f1(x)','y=1/(1-x)')\n", +"y=1;h=0.2;\n", +"printf('n\tXn\tYn (by single-step method)\tYn (computed)\n-----------------------------------------------------------------\n')\n", +"for i=1:6\n", +" x=(i-1)*h\n", +" if i<6 then\n", +" printf(' %i\t%.2f\t%.5f\t\t\t\t%.5f\n',i-1,x,y,f1(x))\n", +" else\n", +" printf(' %i\t%.2f\t%.5f\t\t\t\t \n',i-1,x,y)\n", +" end\n", +" y=y+h*f(y);\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.14: Second_Order_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.14\n", +"//Second Order Runge Kutta Method\n", +"//Page no. 525\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;\n", +"//simple runge kutta method\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h,y+K1);\n", +"y1=y+(K1+K2)/2\n", +"printf('\ny(1.1) by simple runge kutta method = %g\n\n',y1)\n", +"\n", +"//euler cauchy method\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/2,y+K1/2);\n", +"y1=y+(K1+K2)\n", +"printf('y(1.1) by euler cauchy method = %g\n\n',y1)\n", +"\n", +"//optimal method\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+2*h/3,y+2*K1/3);\n", +"y1=y+(K1+3*K2)/4\n", +"printf('y(1.1) by optimal method = %g',y1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.15: Third_Order_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.15\n", +"//Third Order Runge Kutta Method\n", +"//Page no. 526\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;\n", +"//scheme 1\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/2,y+K1/2);\n", +"K3=h*f(x+h/2,y-K1+2*K2);\n", +"y1=y+(K1+4*K2+K3)/6\n", +"printf('\ny(1.1) by scheme 1 = %g\n\n',y1)\n", +"\n", +"//scheme 2\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/3,y+K1/3);\n", +"K3=h*f(x+2*h/3,y+2*K2/3);\n", +"y1=y+(K1+3*K3)/4\n", +"printf('\ny(1.1) by scheme 2 = %.7f\n\n',y1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.16: Fourth_Order_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.16\n", +"//Fourth Order Runge Kutta Method\n", +"//Page no. 528\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/2,y+K1/2);\n", +"K3=h*f(x+h/2,y+K2/2);\n", +"K4=h*f(x+h,y+K3);\n", +"disp(K4,'K4 =',K3,'K3 =',K2,'K2 =',K1,'K1 =')\n", +"y1=y+(K1+2*K2+2*K3+K4)/6\n", +"printf('\ny(1.1) = %.8f\n\n',y1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.17: New_Variant_of_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.17\n", +"//New Variant of Runge Kutta Method\n", +"//Page no. 530\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/2,y+K1/2);\n", +"K3=h*f(x+h/2,y+K2/2);\n", +"K4=h*f(x+h,y+K3);\n", +"K5=h*f(x+3*h/4,y+(5*K1+7*K2+13*K3-K4)/32)\n", +"disp(K5,'K5 =',K4,'K4 =',K3,'K3 =',K2,'K2 =',K1,'K1 =')\n", +"y1=y+(K1+2*K2+2*K3+K5)/6\n", +"printf('\ny(1.1) = %.8f\n\n',y1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.18: Runge_Kutta_Merson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.18\n", +"//Runge Kutta Merson Method\n", +"//Page no. 532\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x+y')\n", +"y=1;x=0;h=0.1;\n", +"printf('n\t Xn\t Yn\t K1\t K2\t K3\t K4\t K5\tY(n+1)\n----------------------------------------------------------------------')\n", +"for i=0:14\n", +" K1=h*f(x,y);\n", +"K2=h*f(x+h/3,y+K1/3);\n", +"K3=h*f(x+h/3,y+(K1+K2)/6);\n", +"K4=h*f(x+h/2,y+(K1+3*K3)/8);\n", +"K5=h*f(x+h,y+(K1-3*K3+4*K4)/2)\n", +"y1=y+(K1+4*K4+K5)/6\n", +"printf('\n %i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f',i,x,y,K1,K2,K3,K4,K5,y1)\n", +"y=y1;\n", +"x=x+h;\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.19: Runge_Kutta_Fehlberg_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.19\n", +"//Runge Kutta Fehlberg Method\n", +"//Page no. 535\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;\n", +"K1=h*f(x,y);\n", +"K2=h*f(x+h/4,y+K1/4);\n", +"K3=h*f(x+3*h/8,y+3*(K1+3*K2)/32);\n", +"K4=h*f(x+12*h/13,y+1932*K1/2197-7200*K2/2197+7296*K3/2197);\n", +"K5=h*f(x+h,y+439*K1/216-8*K2+3680*K3/513-845*K4/4104)\n", +"K6=h*f(x+h/2,y-8*K1/27+2*K2-3544*K3/2565+1859*K4/4104-11*K5/40)\n", +"disp(K6,'K6 =',K5,'K5 =',K4,'K4 =',K3,'K3 =',K2,'K2 =',K1,'K1 =')\n", +"y1=y+(25*K1/216+1408*K3/2565+2197*K4/4104-K5/5)\n", +"y11=y+(16*K1/135+6656*K3/12825+28561*K4/56430-9*K5/50+2*K6/55)\n", +"printf('\ny(1.1) = %.9f\n\n',y1)\n", +"printf('\ny~(1.1) = %.9f\n\n',y11)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.1: Ordinary_Differential_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.1\n", +"//Ordinary Differential Equation\n", +"//Page no. 503\n", +"clc;clear;close;\n", +"s=log(2)/log(1.02)\n", +"disp(s,'Time Taken = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.20: Carp_Karp_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.20\n", +"//Carp Karp Runge Kutta Method\n", +"//Page no. 537\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;printf('\n')\n", +"U=[0,1/5,3/10,3/5,1,7/8];\n", +"v=[0,0,0,0,0;1/5,0,0,0,0;3/40,9/40,0,0,0;3/10,-9/10,6/5,0,0;-11/54,5/2,-70/27,35/27,0;1631/55296,175/512,575/13824,44275/110592,253/4096];\n", +"a=[37/378,0,250/621,125/594,0,512/1771];\n", +"a1=[2825/27648,0,18575/48384,13525/55296,277/14336,1/4];\n", +"for l=1:5\n", +" K(1)=h*f(x,y);\n", +"for i=2:6\n", +" k=0;\n", +" for j=1:i-1\n", +" k=k+v(i,j)*K(j)\n", +" end\n", +" K(i)=h*f(x+U(i)*h,y+k)\n", +"end\n", +"k=0;\n", +"for i=1:6\n", +" k=k+a(i)*K(i)\n", +"end\n", +"y1=y+k;\n", +"k=0;\n", +"for i=1:6\n", +" k=k+a1(i)*K(i)\n", +"end\n", +"y11=y+k;\n", +"for i=1:6\n", +" printf('K%i = %.9f\n',i,K(i))\n", +"end\n", +"printf('\ny(1.1) = Y%i = %.9f\n',l,y1)\n", +"printf('y~(1.1) = Y%i~ = %.9f\n',l,y11)\n", +"y=y1;\n", +"printf('\n\n\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.21: Implicit_Runge_Kutta_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.21\n", +"//Implicit Runge Kutta Method\n", +"//Page no. 539\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x-y')\n", +"y=1;x=1;h=0.1;printf('\n')\n", +"U=[0,1/5];\n", +"v=[0,0;1/2,1/2];\n", +"a2=1;\n", +"K(1)=h*f(x,y);\n", +"K(2)=(x+h/2-y-K(1)/2)/(1/h-1/2)\n", +"y1=y+(K(1)+a2*K(2))\n", +"printf('\ny(1.1) = %.9f\n\n',y1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.22: Linear_Multi_Step_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.22\n", +"//Linear Multi Step Method\n", +"//Page no. 540\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x+y')\n", +"y(1)=1;y(2)=1;x(1)=0;h=0.1;\n", +"printf('n\tXn\t\tYn\t\tfn\n-----------------------------------------------\n 0\t%g\t\t%.3f\t\t%.3f\n',x(1),y(1),f(x(1),y(1)));\n", +"for i=2:11\n", +" x(i)=(i-1)*h;\n", +" y(i+1)=(-y(i)-y(i-1)+h*(f(x(i),y(i))+f(x(i-1),y(i-1))))/2;\n", +" printf(' %i\t%.3f\t\t%.3f\t\t%.3f\n',i-1,x(i),y(i),f(x(i),y(i)))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.23: Milne_Simpson_Predictor_Corrector_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.23\n", +"//Milne Simpson Predictor Corrector Method\n", +"//Page no. 544\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=y+exp(x)')\n", +"h=0.5;\n", +"y=[1,1.824,3.718,7.722]\n", +"for i=1:4\n", +" x=(i-1)*h;\n", +" f1(i)=f(x,y(i));\n", +" printf('\nf%i = %g',i-1,f1(i))\n", +"end\n", +"y41=y(1)+4*h*(2*f1(4)-f1(3)+2*f1(2))/3\n", +"f4=f(x+h,y41);\n", +"y4=y(3)+h*(f4+4*f1(4)+f1(3))/3\n", +"printf('\n\n\nPredictor = %.9f\n\n',y41)\n", +"printf('Evaluator = %.9f\n\n',f4)\n", +"printf('Corrector = %.9f',y4)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.24: Improved_Milne_Simpson_Predictor_Corrector_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.24\n", +"//Improved Milne Simpson Predictor Corrector Method\n", +"//Page no. 546\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x,y)','y=y-x^2')\n", +"y(1)=1;h=0.25;x=0;\n", +"printf('n\tXn\tYn\tfn\tY`n\tYn\tY`n+1\tm(n+1)\tv(n+1)\n------------------------------------------------------------------------\n')\n", +"f1(1)=f(x,y(1));\n", +"for i=1:3\n", +" K1=h*f(x,y(i));\n", +" K2=h*f(x+2*h/3,y(i)+2*K1/3);\n", +" y(i+1)=y(i)+(K1+3*K2)/4\n", +" printf(' %i\t%.3f\t%.3f\t%.3f\n',i-1,x,y(i),f1(i))\n", +" x=x+h\n", +" f1(i+1)=f(x,y(i+1))\n", +"end\n", +"Y31=0\n", +"for i=3:10\n", +" Y41=y(i-2)+4*h*(2*f1(4)-f1(3)+2*f1(2))/3 //predictor\n", +" m4=Y41+28*(y(i+1)-Y31)/29 //modifier\n", +" v4=f(x+h,m4) //evaluator\n", +" Y4=y(i)+h*(v4+4*f1(4)+f1(3))/3 //corrector\n", +" printf(' %i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',i,x,y(i+1),f1(4),Y31,y(i+1),Y41,m4,v4)\n", +" y(i+2)=Y4\n", +" Y31=Y41;\n", +" f1(2)=f1(3);\n", +" f1(3)=f1(4);\n", +" f1(4)=f(x+h,y(i+2))\n", +" x=x+h\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.25: Hamming_Predictor_Corrector_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.25\n", +"//Hamming Predictor Corrector Method\n", +"//Page no. 548\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x,y)','y=y-x^2')\n", +"y(1)=1;h=0.25;x=0;\n", +"printf('n\tXn\tYn\tfn\tY`n\tYc(n)\tY`n+1\tm(n+1)\tv(n+1)\tYc(n+1)\n-----------------------------------------------------------------------------------------\n')\n", +"f1(1)=f(x,y(1));\n", +"for i=1:3\n", +" K1=h*f(x,y(i));\n", +" K2=h*f(x+2*h/3,y(i)+2*K1/3);\n", +" y(i+1)=y(i)+(K1+3*K2)/4\n", +" printf(' %i\t%.3f\t%.3f\t%.3f\n',i-1,x,y(i),f1(i))\n", +" x=x+h\n", +" f1(i+1)=f(x,y(i+1))\n", +"end\n", +"Y31=y(4);Yc=0\n", +"for i=3:10\n", +" Y41=y(i-2)+4*h*(2*f1(4)-f1(3)+2*f1(2))/3 //predictor\n", +" m4=Y41+112*(Y31-Yc)/121 //modifier\n", +" v4=f(x+h,m4) //evaluator\n", +" Y4c=(9*y(i+1)-y(i-1))/8+3*h*(v4+2*f1(4)-f1(3))/8 //corrector\n", +" Y4=Y4c+9*(Y41-Y4c)/121 //final value\n", +" printf(' %i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',i,x,y(i+1),f1(4),Y31,Yc,Y41,m4,v4,Y4c)\n", +" y(i+2)=Y4\n", +" Y31=Y41;\n", +" f1(2)=f1(3);\n", +" f1(3)=f1(4);\n", +" f1(4)=f(x+h,y(i+2))\n", +" Yc=Y4c\n", +" x=x+h\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.26: Multi_Valued_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.26\n", +"//Multi Valued Method\n", +"//Page no. 553\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x,y)','y=2*x^2-y')\n", +"h=0.1;x=0;y=-1;\n", +"deff('y=f2(x,y)','y=4*x-f1(x,y)')\n", +"deff('y=f3(x,y)','y=4-f2(x,y)')\n", +"B=[1,1,1,1;0,1,2,3;0,0,1,3;0,0,0,1];\n", +"y0=[y;h*f1(x,y);h^2*f2(x,y)/2;h^3*f3(x,y)/6]\n", +"y01=y0;\n", +"r=[0;1;3/4;1/6]\n", +"\n", +"disp(r,'If r = ')\n", +"printf('\n\n-------------------------------------------------------------------------\nx = 0\t\t\tx = 0.1\t\t\t\tx = 0.2\n\t')\n", +"for i=1:2\n", +" y11=B*y01\n", +" s(i)=h*(f1(x+h,y11(1)))-y11(2)\n", +" y1=y11+s(i)*r\n", +" if i==2 then\n", +" break\n", +" end\n", +" y2=y1;\n", +" y22=y11;\n", +" y01=y1\n", +"end\n", +"printf('\t (s = %.5g)\t\t (s = %.9f)\n-------------------------------------------------------------------------\n Y0\t\t Y`i\t\t Y1\t\t Y`2\t\t Y2\n-------------------------------------------------------------------------\n',s(1),s(2))\n", +"for i=1:4\n", +" printf('%.5f \t%.5f \t%.5f \t%.5f \t%.5f\n',y0(i),y22(i),y2(i),y11(i),y1(i))\n", +"end\n", +"y0=[y;h*f1(x,y);h^2*f2(x,y)/2;h^3*f3(x,y)/6]\n", +"y01=y0;\n", +"r=[5/12;1;3/4;1/6]\n", +"disp(r,'If r = ')\n", +"printf('\n\n-------------------------------------------------------------------------\nx = 0\t\t\tx = 0.1\t\t\t\tx = 0.2\n\t')\n", +"for i=1:2\n", +" y11=B*y01\n", +" s(i)=h*(f1(x+h,y11(1)))-y11(2)\n", +" y1=y11+s(i)*r\n", +" if i==2 then\n", +" break\n", +" end\n", +" y2=y1;\n", +" y22=y11;\n", +" y01=y1\n", +"end\n", +"printf('\t (s = %.5g)\t\t (s = %.9f)\n-------------------------------------------------------------------------\n Y0\t\t Y`i\t\t Y1\t\t Y`2\t\t Y2\n-------------------------------------------------------------------------\n',s(1),s(2))\n", +"for i=1:4\n", +" printf('%.5f \t%.5f \t%.5f \t%.5f \t%.5f\n',y0(i),y22(i),y2(i),y11(i),y1(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.27: First_order_ODE.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.27\n", +"//First order ODE\n", +"//Page no. 558\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x,y1,y2)','y=y1*y2+x')\n", +"deff('y=f2(x,y1,y2)','y=y1-x')\n", +"h=0.2;x=0;y1=0;y2=1;\n", +"//heun method\n", +"printf('Heun Method:\n\n x\ty1\ty2\n-------------------------\n')\n", +"Y=[y1;y2]\n", +"for i=1:8\n", +" \n", +" F=[f1(x,Y(1),Y(2));f2(x,Y(1),Y(2))]\n", +" Y1=Y+h*F\n", +" x=x+h;\n", +" F1=[f1(x,Y1(1),Y1(2));f2(x,Y1(1),Y1(2))]\n", +" Y=Y+(h/2)*(F+F1)\n", +" printf(' %g\t%.3f\t%.3f\n',x-h,Y(1),Y(2))\n", +" \n", +"end\n", +"\n", +"//classical runge kutta method\n", +"printf('\n\n\nClassical Runge Kutta Method:\n\n n\tx\tYn\tK1\tK2\tK3\tK4\tY(n+1)\n-----------------------------------------------------------------\n')\n", +"Y=[y1;y2];x=0;\n", +"for i=1:6\n", +" K1=h*[f1(x,Y(1),Y(2));f2(x,Y(1),Y(2))]\n", +" K2=h*[f1(x+h/2,Y(1)+K1(1)/2,Y(2)+K1(2)/2);f2(x+h/2,Y(1)+K1(1)/2,Y(2)+K1(2)/2)]\n", +" K3=h*[f1(x+h/2,Y(1)+K2(1)/2,Y(2)+K2(2)/2);f2(x+h/2,Y(1)+K2(1)/2,Y(2)+K2(2)/2)]\n", +" K4=h*[f1(x+h,Y(1)+K3(1),Y(2)+K3(2));f2(x+h,Y(1)+K3(1),Y(2)+K3(2))]\n", +" Y1=Y+(K1+2*K2+2*K3+K4)/6\n", +" printf(' %i\t%.2f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n\t\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n---------------------------------------------------------------\n',i-1,x,Y(1),K1(1),K2(1),K3(1),K4(1),Y1(1),Y(2),K1(2),K2(2),K3(2),K4(2),Y1(2))\n", +" Y=Y1;\n", +" x=x+h\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.28: Differential_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.28\n", +"//Differential Equation\n", +"//Page no. 562\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x,y)','y=2*y^2/(1+x)')\n", +"h=0.1;z(1)=-1;\n", +"for i=1:11\n", +" printf('\nZ(%g) = %g\n',(i-1)/10,z(i))\n", +" z(i+1)=z(i)+h*f((i-1)/10,z(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.6: Taylor_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.6\n", +"//Taylor Method\n", +"//Page no. 510\n", +"clc;clear;close;\n", +"\n", +"deff('y=f1(x,y)','y=x^2+y^2')\n", +"deff('y=f2(x,y)','y=2*x+2*y*f1(x,y)')\n", +"deff('y=f3(x,y)','y=2+2*f1(x,y)^2+2*y*f2(x,y)')\n", +"deff('y=f4(x,y)','y=6*f1(x,y)*f2(x,y)+2*y*f3(x,y)')\n", +"h=0.2;\n", +"for l=1:2\n", +" a=0;y=0;x=0;\n", +" printf('\n---------------\nh = %g\n---------------\n',h)\n", +" for i=1:4\n", +" x=a+(i-1)*h\n", +" k=0;\n", +" for j=1:4\n", +" if j==1 then\n", +" k=k+(h^j)*f1(x,y)/factorial(j)\n", +" elseif j==2\n", +" k=k+(h^j)*f2(x,y)/factorial(j)\n", +" elseif j==3\n", +" k=k+(h^j)*f3(x,y)/factorial(j)\n", +" else\n", +" k=k+(h^j)*f4(x,y)/factorial(j)\n", +" end\n", +"end\n", +"y=y+k;\n", +"printf('\nx = %g\n\ny(%g) = %g\n\n',x,x+0.2,y)\n", +"end\n", +"h=h+0.2;\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.7: Picard_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.7\n", +"//Picard Method\n", +"//Page no. 511\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x^2+y^2')\n", +"y(1)=0;\n", +"for i=1:2\n", +" y(i+1)=y(1)+integrate('f(x,y(i))','x',0,i/10)\n", +" printf('\n y(%g) = %g\n',i/10,y(i+1))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.8: Euler_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.8\n", +"//Euler Method\n", +"//Page no. 513\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x+y')\n", +"y(1)=1;\n", +"h=0.1;\n", +"for i=1:6\n", +" printf('\ny(%g) = %g\n',(i-1)/10,y(i))\n", +" y(i+1)=y(i)+h*f((i-1)/10,y(i))\n", +" \n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 15.9: Trapezium_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 15.9\n", +"//Trapezium Method\n", +"//Page no. 516\n", +"clc;clear;close;\n", +"deff('y=f(x,y)','y=x*y^2')\n", +"y=1;\n", +"h=0.2;\n", +"y2=poly(0,'y2')\n", +"for i=1:2\n", +" x=(i-1)*h;\n", +" x1=x+h\n", +" y1=roots(-y2+y+h*(f(x,y)+f(x1,y2))/2)\n", +" printf('\n Y(%i) = %g or %g\n',i,y1(1),y1(2))\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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/16-Numerical_Solutions_of_Ordinary_Differential_Equations_Boundary_Value_Problems.ipynb b/Numerical_Methods_Principles_by_Analysis/16-Numerical_Solutions_of_Ordinary_Differential_Equations_Boundary_Value_Problems.ipynb new file mode 100644 index 0000000..1b738ec --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/16-Numerical_Solutions_of_Ordinary_Differential_Equations_Boundary_Value_Problems.ipynb @@ -0,0 +1,397 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 16: Numerical Solutions of Ordinary Differential Equations Boundary Value Problems" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.1: Outline_of_Linear_Shooting_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.1\n", +"//Outline of Linear Shooting Method\n", +"//Page no. 572\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=x^2');\n", +"h=0.5;X0=0;Y0=1;Z1=[-1,-1.5,-1.1771];i=1;Y1=Y0;\n", +"for j=1:3\n", +" Z0=Z1(i);\n", +" i=i+1\n", +" Y0=1;\n", +" for n=1:2\n", +" printf('\nFor n = %i\n---------------------------\n',n-1)\n", +" K1(1)=h*Z0;\n", +" printf('\n K11 = %g',K1(1));\n", +" K1(2)=h*f(Y0);\n", +" printf('\n K12 = %g',K1(2));\n", +" K2=h*f(Y0+K1(2))\n", +" printf('\n K22 = %g',K2);\n", +" Z0=Z0+(K1(2)+K2)/2\n", +" printf('\n Z%i = %g',n,Z0);\n", +" K2=h*Z0;\n", +" printf('\n K21 = %g',K2);\n", +" Y0=Y0+(K1(1)+K2)/2\n", +" printf('\n Y%i = %g',n,Y0);\n", +" printf('\n\n\n')\n", +" if n==1 then\n", +" Y2=Y0\n", +" end\n", +" end\n", +" printf('\n\n\n')\n", +"end\n", +"printf('Hence the solution is y(%g) = %i, y(%g) = %.4f and y(%g) = %.1f',X0,Y1,X0+h,Y2,X0+2*h,Y0)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.2: Linear_Shooting_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.2\n", +"//Linear Shooting Method\n", +"//Page no. 576\n", +"clc;close;clear;\n", +"\n", +"deff('y=f1(x,y,y1)','y=-x*y1+x^2*y+2*x^3')\n", +"deff('y=F1(x,y,y1)','y=-x*y1+x^2*y+2*x^3')\n", +"deff('y=F2(x,y,y1)','y=-x*y1+x^2*y')\n", +"a=0;b=1;\n", +"y0=1;y1=-1;n=5;\n", +"h=(b-a)/n\n", +"y=y0;y01=0;x=a;\n", +"for i=0:5\n", +" yi1(1,i+1)=y\n", +" K1=h*y01;\n", +" R1=h*F1(x,y,y01);\n", +" K2=h*(y+R1/2);\n", +" R2=h*F1(x+h/2,y+K1/2,y01+R1/2)\n", +" K3=h*(y01+R2/2)\n", +" R3=h*F1(x+h/2,y+K2/2,y01+R2/2)\n", +" K4=h*(y+R3)\n", +" R4=h*F1(x+h,y+K3,y01+R3)\n", +" y=y+(K1+2*K2+2*K3+K4)/6\n", +" y01=y01+(R1+2*R2+2*R3+R4)/6\n", +" x=x+h\n", +"end\n", +"y=0;y01=1;x=a;\n", +"for i=0:5\n", +" yi2(1,i+1)=y\n", +" K1=h*y01;\n", +" R1=h*F2(x,y,y01);\n", +" K2=h*(y+R1/2);\n", +" R2=h*F2(x+h/2,y+K1/2,y01+R1/2)\n", +" K3=h*(y01+R2/2)\n", +" R3=h*F2(x+h/2,y+K2/2,y01+R2/2)\n", +" K4=h*(y+R3)\n", +" R4=h*F2(x+h,y+K3,y01+R3)\n", +" y=y+(K1+2*K2+2*K3+K4)/6\n", +" y01=y01+(R1+2*R2+2*R3+R4)/6\n", +" x=x+h\n", +"end\n", +"for i=1:6\n", +" yi(i)=yi1(1,i)+((y1-yi1(6))/yi2(6))*yi2(i)\n", +"end\n", +"y=1;x=a;y01=y1\n", +"for i=0:5\n", +" yir(1,i+1)=y;\n", +" K1=h*y01;\n", +" R1=h*f1(x,y,y01);\n", +" K2=h*(y+R1/2);\n", +" R2=h*f1(x+h/2,y+K1/2,y01+R1/2)\n", +" K3=h*(y01+R2/2)\n", +" R3=h*f1(x+h/2,y+K2/2,y01+R2/2)\n", +" K4=h*(y+R3)\n", +" R4=h*f1(x+h,y+K3,y01+R3)\n", +" y=y+(K1+2*K2+2*K3+K4)/6\n", +" y01=y01+(R1+2*R2+2*R3+R4)/6\n", +" x=x+h\n", +"end\n", +"x=a;\n", +"printf('\n ------------------------------------------------------------------------------------------------------\n\tx')\n", +"for i=1:6\n", +" printf('\t%.1f\t',x)\n", +" x=x+h\n", +"end\n", +"printf('\n\ty')\n", +"for i=1:6\n", +" printf('\t%.4f\t',yi(i))\n", +"end\n", +"printf('\n by RK')\n", +"for i=1:6\n", +" printf('\t%.4f\t',yir(i))\n", +"end\n", +"printf('\n ------------------------------------------------------------------------------------------------------')\n", +"printf('\n\n\nNote: Computation error in calculation of values by RK method performed in book')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.3: Multiple_Shooting_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.3\n", +"//Multiple Shooting Method\n", +"//Page no. 577\n", +"clc;close;clear;\n", +"\n", +"h=0.25;x=0;y1=0;\n", +"deff('y=f(x)','y=-(4*h^2)/(1+x)^2')\n", +"deff('y=f1(x)','y=-2*(1+(h^2)/(1+x)^2)')\n", +"\n", +"for i=1:4\n", +" x=x+h\n", +" B(i)=f(x);\n", +" for j=1:4\n", +" if i==4 & i==j\n", +" A(i,j)=f1(x)+1/4\n", +" A(i,j-1)=2\n", +" elseif j==i then\n", +" A(i,j)=f1(x)\n", +" A(i,j+1)=1\n", +" if j-1~=0 then\n", +" A(i,j-1)=1\n", +" end\n", +" end\n", +" end\n", +"end\n", +"y=inv(A)*B\n", +"disp(B,'B =',A,'A = ')\n", +"printf('\n\n\n x :')\n", +"for i=1:5\n", +" printf('\t%.2f',x)\n", +" x=x+h\n", +"end\n", +"x=0;printf('\n y :\t%.2f',y1);\n", +"for i=1:4\n", +" printf('\t%.4f',y(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.4: Finite_Difference_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.4\n", +"//Finite Difference Method\n", +"//Page no. 582\n", +"clc;close;clear;\n", +"\n", +"x=0;h=0.25;q=-1;Y(1)=-2;Y(5)=1;\n", +"printf('\n i\txi\tYi\tpi\tqi\tri\n-----------------------------------------------\n')\n", +"for i=1:5\n", +" r(i)=-x^2\n", +" if i>1 & i<5 then\n", +" printf(' %i\t%g\t%s\t%g\t%i\t%g\n',i-1,x,'?',x,q,r(i))\n", +" else\n", +" printf(' %i\t%g\t%g\t%g\t%i\t%g\n',i-1,x,Y(i),x,q,r(i))\n", +" end\n", +" x=x+h\n", +"end\n", +"x=0;\n", +"printf('-----------------------------------------------\n')\n", +"for i=1:3\n", +" x=x+h\n", +" for j=1:3\n", +" if i==j then\n", +" A(i,j)=2+h^2*q\n", +" elseif i<j & abs(i-j)~=2\n", +" A(i,j)=-1+h*x/2\n", +" elseif i>j & abs(i-j)~=2\n", +" A(i,j)=-1-h*x/2\n", +" end\n", +" end\n", +" if i==3 then\n", +" B(i)=-h^2*r(i+1)+(-h*x/2+1)*Y(1+2*(i-1))\n", +" else\n", +" B(i)=-h^2*r(i+1)+(h*x/2+1)*Y(1+2*(i-1))\n", +" end\n", +" B(i)=(-1)^(i+1)*B(i)\n", +"end\n", +"disp(B,'B =',A,'A = ')\n", +"y=inv(A)*B\n", +"for i=1:3\n", +" Y(i+1)=y(i)\n", +"end\n", +"x=0;\n", +"disp('The Solution is :',B,'B =',A,'A = ')\n", +"printf(' x :')\n", +"for i=1:5\n", +" printf('\t %.2f',x)\n", +" x=x+h\n", +"end\n", +"x=0;printf('\n y :');\n", +"for i=1:5\n", +" printf('\t%.3f',Y(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.5: Non_Linear_Problem.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.5\n", +"//Non Linear Problem\n", +"//Page no. 584\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=2/(1+x)')\n", +"Y=[1,0.75,0.75,0.75,0.5];h=0.25\n", +"A=[-2,1,0;1,-2,1;0,1,-2];A_1=inv(A)\n", +"disp(A_1,'Inverse of A =',A,'A =')\n", +"printf('\nThe Solution of the system is: \n\n Iteration\t Y0\t\t Y1\t\t Y2\t\t Y3\t\t Y4\n----------------------------------------------------------------------------------------')\n", +"for i=0:6\n", +" printf('\n %i',i)\n", +" for j=1:5\n", +" if j<4 & i~=0 then\n", +" Y(j+1)=y(j)\n", +" end\n", +" printf('\t\t%.4f',Y(j))\n", +" end\n", +" x=0;\n", +" for j=1:3\n", +" x=x+h\n", +" if j~=2 then\n", +" B(j)=h^2*f(x)*Y(j+1)^2-Y(1+2*(j-1))\n", +" else\n", +" B(j)=h^2*f(x)*Y(j+1)^2\n", +" end\n", +" end\n", +" y=A_1*B\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 16.6: Collocation_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 16.6\n", +"//Collocation Method\n", +"//Page no. 589\n", +"clc;close;clear;\n", +"\n", +"h1=0.000001;h=0.25;x=0;\n", +"Y(1)=0;Y(5)=0;\n", +"deff('y=p(x)','y=1')\n", +"deff('y=q(x)','y=-2/(1+x)^2')\n", +"deff('y=f(x)','y=(2*x-4)/(1+x)^4')\n", +"deff('y=fi(x,j)','y=(1-x)*x^j')\n", +"deff('y=f1(x,y)','y=(-x+y)/h1') //function for differentiation\n", +"for i=1:4\n", +" x=x+h\n", +" for j=1:4\n", +" A(i,j)=p(x)*f1(f1(fi(x,j),fi(x+h1,j)),f1(fi(x+h1,j),fi(x+2*h1,j)))+f1(p(x),p(x+h1))*f1(fi(x,j),fi(x+h1,j))+q(x)*fi(x,j)\n", +" end\n", +"end\n", +"x=0;\n", +"for i=1:4\n", +" x=x+h\n", +" B(i)=f(x)\n", +"end\n", +"disp(B,'B =',A,'A =')\n", +"C=inv(A)*B\n", +"x=0;\n", +"for i=2:4\n", +" x=x+h;\n", +" for j=1:4\n", +" Y(i)=Y(i)+C(j)*fi(x,j)\n", +" end\n", +"end\n", +"disp(Y,'Solution Matrix Y = ')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/18-Numerical_Solutions_of_Parabolic_Partial_Differential_Equations.ipynb b/Numerical_Methods_Principles_by_Analysis/18-Numerical_Solutions_of_Parabolic_Partial_Differential_Equations.ipynb new file mode 100644 index 0000000..c66c4b2 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/18-Numerical_Solutions_of_Parabolic_Partial_Differential_Equations.ipynb @@ -0,0 +1,399 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 18: Numerical Solutions of Parabolic Partial Differential Equations" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 18.4: Forward_Difference_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 18.4\n", +"//Forward Difference Method\n", +"//Page no. 624\n", +"clc;clear;close;\n", +"\n", +"h=0.2;k=0.02;\n", +"r=k/h^2;\n", +"printf('\n j\tt\t|\ti -->\t')\n", +"for i=0:5\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tx -->\t')\n", +"for i=0:5\n", +" printf('%.3f\t',(i)/5)\n", +"end\n", +"printf('\n-------------------------------------------------------------------------------')\n", +"for j=1:6\n", +" printf('\n %i\t%.3f\t|\t\t',j-1,(j-1)/50)\n", +" for i=1:6\n", +" if i==1 | i==6 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=sin(%pi*(i-1)/5)\n", +" else\n", +" u(j,i)=(u(j-1,i-1)+u(j-1,i+1))/2\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 18.5: Bender_Schmidt_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 18.5\n", +"//Bender Schmidt Method\n", +"//Page no. 625\n", +"clc;clear;close;\n", +"\n", +"h=0.1;k=0.005;\n", +"r=k/h^2;\n", +"printf('\n j |\ti -->\t')\n", +"for i=0:10\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n | |\tx -->\t')\n", +"for i=0:10\n", +" printf('%.3f\t',(i)/10)\n", +"end\n", +"printf('\n------------------------------------------------------------------------------------------------------')\n", +"for j=1:9\n", +" printf('\n %i |\t\t',j-1)\n", +" for i=1:11\n", +" if i==1 | i==11 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=sin(%pi*(i-1)/10)\n", +" else\n", +" u(j,i)=u(j-1,i)/2+(u(j-1,i-1)+u(j-1,i+1))/4\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 18.6: Crank_Nicolson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 18.6\n", +"//Crank Nicolson Method\n", +"//Page no. 631\n", +"clc;clear;close;\n", +"h=1/2;k=1/8;\n", +"r=k/h^2;\n", +"for i=1:2:3\n", +" for j=1:9\n", +" if i==1 | j==1 then\n", +" u(i,j)=0;\n", +" end\n", +" if i==3 then\n", +" u(i,j)=(j-1)/8\n", +" end\n", +" end\n", +"end\n", +"a=[3,-1,0;-1,3,-1;0,-1,3];\n", +"a=inv(a);\n", +"for j=2:9\n", +" u(2,j)=(u(1,j-1)+2*u(2,j-1)+u(3,j-1)+u(1,j)+u(3,j))/6\n", +"end\n", +"u=u'\n", +"printf('\nfor h = 1/2\n\n')\n", +"printf('i\\j --> ')\n", +"for i=1:3\n", +" printf('\t%i\t',i)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------\n')\n", +"for i=1:9\n", +" printf('\n %i',i)\n", +" for j=1:3\n", +" printf('\t %.9f',u(i,j))\n", +" end\n", +"end\n", +"\n", +"\n", +"\n", +"\n", +"h=1/4;k=1/8;\n", +"r=k/h^2;\n", +"for i=1:4:5\n", +" for j=1:9\n", +" if i==1 | j==1 then\n", +" u(i,j)=0;\n", +" end\n", +" if i==5 then\n", +" u(i,j)=(j-1)/8\n", +" end\n", +" end\n", +"end\n", +"a=[3,-1,0;-1,3,-1;0,-1,3];\n", +"a=inv(a);\n", +"for j=2:9\n", +" b=[u(1,j-1)-u(2,j-1)+u(3,j-1)+u(1,j);u(2,j-1)-u(3,j-1)+u(4,j-1);u(3,j-1)-u(4,j-1)+u(5,j-1)+u(5,j)]\n", +" x=a*b\n", +" u(2,j)=x(1);u(3,j)=x(2);u(4,j)=x(3);\n", +"end\n", +"u=u'\n", +"printf('\n\n\n\n\nfor h = 1/4\n\n')\n", +"printf('i\\j --> ')\n", +"for i=1:5\n", +" printf('\t%i\t',i)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------\n')\n", +"for i=1:9\n", +" printf('\n %i',i)\n", +" for j=1:5\n", +" printf('\t %.9f',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 18.7: Gauss_Seidel_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 18.7\n", +"//Gauss Seidel Method\n", +"//Page no. 637\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=4*x-4*x^2')\n", +"h=0.2;k=0.04;\n", +"r=k/h^2;\n", +"printf('\n k\t|\ti -->\t')\n", +"for i=0:5\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\tx -->\t')\n", +"for i=0:5\n", +" printf('%.2f\t',(i)/5)\n", +"end\n", +"printf('\n-------------------------------------------------------------------------------')\n", +"for k=1:7\n", +" printf('\n %i\t|\t\t',k-1)\n", +" for i=1:6\n", +" if i==1 | i==6 then\n", +" u(k,i)=0;\n", +" elseif k==1 then\n", +" u(k,i)=f((i-1)/5)\n", +" else\n", +" u(k,i)=(u(k-1,i-1)+u(k-1,i+1))/2\n", +" end\n", +" printf('%.2f\t',u(k,i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 18.8: ADI_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 18.8\n", +"//ADI Method\n", +"//Page no. 642\n", +"clc;clear;close;\n", +"\n", +"for i=1:4\n", +" for j=1:5\n", +" P(i,j)=20\n", +" end\n", +"end\n", +"r=1;k=0;\n", +"for i=1:6\n", +" v1(i)=20\n", +" u1(i)=20\n", +"end\n", +"P1=[25,30,35,50,60;35,0,0,0,70;45,0,0,0,80;60,70,80,100,90]\n", +"for i=1:4\n", +" printf('\n')\n", +" for j=1:5\n", +" printf('%i\t',P(i,j))\n", +" end\n", +" if i==2 then\n", +" printf('-->')\n", +" end\n", +" printf('\t')\n", +" for j=1:5\n", +" printf('%i\t',P1(i,j))\n", +" if i>1 & i<4 & j>1 & j<5 then\n", +" P1(i,j)=P(i,j)\n", +" end\n", +" end\n", +"end\n", +"P1v=P1;P1h=P1;\n", +"for i=1:6\n", +" for j=1:6\n", +" if i==j then\n", +" Av(i,j)=1+2*r\n", +" elseif abs(i-j)==1 & i+j~=5 & i+j~=9\n", +" Av(i,j)=-r\n", +" end\n", +" end\n", +"end\n", +"for i=1:6\n", +" for j=1:6\n", +" if i==j then\n", +" Ah(i,j)=1+2*r\n", +" elseif abs(i-j)==1 & i+j~=7\n", +" Ah(i,j)=-r\n", +" end\n", +" end\n", +"end\n", +"n=8\n", +"for l=1:n\n", +" k=0;\n", +"for j=0:2\n", +" for i=1:2\n", +" if i==1 then\n", +" Bv(i+j+k)=r*P1h(i+1,j+1)+(1-2*r)*P1h(i+1,j+2)+r*P1h(i+1,j+3)+r*P1h(i,j+1)\n", +" else\n", +" Bv(i+j+k)=r*P1h(i+1,j+1)+(1-2*r)*P1h(i+1,j+2)+r*P1h(i+1,j+3)+r*P1h(i+2,j+1)\n", +" end\n", +" end\n", +" k=k+1;\n", +"end\n", +"k=0;\n", +"Bh=[r*30+(1-2*r)*v1(1)+r*v1(4)+r*35;r*35+(1-2*r)*v1(3)+r*v1(5);r*v1(1)+(1-2*r)*v1(2)+r*v1(3)+r*(70);r*v1(1)+(1-2*r)*v1(2)+r*(70+45);r*v1(3)+(1-2*r)*v1(4)+r*80;r*v1(5)+(1-2*r)*v1(6)+r*(100+80)]\n", +" for i=1:6\n", +" v(i,l)=v1(i)\n", +" end\n", +" for i=1:6\n", +" u(i,l)=u1(i)\n", +" end\n", +" v1=inv(Av)*Bv\n", +" u1=inv(Ah)*Bh\n", +" k=1;\n", +" for i=2:3\n", +" for j=2:4\n", +" P1h(i,j)=u1(i+j+k-4)\n", +" end\n", +" k=k+2\n", +" end\n", +" k=0;\n", +" for j=2:4\n", +" for i=2:3\n", +" P1v(i,j)=v1(i+j+k-3)\n", +" end\n", +" k=k+1\n", +" end\n", +"end\n", +"printf('\n\n\n\nResults for Vertical Transverse in Celsius :\n')\n", +"for i=1:7\n", +" printf('\n')\n", +" if i==1 then\n", +" printf('Itr -->')\n", +" for j=1:n\n", +" printf('\t %i',j-1)\n", +" end\n", +" printf('\n----------------------------------------------------------------------')\n", +" else\n", +" printf(' v%i',i-1)\n", +" for j=1:n\n", +" printf('\t%.2f',v(i-1,j))\n", +" end\n", +" end\n", +"end\n", +"printf('\n\n\n\nResults for Horizontal Transverse in Celsius :\n')\n", +"for i=1:7\n", +" printf('\n')\n", +" if i==1 then\n", +" printf('Itr -->')\n", +" for j=1:n\n", +" printf('\t %i',j-1)\n", +" end\n", +" printf('\n----------------------------------------------------------------------')\n", +" else\n", +" printf(' u%i',i-1)\n", +" for j=1:n\n", +" printf('\t%.2f',u(i-1,j))\n", +" end\n", +" end\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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/19-Numerical_Solutions_of_Hyperbolic_Partial_Differential_Equations.ipynb b/Numerical_Methods_Principles_by_Analysis/19-Numerical_Solutions_of_Hyperbolic_Partial_Differential_Equations.ipynb new file mode 100644 index 0000000..831d59e --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/19-Numerical_Solutions_of_Hyperbolic_Partial_Differential_Equations.ipynb @@ -0,0 +1,751 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 19: Numerical Solutions of Hyperbolic Partial Differential Equations" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.10: Non_Linear_1st_Order_Hyperboolic_Differential_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.10\n", +"//Non Linear 1st Order Hyperboolic Differential Equation\n", +"//Page no. 667\n", +"clc;clear;close;\n", +"\n", +"c=-2;k=0.05;h=0.2;\n", +"r=abs(c)*k/h;\n", +"printf('\n i\t x\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t |\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*k)\n", +"end\n", +"i=1;\n", +"x=0;\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"for j=1:7\n", +" for i=1:6\n", +" if j==1 then\n", +" u(i,j)=exp(-x);\n", +" u(i+1,j)=exp(-(x+h));\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=u(i,j-1)-k*(u(i+1,j-1)^2-u(i-1,j-1)^2)/(4*h)+k^2*((u(i+1,j-1)+u(i,j-1))*(u(i+1,j-1)^2-u(i,j-1)^2)-(u(i,j-1)+u(i-1,j-1))*(u(i,j-1)^2-u(i-1,j-1)^2))/(8*h^2)\n", +" end\n", +" x=x+h\n", +" end\n", +"end\n", +"x=0;\n", +"for i=1:6\n", +" printf('\n %i\t%.1f\t|\t\t',i-1,x)\n", +" for j=1:7\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +" x=x+h\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.11: Finite_Difference_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.11\n", +"//Finite Difference Method\n", +"//Page no. 670\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=sin(%pi*x)')\n", +"deff('y=g(x)','y=0')\n", +"a=1;b=1;c=1;n=5;m=10;\n", +"h=a/n;k=b/m;r=c*k/h;\n", +"r1=r^2;r2=r1/2;s1=1-r1;s2=2*(1-r2)\n", +"printf('\n i ')\n", +"for i=1:n\n", +" printf('\t %i',i)\n", +"end\n", +"printf('\n-----------------------------------------------\nfi')\n", +"for i=1:n\n", +" f1(i)=f(h*(i-1))\n", +" printf('\t%.3f',f1(i))\n", +"end\n", +"printf('\ngi')\n", +"for i=1:n\n", +" g1(i)=g(h*(i-1))\n", +" printf('\t %g',g1(i))\n", +"end\n", +"printf('\n\n\n i / j --> ')\n", +"for i=1:m\n", +" printf('\t %i',i)\n", +"end\n", +"printf('\n-------------------------------------------------------------------------------------------------')\n", +"for j=1:m\n", +" for i=1:n\n", +" if i==1 | i==n then\n", +" u(i,j)=0;\n", +" elseif j==1\n", +" u(i,j)=f1(i)\n", +" elseif j==2\n", +" u(i,j)=s1*f1(i)+k*g1(i)+r2*(f1(i+1)+f1(i-1))\n", +" else\n", +" u(i,j)=s2*u(i,j-1)+r1*u(i-1,j-1)+u(i+1,j-1)-u(i,j-2)\n", +" end\n", +" \n", +" end\n", +"end\n", +"for i=1:n\n", +" printf('\n %i\t',i)\n", +" for j=1:m\n", +" printf('\t%.3f',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.12: Hyperbolic_Partial_Differential_Equations.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.12\n", +"//Hyperbolic Partial Differential Equations\n", +"//Page no. 673\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=12*x')\n", +"Ua(1)=0.25;\n", +"Ua(2)=0.75\n", +"A=[1,-2;1,2];\n", +"x1=inv(A)*Ua;\n", +"printf('Xb = %g and Tb = %g',x1(1),x1(2))\n", +"A=[2,-1;2,1];\n", +"B=[-7.5;-8.5];\n", +"x2=inv(A)*B;\n", +"printf('\n\n Pb = %g and Qb = %g',x2(1),x2(2))\n", +"x1(1)=x1(1)-Ua(1)\n", +"du=x1'*x2\n", +"printf('\n\n dU = %g',du)\n", +"Ub=f(Ua(1))+du;\n", +"printf('\n\n Modified Ub = %g',Ub)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.13: Hyperbolic_Differential_Equations_in_2D_or_3D.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.13\n", +"//Hyperbolic Differential Equations in 2D or 3D\n", +"//Page no. 675\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x,y)','y=x*(2-x)*y*(2-y)')\n", +"c2=3;k=0.4;h=0.4;c2=3;s2=0.5\n", +"for l=0:11\n", +" if l==0 then\n", +" printf('\n t = %i\n\n i\t x\t|\tj -->\t',l)\n", +" for i=0:5\n", +" printf(' %i\t',i)\n", +" end\n", +" printf('\n |\t |\t|\ty -->\t')\n", +" for i=0:5\n", +" printf('%.3f\t',i*k)\n", +" end\n", +" x=0;\n", +" printf('\n---------------------------------------------------------------------------------------')\n", +" for i=1:6\n", +" y=0;\n", +" printf('\n %i\t%.3f\t|\t\t',i-1,x)\n", +" for j=1:6\n", +" if i==1 | i==6 then\n", +" u(i,j)=0;\n", +" elseif j==1 | j==6 then\n", +" u(i,j)=0\n", +" else\n", +" u(i,j)=f(x,y)\n", +" end\n", +" printf('%.3f\t',u(i,j))\n", +" y=y+k;\n", +" end\n", +" x=x+h\n", +"end\n", +"u2=u;\n", +"else\n", +" printf('\n\n\n t = %i\n\n i\t x\t|\tj -->\t',l)\n", +" for i=0:5\n", +" printf(' %i\t',i)\n", +" end\n", +" printf('\n |\t |\t|\ty -->\t')\n", +" for i=0:5\n", +" printf('%.3f\t',i*k)\n", +" end\n", +" x=0;\n", +" printf('\n---------------------------------------------------------------------------------------')\n", +" for i=1:6\n", +" y=0;\n", +" printf('\n %i\t%.3f\t|\t\t',i-1,x)\n", +" for j=1:6\n", +" if i==1 | i==6 then\n", +" u(i,j)=0;\n", +" elseif j==1 | j==6 then\n", +" u(i,j)=0\n", +" elseif l==1\n", +" u(i,j)=s2*(u1(i+1,j)+u1(i-1,j)+u1(i,j+1)+u1(i,j-1)-4*u1(i,j))+2*u1(i,j)\n", +" else\n", +" u(i,j)=s2*(u1(i+1,j)+u1(i-1,j)+u1(i,j+1)+u1(i,j-1)-4*u1(i,j))+2*u1(i,j)-u2(i,j)\n", +" end\n", +" printf('%.4f\t',u(i,j))\n", +" y=y+k;\n", +" end\n", +" x=x+h\n", +" end\n", +"end\n", +"if l>1 then\n", +" u2=u1\n", +"end\n", +"u1=u;\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.3: Simple_Explicit_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.3\n", +"//Simple Explicit Method\n", +"//Page no. 658\n", +"clc;clear;close;\n", +"\n", +"c=-2;dt=0.07;dx=0.2;\n", +"r=abs(c)*dt/dx;\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"for j=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',(j-1)*dx,j-1)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1)\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.4: Simple_Implicit_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.4\n", +"//Simple Implicit Method\n", +"//Page no. 659\n", +"clc;clear;close;\n", +"\n", +"c=-2;dt=0.07;dx=0.2;\n", +"r=abs(c)*dt/dx;\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"for j=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',(j-1)*dx,j-1)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+r)\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.5: Lax_Wendroff_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.5\n", +"//Lax Wendroff Method\n", +"//Page no. 660\n", +"clc;clear;close;\n", +"\n", +"c=-2;dt=0.07;dx=0.2;\n", +"r=abs(c)*dt/dx;\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"i=1;\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"for j=1:7\n", +" for i=1:6\n", +" if j==1 then\n", +" u(i,j)=0;\n", +" u(i+1,j)=0;\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=r*(r-1)*u(i+1,j-1)/2+(1-r^2)*u(i,j-1)+r*(1+r)*u(i-1,j-1)/2\n", +" end\n", +" end\n", +"end\n", +"for i=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',(i-1)*dx,i-1)\n", +" for j=1:7\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.6: Wendroff_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.6\n", +"//Wendroff Method\n", +"//Page no. 661\n", +"clc;clear;close;\n", +"\n", +"c=2;k=0.07;h=0.2;\n", +"a=(h+k*c)/(h-k*c)\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*k)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"for i=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',(i-1)*h,i-1)\n", +" for j=1:7\n", +" if j==1 then\n", +" u(i,j)=0;\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a\n", +" end\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.7: Leapfrog_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.7\n", +"//Leapfrog Method\n", +"//Page no. 662\n", +"clc;clear;close;\n", +"\n", +"c=2;k=0.07;h=0.2;\n", +"r=c*k/h\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*k)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"\n", +"for j=1:7\n", +" for i=1:6\n", +" if j==1 | j==2 & i~=1 then\n", +" u(i,j)=0;\n", +" u(i+1,j)=0;\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=u(i,j-2)-r*(u(i+1,j-1)-u(i-1,j-1))\n", +" end\n", +" end\n", +"end\n", +"for i=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',(i-1)*h,i-1)\n", +" for j=1:7\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.8: Variable_Coefficients.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.8\n", +"//Variable Coefficients\n", +"//Page no. 663\n", +"clc;clear;close;\n", +"\n", +"//simple explicit method\n", +"printf('\n\nBy Simple Explicit Method:\n\n')\n", +"dt=0.05;dx=0.2;\n", +"x=0;\n", +"printf('\n i\t x\t r\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n \t\t\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n----------------------------------------------------------------------------------------------')\n", +"for j=1:6\n", +" r=sqrt(1+2*x)*dt/dx;\n", +" printf('\n %i\t%.3f\t%.3f\t|\t\t',(j-1),x,r)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1)\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" \n", +" end\n", +" x=x+dx\n", +"end\n", +"\n", +"\n", +"//simple implicit method\n", +"printf('\n\n\nBy Simple Implicit Method:\n')\n", +"c=-2;dt=0.05;dx=0.2;x=0\n", +"printf('\n i\t x\t r\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n \t\t\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n----------------------------------------------------------------------------------------------')\n", +"for j=1:6\n", +" r=sqrt(1+2*x)*dt/dx;\n", +" printf('\n %i\t%.3f\t%.3f\t|\t\t',(j-1),x,r)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=0;\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+r)\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +" x=x+dx\n", +"end\n", +"\n", +"\n", +"//wendroff method\n", +"printf('\n\n\nBy Wendroff Method:\n')\n", +"k=0.05;h=0.2;\n", +"x=0.1;\n", +"printf('\n i\t x\t c\t a\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n \t\t\t\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*k)\n", +"end\n", +"printf('\n-------------------------------------------------------------------------------------------------------')\n", +"for i=1:6\n", +" c=sqrt(1+2*x);\n", +" a=(h+k*c)/(h-k*c)\n", +" printf('\n %i\t%.3f\t%.3f\t%.3f\t|\t\t',(i-1),x-h/2,c,a)\n", +" for j=1:7\n", +" if j==1 then\n", +" u(i,j)=0;\n", +" u(i+1,j)=0;\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a\n", +" end\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +" x=x+h\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 19.9: Inhomogeneous_1st_Order_Hyperboolic_Differential_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 19.9\n", +"//Inhomogeneous 1st Order Hyperboolic Differential Equation\n", +"//Page no. 665\n", +"clc;clear;close;\n", +"\n", +"//simple explicit method\n", +"printf('\n\nBy Simple Explicit Method:\n')\n", +"c=-2;dt=0.07;dx=0.2;\n", +"r=abs(c)*dt/dx;\n", +"printf('\n i\tx\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"x=0;\n", +"for j=1:6\n", +" printf('\n %i\t%.1f\t|\t\t',j-1,x)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=exp(-x);\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1)+dt*2*x\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +" x=x+dx\n", +"end\n", +"\n", +"\n", +"//simple implicit method\n", +"printf('\n\n\nBy Simple Implicit Method:\n')\n", +"c=-2;dt=0.07;dx=0.2;\n", +"r=abs(c)*dt/dx;\n", +"printf('\n i\tx\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*dt)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"x=0;\n", +"for j=1:6\n", +" printf('\n %i\t%.1f\t|\t\t',j-1,x)\n", +" for i=1:7\n", +" if i==1 then\n", +" u(j,i)=exp(-x);\n", +" elseif j==1 then\n", +" u(j,i)=1\n", +" else\n", +" u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+r)+dt*2*x\n", +" end\n", +" printf('%.3f\t',u(j,i))\n", +" end\n", +" x=x+dx\n", +"end\n", +"\n", +"\n", +"//wendroff method\n", +"printf('\n\n\nBy Wendroff Method:\n')\n", +"c=2;k=0.07;h=0.2;\n", +"a=(h+k*c)/(h-k*c)\n", +"printf('\n x\ti\t|\tj -->\t')\n", +"for i=0:6\n", +" printf(' %i\t',i)\n", +"end\n", +"printf('\n |\t|\t|\tt -->\t')\n", +"for i=0:6\n", +" printf('%.3f\t',i*k)\n", +"end\n", +"printf('\n---------------------------------------------------------------------------------------')\n", +"x=0;\n", +"for i=1:6\n", +" printf('\n %.1f\t%i\t|\t\t',x,i-1)\n", +" for j=1:7\n", +" if j==1 then\n", +" u(i,j)=exp(-x);\n", +" elseif i==1 then\n", +" u(i,j)=1\n", +" else\n", +" u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a+(2*h*k)*(x+h/2)/(a*(h+c*k))\n", +" end\n", +" printf('%.3f\t',u(i,j))\n", +" end\n", +" x=x+h\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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/2-Scope_of_Numerical_and_Mathematical_Methods.ipynb b/Numerical_Methods_Principles_by_Analysis/2-Scope_of_Numerical_and_Mathematical_Methods.ipynb new file mode 100644 index 0000000..7652685 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/2-Scope_of_Numerical_and_Mathematical_Methods.ipynb @@ -0,0 +1,84 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 2: Scope of Numerical and Mathematical Methods" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4: Solving_Simultaneous_Linear_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 2.4\n", +"//Solving Simultaneous Linear Equation\n", +"//Page 36\n", +"clc;close;clear;\n", +"//eq1= 5x-331y=3.5\n", +"//eq2= 6x-397y=5.2\n", +"\n", +"A=[5,-331;6,-397];\n", +"B=[3.5;5.2];\n", +"C=inv(A)*B; //finding value by multiplying inverse with values\n", +"disp(C(1,1),'Value of x=');\n", +"disp(C(2,1),'Value of y=');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.6: Integration.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 2.6\n", +"//Integration\n", +"//Page no. 36\n", +"clc;clear;close;\n", +"disp(integrate('1/x','x',exp(-4),1),'Integration Value='); //performing integration with respect to dx" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/20-Numerical_Solutions_of_Elliptical_Partial_Differential_Equations.ipynb b/Numerical_Methods_Principles_by_Analysis/20-Numerical_Solutions_of_Elliptical_Partial_Differential_Equations.ipynb new file mode 100644 index 0000000..010a912 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/20-Numerical_Solutions_of_Elliptical_Partial_Differential_Equations.ipynb @@ -0,0 +1,467 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 20: Numerical Solutions of Elliptical Partial Differential Equations" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.1: Direct_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.1\n", +"//Direct Method\n", +"//Page no. 682\n", +"clc;clear;close;\n", +"h=1/3;\n", +"A=[-4,1,1,0;1,-4,0,1;1,0,-4,1;0,1,1,-4]\n", +"x=0;\n", +"for i=1:4\n", +" x=x+h\n", +" if i==4 then\n", +" B(i,1)=0\n", +" else\n", +" B(i,1)=-1*sin(x*%pi)^2\n", +" end\n", +"end\n", +"disp(A,'A =')\n", +"disp(B,'B =')\n", +"U=inv(A)*B\n", +"disp(U,'U =')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.2: Five_Point_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.2\n", +"//Five Point Formula\n", +"//Page no. 683\n", +"clc;clear;close;\n", +"\n", +"A=[-4,1,1,0;1,0,-4,1;1,-4,0,1;0,1,1,-4];\n", +"B=[-25;-150;-25;-150];\n", +"u1=inv(A)*B;\n", +"j=0;k=1\n", +"for i=1:4\n", +" j=j+1;\n", +" printf('\nu%i%i = %g\n',k,j,u1(i))\n", +" if i==2 then\n", +" j=0;k=2\n", +" end\n", +"end\n", +"printf('\n\n U = \n')\n", +"for i=1:4\n", +" printf('\n')\n", +" for j=1:4\n", +" if j==1 then\n", +" u(i,j)=0\n", +" elseif j==4\n", +" u(i,j)=100\n", +" elseif (i==1 | i==4) & j==2\n", +" u(i,j)=25\n", +" elseif i==1 | i==4\n", +" u(i,j)=u(i,j-1)*2\n", +" else\n", +" u(i,j)=u1((j-1)*2-i+2)\n", +" end\n", +" printf('\t%g\t',u(i,j))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.3: Finite_Difference_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.3\n", +"//Finite Difference Method\n", +"//Page no. 685\n", +"clc;clear;close;\n", +"\n", +"printf('Itr\t\t U11\t\t U21\t\t U12\t\t U22\n-----------------------------------------------------------------------\n')\n", +"for i=1:4\n", +" for j=1:4\n", +" if j==1 then\n", +" u(i,j)=0\n", +" elseif j==4\n", +" u(i,j)=100\n", +" elseif (i==1 | i==4) & j==2\n", +" u(i,j)=25\n", +" elseif i==1 | i==4\n", +" u(i,j)=u(i,j-1)*2\n", +" else\n", +" u(i,j)=0\n", +" end\n", +" end\n", +"end\n", +"for k=0:17\n", +" printf(' %i\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n',k,u(3,2),u(3,3),u(2,2),u(2,3))\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u1(i,j)=(u(i,j+1)+u(i,j-1)+u(i-1,j)+u(i+1,j))/4\n", +" end\n", +" end\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u(i,j)=u1(i,j)\n", +" end\n", +" end\n", +"end\n", +"disp(u,'U = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.4: Seven_Point_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.4\n", +"//Seven Point Formula\n", +"//Page no. 686\n", +"clc;clear;close;\n", +"printf('Itr\t\t U111\t\t U211\t\t U121\t\t U221\n-----------------------------------------------------------------------\n')\n", +"for i=1:4\n", +" for j=1:4\n", +" for k=3:-1:1\n", +" if k==3 then\n", +" u(i,j,k)=100\n", +" elseif (i==1 | i==4 | j==1 | j==4) & k==2\n", +" u(i,j,k)=300\n", +" elseif k==2\n", +" u(i,j,k)=0\n", +" elseif (i==1 | i==4 | j==1 | j==4) & k==1\n", +" u(i,j,k)=500\n", +" else\n", +" u(i,j,k)=700\n", +" end\n", +" end\n", +" end\n", +"end\n", +"k=2\n", +"for l=0:14\n", +" printf(' %i\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n',l,u(3,2,2),u(3,3,2),u(2,2,2),u(2,3,2))\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u1(i,j)=(u(i,j+1,k)+u(i,j-1,k)+u(i-1,j,k)+u(i+1,j,k)+u(i,j,k+1)+u(i,j,k-1))/6\n", +" end\n", +" end\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u(i,j,2)=u1(i,j)\n", +" end\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.5: Nine_Point_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.5\n", +"//Nine Point Formula\n", +"//Page no. 688\n", +"clc;clear;close;\n", +"\n", +"printf('Itr\t\t U11\t\t U12\t\t U21\t\t U22\n-----------------------------------------------------------------------\n')\n", +"for i=1:4\n", +" for j=1:4\n", +" if j==1 then\n", +" u(i,j)=0\n", +" elseif j==4\n", +" u(i,j)=100\n", +" elseif (i==1 | i==4) & j==2\n", +" u(i,j)=25\n", +" elseif i==1 | i==4\n", +" u(i,j)=u(i,j-1)*2\n", +" else\n", +" u(i,j)=0\n", +" end\n", +" end\n", +"end\n", +"for k=0:17\n", +" printf(' %i\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n',k,u(3,2),u(2,2),u(3,3),u(2,3))\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u1(i,j)=(u(i+1,j-1)+u(i-1,j-1)+u(i+1,j+1)+u(i-1,j+1)+4*(u(i,j+1)+u(i,j-1)+u(i-1,j)+u(i+1,j)))/20\n", +" end\n", +" end\n", +" for i=3:-1:2\n", +" for j=2:3\n", +" u(i,j)=u1(i,j)\n", +" end\n", +" end\n", +"end\n", +"disp(u,'The Solution of the System is = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.6: Five_Point_Formula.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.6\n", +"//Five Point Formula\n", +"//Page no. 689\n", +"clc;clear;close;\n", +"\n", +"h=0.25;k=0.25;y=1;x=0;\n", +"deff('x=f(y)','x=y^3')\n", +"\n", +"for i=1:5\n", +" x=0;\n", +" printf('\n%g\t|',y)\n", +" for j=1:5\n", +" if (i==1 | i==5)\n", +" u(i,j)=f(x)\n", +" elseif j==5\n", +" u(i,j)=f(x)\n", +" else\n", +" u(i,j)=0\n", +" end\n", +" x=x+k;\n", +" printf('%f\t',u(i,j))\n", +" end\n", +" y=y-h\n", +"end\n", +"printf('\n\t-------------------------------------------------------------------------\n')\n", +"x=0;\n", +" for j=1:5\n", +" printf('\t %g\t',x)\n", +" x=x+k\n", +" end\n", +"printf('\n\n\n Itr\t U11\t U12\t U13\t U21\t U22\t U23\t U31\t U32\t U33\n-------------------------------------------------------------------------------\n')\n", +"\n", +"for l=0:20\n", +" y=0;\n", +" printf(' %i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',l,u(4,2),u(4,3),u(4,4),u(3,2),u(3,3),u(3,4),u(2,2),u(2,3),u(2,4))\n", +" for i=4:-1:2\n", +" y=y+k\n", +" for j=2:4\n", +" u1(i,j)=(u(i,j+1)+u(i,j-1)+u(i-1,j)+u(i+1,j)-h^2*y)/4\n", +" end\n", +" end\n", +" for i=4:-1:2\n", +" for j=2:4\n", +" u(i,j)=u1(i,j)\n", +" end\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.7: Laplace_Distributio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.7\n", +"//Laplace Distribution\n", +"//Page no. 694\n", +"clc;clear;close;\n", +"\n", +"dr=3;r0=4;dth=%pi/4;\n", +"deff('y=f(u1,u2,u3,u4)','y=(u1+u3+(dr*(u3-u1))/(2*r0)+(u2+u4)*(dr/(r0*dth))^2)/(2*(1+(dr/(r0*dth))^2))') //laplace distribution\n", +"for i=1:8\n", +" U(i)=0;\n", +"end\n", +"printf('Itr\t U1\t U2\t U3\t U4\t U5\t U6\t U7\t U8\n-----------------------------------------------------------------------')\n", +"for l=0:15\n", +" printf('\n %i',l)\n", +" for i=1:8\n", +" if i==1 then\n", +" u1(i)=f(100,U(8),40,U(i+1))\n", +" elseif i==8\n", +" u1(i)=f(100,U(i-1),40,U(1))\n", +" else\n", +" u1(i)=f(100,U(i-1),40,U(i+1))\n", +" end\n", +" \n", +" end\n", +" for i=1:8\n", +" U(i)=u1(i)\n", +" printf('\t%.3f',U(i))\n", +" end\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 20.8: Spherical_Coordinate_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 20.8\n", +"//Spherical Coordinate System\n", +"//Page no. 697\n", +"clc;clear;close;\n", +"deff('y=cot(x)','y=1/tan(x)')\n", +"dr=5;r0=50;dth=%pi/4;dfi=%pi/4;N=-10;Z=60;Nb=0;Zt=70;\n", +"deff('y=f(u1,u2,u3,u4,u5,u6,th0)','y=((u1+u3)/dr^2+(u3-u1)/(r0*dr)+(u2+u4)/(r0*dth)^2+(u2*cot(th0)/(r0^2*dth)+(u5+u6)/(r0*sin(th0)*dfi)^2))/(2/dr^2+2/(r0*dth)^2+cot(th0)/(r0^2*dth)+2/(r0*sin(th0)*dfi)^2)') //laplace distribution in spherical coordinate\n", +"T1=40;T2=20;H1=35;H2=10;B1=5;B2=0;t1=240;t2=180;b1=100;b2=80;h1=210;h2=150\n", +"printf('\n----------------------------------------------------------------------------\n')\n", +"s=['T','H','B','t','h','b'];\n", +"for i=1:8\n", +" if i<4 | i>6 then\n", +" T(1,i)=T1;\n", +" H(1,i)=H1;\n", +" B(1,i)=B1;\n", +" b(1,i)=b1;\n", +" t(1,i)=t1;\n", +" h(1,i)=h1;\n", +" else\n", +" T(1,i)=T2;\n", +" H(1,i)=H2;\n", +" b(1,i)=b2;\n", +" B(1,i)=B2;\n", +" t(1,i)=t2;\n", +" h(1,i)=h2;\n", +" end\n", +"end\n", +"h(1)=0;h(2)=0;\n", +"Al=[T;H;B;t;h;b]\n", +"for i=1:6\n", +" if i==1 then\n", +" printf('Temperature Distribution in Outer Sphere\n')\n", +" printf('----------------------------------------------------------------------------\n')\n", +" end\n", +" if i==4 then\n", +" printf('\nTemperature Distribution in Inner Sphere\n')\n", +" printf('----------------------------------------------------------------------------\n')\n", +" end\n", +" printf('\nPoint : ')\n", +" for j=1:8\n", +" printf('\t%s%i',s(i),j)\n", +" end\n", +" printf('\nTemperature : ')\n", +" for j=1:8\n", +" if (j==1 | j==2) & i==5 then\n", +" printf('\t%s','?')\n", +" else\n", +" printf('\t%i',Al(i,j))\n", +" end\n", +" end\n", +" printf('\n----------------------------------------------------------------------------')\n", +"end\n", +"th0=10^-30\n", +"Uh1=f(1000,Al(5,8),Al(2,1),Al(5,2),Al(6,1),Al(4,1),th0)\n", +"disp(Uh1,'Uh1 = ')\n", +"th0=%pi/4;\n", +"Uh2=f(1000,Uh1,Al(2,2),Al(5,3),Al(6,2),Al(4,2),th0)\n", +"disp(Uh2,'Uh2 = ')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/21-Advances_in_Numerical_Methods_Using_Parallel_Computing_Paradigm.ipynb b/Numerical_Methods_Principles_by_Analysis/21-Advances_in_Numerical_Methods_Using_Parallel_Computing_Paradigm.ipynb new file mode 100644 index 0000000..de8b791 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/21-Advances_in_Numerical_Methods_Using_Parallel_Computing_Paradigm.ipynb @@ -0,0 +1,344 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 21: Advances in Numerical Methods Using Parallel Computing Paradigm" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 21.1: Parallel_Bisection_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 21.1\n", +"//Parallel Bisection Method\n", +"//Page no. 721\n", +"clc;close;clear;\n", +"\n", +"deff('y=f(x)','y=x^2-cos(x)')\n", +"a=0;b=1;e=0.0001;i=1;\n", +"printf('Itr\ta\tb\th\t\tx0\t\tx1\t\tx2\t\tx3\t\tx4\t\tx5\n\t\t\t\t\ty0\t\ty1\t\ty2\t\ty3\t\ty4\t\ty5')\n", +"printf('\n--------------------------------------------------------------------------------------------------------------------------------\n')\n", +"while (abs(a-b)>=e)\n", +" \n", +"\n", +" h=(b-a)/5;\n", +" y(1)=f(a);\n", +" x(1)=a;\n", +" printf(' %i\t%g\t%g\t%f\t%f',i,a,b,h,x(1))\n", +" for j=2:6\n", +" x(j)=x(j-1)+h;\n", +" y(j)=f(x(j));\n", +" if (y(j-1)*y(j)<0)\n", +" a=x(j-1);\n", +" b=x(j);\n", +" end\n", +" printf('\t%f',x(j))\n", +" end\n", +" printf('\n\t\t\t\t\t')\n", +" for j=1:6\n", +" printf('%f\t',y(j))\n", +" end\n", +" \n", +" printf('\n')\n", +" i=i+1;\n", +"end\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 21.2: Lagrange_Interpolation_in_Parallel_Computing.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 21.2\n", +"//Lagrange Interpolation in Parallel Computing\n", +"//Page no. 723\n", +"clc;close;clear;\n", +"\n", +"xi=[-1,0,2,5];\n", +"yi=[9,5,3,15];\n", +"s=['x=1','n=4','Data:','(-1,9)','(0,5)','(2,3)','(5,15)']\n", +"for i=1:4\n", +" printf('\tProcessor\t')\n", +"end\n", +"printf('\n')\n", +"for i=1:4\n", +" printf('\t N%i\t\t',i)\n", +"end\n", +"printf('\n')\n", +"for i=1:7\n", +" for j=1:4\n", +" printf(' %s\t\t',s(i))\n", +" end\n", +" printf('\n')\n", +"end\n", +"\n", +"x=1;T=0;\n", +"for k=0:3\n", +" p=yi(k+1)\n", +" for j=0:3\n", +" if(j~=k)\n", +" p=p*((x-xi(j+1))/(xi(k+1)-xi(j+1)))\n", +" end\n", +" end\n", +" T=T+p;\n", +" printf('\nT(%i) = %g',k+1,p)\n", +"end\n", +"printf('\n\nT = %g',T)\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 21.3: Trapezoidal_Rule_and_Simpsons_Rule_in_Parallel_Computing.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 21.3\n", +"//Trapezoidal Rule and Simpsons Rule in Parallel Computing\n", +"//Page no. 726\n", +"clc;close;clear;\n", +"n=8;a=0;b=8;\n", +"h=(b-a)/n\n", +"deff('y=f(x)','y=1/(1+x)')\n", +"for i=0:8\n", +" x(i+1)=i;\n", +" y(i+1)=f(x(i+1))\n", +"end\n", +"printf('xi\t ')\n", +"for i=1:9\n", +" printf('%i\t ',x(i))\n", +"end\n", +"printf('\n yi\t')\n", +"for i=1:9\n", +" printf('1/%i\t',i)\n", +"end\n", +"\n", +"//trapezoidal rule\n", +"S=0;\n", +"for i=1:9\n", +" if(i==1 | i==9)\n", +" S=S+y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +"end\n", +"S=S*h/2\n", +"printf('\n\nTrapezoidal Rule Sum = %g',S)\n", +"\n", +"//Simpsons 1/3rd Rule\n", +"S=0;\n", +"for i=1:9\n", +" if(i==1 | i==9)\n", +" S=S+y(i)\n", +" elseif(((i)/2)-fix((i)/2)==0)\n", +" S=S+4*y(i)\n", +" else\n", +" S=S+2*y(i)\n", +" end\n", +"end\n", +"S=S*h/3\n", +"printf('\n\nSimpsons 1/3rd Rule Sum = %g',S)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 21.4: Parallel_Gauss_Seidel_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 21.4\n", +"//Parallel Gauss-Seidel Method\n", +"//Page no. 730\n", +"clc;close;clear;\n", +"A=[3,2;6,2];\n", +"B=[2;3];\n", +"x(1)=1/4;\n", +"x(2)=1/5;\n", +"e=0.002;\n", +"old(1)=x(1);\n", +"old(2)=x(2);\n", +"new(1)=old(1);\n", +"new(2)=old(2);\n", +"printf('\t\tProcess 1\t\tProcess 2\n Itr\t\told\tnew1\t\told2\tnew2\n\n')\n", +"printf(' %i\t\t%g\t%g\t\t%g\t%g\n',0,old(1),new(1),old(2),new(2))\n", +"for i=1:4\n", +" printf(' %i',i)\n", +" for j=1:2\n", +" k=0;\n", +" for l=1:j-1\n", +" k=k-(A(j,l)*old(l));\n", +" end\n", +" m=0;\n", +" for l=j+1:2\n", +" m=m-(A(j,l)*old(l));\n", +" end\n", +" new(j)=(B(j)+k+m)/A(j,j)\n", +" printf('\t\t%.5g\t%.5g',old(j),new(j))\n", +" end\n", +" printf('\n')\n", +" old(1)=new(1)\n", +" old(2)=new(2)\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 21.5: Poissons_Partial_Differential_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 21.5\n", +"//Poissons Partial Differential Equation\n", +"//Page no. 733\n", +"clc;clear;close;\n", +"\n", +"s=['st','nd','rd']\n", +"for i=4:20\n", +" s(i)='th'\n", +"end\n", +"h=0.25;deff('y=f(x)','y=x^3');y=1;x=0;\n", +"for i=1:6\n", +" \n", +" \n", +" if i~=6 then\n", +" printf('%g\t|',y)\n", +" y=y-h;\n", +" x=0;\n", +" for j=1:5\n", +" if i==1 | i==5 | j==5 then\n", +" P(i,j)=f(x)\n", +" else\n", +" P(i,j)=0\n", +" end\n", +" printf('%f\t',P(i,j))\n", +" x=x+h;\n", +" end\n", +" else\n", +" printf('---------------------------------------------------------------------------------\n\t')\n", +" x=0;\n", +" for j=1:5\n", +" printf(' %g\t\t',x)\n", +" x=x+h\n", +" end\n", +" end\n", +" printf('\n')\n", +"end\n", +"\n", +"printf('\n\n\n')\n", +"\n", +"for l=0:17\n", +" y=1;\n", +" if l~=0 then\n", +" printf('After the %i%s Iteration : \n------------------------------------------------------\n %i',l,s(l),l)\n", +" for i=1:6\n", +" if i~=6 then\n", +" printf('\t%g',y)\n", +" y=y-h\n", +" for j=1:5\n", +" printf('\t%.3f',P(i,j))\n", +" end\n", +" else\n", +" x=0;\n", +" printf('\t')\n", +" for j=1:5\n", +" printf('\t%g',x)\n", +" x=x+h \n", +" end\n", +" end\n", +" printf('\n')\n", +" end\n", +" printf('------------------------------------------------------\n')\n", +"end\n", +"y=0;\n", +" for i=4:-1:2\n", +" y=y+h\n", +" for j=2:4\n", +" P1(i,j)=(P(i,j+1)+P(i,j-1)+P(i-1,j)+P(i+1,j)-h^2*y)/4\n", +" end\n", +" end\n", +" for i=4:-1:2\n", +" for j=2:4\n", +" P(i,j)=P1(i,j)\n", +" end\n", +" end\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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/22-Numerical_Methods_Using_Neural_Networks.ipynb b/Numerical_Methods_Principles_by_Analysis/22-Numerical_Methods_Using_Neural_Networks.ipynb new file mode 100644 index 0000000..6bcd6af --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/22-Numerical_Methods_Using_Neural_Networks.ipynb @@ -0,0 +1,257 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 22: Numerical Methods Using Neural Networks" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.1: MLP_Algorithm.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.1\n", +"//MLP Algorithm\n", +"//Page no. 748\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=1/(1+exp(-x))')\n", +"Wih=[0.1,-0.3;0.3,0.4];\n", +"Who=[0.4;0.5]\n", +"i=[0.2,0.6];\n", +"t=0.7;\n", +"a=10;\n", +"for k=1:3\n", +" printf('\n\n\nAfter Iteration %i :\n\n',k)\n", +" disp(Wih,'Wih = ')\n", +" disp(Who,'Who = ')\n", +"a1=i*Wih;\n", +"disp(a1,'a = ')\n", +"h=[f(a1(1)),f(a1(2))]\n", +"disp(h,'h = ')\n", +"b1=h*Who\n", +"disp(b1,'b1 =')\n", +"o=f(b1)\n", +"disp(o,'o = ')\n", +"d=o*(1-o)*(t-o)\n", +"disp(d,'d =')\n", +"for j=1:2\n", +" e(1,j)=h(j)*(1-h(j))*d*Who(j)\n", +"end\n", +"disp(e,'e =')\n", +"dWho=a*h'*d;\n", +"disp(dWho,'dWho =')\n", +"Who=Who+dWho;\n", +"dWih=a*i'*e;\n", +"disp(dWih,'dWih =')\n", +"Wih=Wih+dWih;\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.2: MLP.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.2\n", +"//MLP\n", +"//Page no. 758\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^3-x^2+x-1')\n", +"printf('Input\t\tDesired\t\tNetwork\t\tError\n\t\t Output\t\t Output\n---------------------------------------------------------\n')\n", +"in=[0.7572,0.7601,0.7620,1.4831,1.4874,1.4900,2.0913,2.0934,2.1006,2.8391,2.8496,2.8529,3.4555,3.4631,3.4654,4.1547,,4.1839,4.1874]\n", +"n=[-0.3941,-0.3896,-0.3867,1.6054,1.6259,1.6391,5.8762,5.8969,5.9685,16.6462,16.8542,16.9188,31.7640,31.9928,32.0646,57.6220,58.9268,59.0866]\n", +"for i=1:18\n", +" printf(' %.4f\t\t%.4f\t\t%.4f\t\t%.4f\n',in(i),f(in(i)),n(i),n(i)-f(in(i)))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.3: Bisection_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.3\n", +"//Bisection Method\n", +"//Page no. 764\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^3-x^2+x-1')\n", +"printf('N01\tN02\tN11\tN12\tN21\tNet31\tO31\tN41\tN42\n------------------------------------------------------------------------\n')\n", +"N01=[0,1,0.5,0.75,0.875,0.938,0.969,0.984,0.992,0.996,0.998,0.999,1,1]\n", +"N02(1)=2\n", +"for i=2:13\n", +" N02(i)=1;\n", +"end\n", +"for i=1:13\n", +" net31(i)=f(N01(i+1))*f(N01(i))\n", +" if net31(i)>0 then\n", +" O31(i)=1;\n", +" else\n", +" O31(i)=0;\n", +" end\n", +" N41(i)=(1-O31(i))*(N01(i))+O31(i)*N01(i+1)\n", +" N42(i)=(1-O31(i))*N01(i+1)+O31(i)*N02(i)\n", +" if i==2 then\n", +" printf('%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',0,N02(i),f(N01(i)),N01(i+1),f(N01(i+1)),net31(i),O31(i),N41(i),N42(i))\n", +" else\n", +" printf('%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',N01(i),N02(i),f(N01(i)),N01(i+1),f(N01(i+1)),net31(i),O31(i),N41(i),N42(i))\n", +"end\n", +"\n", +"end\n", +"printf('\n\nTherefore the solution is %.3f',N42(13))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.4: Hopfield_Neural_Network.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.4\n", +"//Hopfield Neural Network\n", +"//Page no. 766\n", +"clc;clear;close;\n", +"\n", +"A=[1,2,1;-1,1,1;1,0,-1];\n", +"disp(inv(A),'Inverse of A =',A,'A =')\n", +"for i=1:3\n", +" for j=1:3\n", +" k=0;\n", +" for l=1:3\n", +" k=k+A(i,l)*A(j,l)\n", +" end\n", +" T(i,j)=k;\n", +" end\n", +"end\n", +"disp(T,'T =')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.5: RBF_Network.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.5\n", +"//RBF Network\n", +"//Page no. 773\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=10*sin(x)')\n", +"printf('Input\t\tDesired\t\tNetwork\t\tError\n\t\t Output\t\t Output\n---------------------------------------------------------\n')\n", +"in=[0.7053,0.7060,0.7097,1.5056,1.5103,1.5377,2.2481,2.2514,2.2599,3.7076,3.7229,3.7364,5.0002,5.0022,5.0028,6.3749,6.3799,6.3886]\n", +"n=[6.4828,6.4883,6.5164,9.9786,9.9816,9.9944,7.7926,7.7718,7.7180,-5.3625,-5.4910,-5.6034,-9.5884,-9.5828,-9.5809,0.9163,0.9663,1.0527]\n", +"for i=1:18\n", +" printf(' %.4f\t\t%.4f\t\t%.4f\t\t%.4f\n',in(i),f(in(i)),n(i),f(in(i))-n(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 22.7: First_Order_ODE.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 22.7\n", +"//First Order ODE\n", +"//Page no. 783\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=(exp(-x^2/2))/(1+x+x^3)')\n", +"printf('Test Points\tActual Solution \tEstimated Solution\tError\n x\t\twa(x)\t\t\twt(x)\t\t\tdw(x)\n------------------------------------------------------------------------\n')\n", +"x=[0.1054,0.1091,0.2693,0.2703,0.3067,0.3088,0.4268,0.4284,0.5098,0.5139];\n", +"e=[0.1027,0.1063,0.2513,0.2522,0.2832,0.2849,0.3792,0.3805,0.4398,0.4427]\n", +"for i=1:10\n", +" printf(' %.4f \t%.4f \t\t%.4f\t\t\t%.4f\t\n',x(i),(1-f(x(i))),e(i),-e(i)+(1-f(x(i))))\n", +"end\n", +"printf('\n\n\nExperimental result varying from calculated result')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/3-Errors_and_Their_Propagation.ipynb b/Numerical_Methods_Principles_by_Analysis/3-Errors_and_Their_Propagation.ipynb new file mode 100644 index 0000000..9fd0544 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/3-Errors_and_Their_Propagation.ipynb @@ -0,0 +1,178 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 3: Errors and Their Propagation" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.1: Limiting_Error.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 3.1\n", +"//Limiting Error\n", +"//Page no. 45\n", +"clc;clear;close;\n", +"R=1000;\n", +"e=0.1*1000; //limiting error calculation\n", +"printf('Magnitude of the Resistor resistence (R) =\n%i <= R <= %i',R-e,R+e)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.2: Known_Error.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 3.2\n", +"//Known Error\n", +"//Page no. 46\n", +"clc;clear;close;\n", +"l=28;d=5;\n", +"v=%pi*l*(d/2)^2;\n", +"printf('\nVolume of Cylinder= %f cu. cm',v);\n", +"re_d=0.1;re_l=-0.5;\n", +"re_v=2*re_d+re_l; //relative error computation\n", +"printf('\n\nRelative error in volume= %f %%',re_v);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.3: Absolute_Relative_and_Percentage_Errors.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 3.3\n", +"//Absolute, Relative and Percetage Errors\n", +"//Page no. 48\n", +"clc;clear;close;\n", +"x=0.00006;x1=0.00005;\n", +"ex=x-x1; //absolute error\n", +"Ex=ex/x1; //relative error\n", +"px=100*Ex; //percentage error \n", +"printf('\nAbsolute Error= %f\nRelative Error= %f\nPercentage Error= %f %%',ex,Ex,px);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.4: Absolute_Relative_and_Percentage_Errors.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 3.4\n", +"//Absolute, Relative and Percetage Errors\n", +"//Page no. 48\n", +"clc;clear;close;\n", +"x=100500;x1=100000;\n", +"ex=x-x1; //absolute error\n", +"Ex=ex/x1; //relative error\n", +"px=100*Ex; //percentage error \n", +"printf('\nAbsolute Error= %f\nRelative Error= %f\nPercentage Error= %f %%',ex,Ex,px);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3.5: Absolute_Relative_and_Percentage_Errors.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 3.5\n", +"//Absolute, Relative and Percentage Errors\n", +"//Page no. 52\n", +"clc;clear;close;\n", +"x=9.12345;y=7.654321;\n", +"x1=9.1234;y1=7.6543; //on a 5 decimal computer\n", +"ex=x-x1; //absolute error of x\n", +"ey=y-y1; //absolute error of y\n", +"z1=x1+y1;\n", +"printf('\nAbsolute Error in x= %f',ex);\n", +"printf('\nAbsolute Error in y= %f',ey);\n", +"printf('\nAddition on a 5 decimal computer yields= %.5g',z1);\n", +"z2=16.777;\n", +"printf('\nAbsolute Total Error= %f',x+y-z2);\n", +"printf('\nAbsolute Propagated Error= %f',x+y-z1);\n", +"printf('\nAbsolute Round-off Error= %.4g',z1-z2);\n", +"printf('\nRealtive Total Error= %.4g',(x+y-z2)/(x+y));\n", +"printf('\nRelative Propagated Error= %.2g',(x+y-z1)/(x+y));\n", +"printf('\nRelative Round-off Error= %.3g',(z1-z2)/(x+y));\n", +"printf('\nBound on the propagated relative error= %f',2*10^-4);\n", +"printf('\nBound on the total relative error= %f',3*10^-4);\n", +"printf('\nAs we can see that both the propagated and total relative error are less than their bound values')" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/4-Programming_Tools_and_Techniques.ipynb b/Numerical_Methods_Principles_by_Analysis/4-Programming_Tools_and_Techniques.ipynb new file mode 100644 index 0000000..ca962cb --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/4-Programming_Tools_and_Techniques.ipynb @@ -0,0 +1,143 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 4: Programming Tools and Techniques" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.1: Quadratic_Equatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 4.1\n", +"//Quadratic Equation\n", +"//Page no. 96\n", +"clc;clear;close;\n", +"a=input('Enter value of a= ');\n", +"b=input('Enter vlaue of b= ');\n", +"c=input('Enter value of c= ');\n", +"x1=(-1*b+sqrt((b^2)-4*a*c))/(2*a); //1st root\n", +"x2=(-1*b-sqrt((b^2)-4*a*c))/(2*a); //2nd root\n", +"printf('\n1st Root= %f', x1);\n", +"printf('\n2nd Root= %f', x2);" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4.2: Database_Management.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 4.2\n", +"//Database Management\n", +"//Page no. 112\n", +"clc;clear;close;\n", +"M=[12,25,21,35;25,7,23,29;10,27,7,36;26,26,26,35;29,0,23,30]; //marks\n", +"\n", +"//calculation of composite score\n", +"for i=1:5,\n", +" j=1;k=0;\n", +" max1=M(i,j);\n", +" if(max1<M(i,j+1))\n", +" max1=M(i,j+1)\n", +" else\n", +" k=1;\n", +" end,\n", +" \n", +" if(M(i,j+2)>M(i,j+k))\n", +" max2=M(i,j+2);\n", +" else\n", +" max2=M(i,j);\n", +" end,\n", +" CS(i,1)=max1+max2+M(i,4);\n", +"end\n", +"\n", +"I=['Reg. No.','Name of Students','Test 1','Test 2','Test 3','Final';\n", +"'CS/01','C.V.Rajan','12','25','21','35';\n", +"'CS/02','B.X.Roy','25','07','23','29';\n", +"'CS/03','P.C.Sasikumar','10','27','07','36';\n", +"'CS/04','B.D.Box','26','26','26','35';\n", +"'CS/05','K.K.Mukherjee','29','0','23','30';]\n", +"printf('\n')\n", +"for i=1:6\n", +" for j=1:6\n", +" if(j>2)\n", +" printf('\t')\n", +" end\n", +" \n", +" printf('%s ',I(i,j));\n", +" if(i~=1)\n", +" if(j>2)\n", +" printf('\t')\n", +" end\n", +" printf(' ')\n", +" \n", +" end\n", +" if(i==1 & j==6)\n", +" printf('Composite Score\n')\n", +" end\n", +" \n", +" end\n", +" \n", +" if(i~=1)\n", +" printf('%i\n',CS(i-1,1));\n", +"end\n", +"\n", +"end\n", +"//disp(CS,'Composite Score',I);\n", +"max1=CS(1,1);j=1;\n", +"for i=2:5\n", +" if(max1<CS(i,1))\n", +" max1=CS(i,1);j=i;\n", +" end,\n", +"end\n", +"printf('\n\nTopper is:\n%s\t%s\t%s',I(1,1),I(1,2),'Composite Score')\n", +"printf('\nCS/0%i\t\t%s\t\t\t%i',j,I(j+1,2),CS(j,1))" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/5-Solutions_of_Algebraic_and_Transcendental_Equations.ipynb b/Numerical_Methods_Principles_by_Analysis/5-Solutions_of_Algebraic_and_Transcendental_Equations.ipynb new file mode 100644 index 0000000..55b874c --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/5-Solutions_of_Algebraic_and_Transcendental_Equations.ipynb @@ -0,0 +1,1738 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 5: Solutions of Algebraic and Transcendental Equations" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.10: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.10\n", +"//Newton Raphson Method\n", +"//Page no. 167\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x*exp(-x)')\n", +"deff('x=f1(x)','x=exp(-x)-x*exp(-x)')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=2;e=0.00001\n", +"for i=1:11\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, this is not convergent (i.e.) divergent')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.11: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.11\n", +"//Newton Raphson Method\n", +"//Page no. 167\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x^3-x-3')\n", +"deff('x=f1(x)','x=3*x^2-1')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=0;e=0.00001\n", +"for i=1:11\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, it is cyclic in nature')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.12: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.12\n", +"//Newton Raphson Method\n", +"//Page no. 168\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=atan(x)')\n", +"deff('x=f1(x)','x=1/(1+x^2)')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=1.45;e=0.00001\n", +"for i=1:12\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.5g \t\t%.5g\t\t%.5g \t\t%.5g \t\t%.5g\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, it is divergent')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.13: Secant_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.13\n", +"//Secant Method\n", +"//Page no. 170\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=exp(x)-3*x-sin(x)')\n", +"deff('x=f1(x)','x=exp(x)-3-cos(x)')\n", +"printf('n\txn\t\tf(xn)\t\tXn+1\t\tf(Xn+1)\t\tXn+2\t\tError\n')\n", +"printf('----------------------------------------------------------------------------------------------------------\n')\n", +"x0=0.567123008;x1=1;e=0.00001\n", +"for i=1:9\n", +" x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0))\n", +" e1=abs(x0-x2)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),x1,f(x1),x2,e1)\n", +" x0=x1;\n", +" x1=x2\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, the root is %.10f',x2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.14: Kizner_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.14\n", +"//Kizner Method\n", +"//Page no. 172\n", +"clc;clear;close;\n", +"h2=0.00001\n", +"deff('x=f(x)','x=2*x-3-cos(x)')\n", +"deff('y=f1(x,y)','y=h2/(-x+y)') //function for differentiation\n", +"printf('n\th\tc\txn\t\tf(xn)\t\tF(xn)\t\tk1\t\t v\t\tXn+1\n')\n", +"printf('--------------------------------------------------------------------------------------------------------------------\n')\n", +"x0=2;e=0.00001;h=0.5;c=0.5;\n", +"for i=1:11\n", +" h1=-f(x0);\n", +" F=f1(f(x0),f(x0+h2))\n", +" k1=h1*F/2;\n", +" v=h*f(x0)/(c*(f(x0+c+h)-f(x0+c)))-k1/c;\n", +" a=0;\n", +" for j=0:3\n", +" a=a+(v^j)/factorial(j+1)\n", +" end\n", +" x1=x0+k1*a\n", +" printf(' %i\t%g\t%g\t%.6f\t%.6f\t%.6f\t%.8f\t %.5f\t%.6f\n',i-1,h,c,x0,f(x0),F,k1,v,x1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, the solution is %.10f',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.15: Brent_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.15\n", +"//Brent Method\n", +"//Page no. 173\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=x^2+x-2')\n", +"x1=0;x2=0.5;x3=2;\n", +"r=f(x2)/f(x3);s=f(x2)/f(x1);t=f(x1)/f(x3);\n", +"q=(t-1)*(r-1)*(s-1);\n", +"p=r*t*(s-1)*(x2-x3)-s*(1-r)*(x2-x1)+(t*s-r)*x2\n", +"printf('Root is : %.10g',x2+(p/q))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.19: Horner_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.19\n", +"//Horner Method\n", +"//Page no. 177\n", +"clc;clear;close;\n", +"deff('y=f(x,a1,a2,a3,a4)','y=a1*x^3+a2*x^2+a3*x+a4')\n", +"\n", +"k=1;m=2;\n", +"a=[4;-13;-31;-275];\n", +"for i=1:10\n", +" s=1;\n", +" si=f(s,a(1),a(2),a(3),a(4))*abs(1/f(s,a(1),a(2),a(3),a(4)))\n", +" while 1\n", +" a1=f(s,a(1),a(2),a(3),a(4))*abs(1/f(s,a(1),a(2),a(3),a(4)))\n", +" if si~=a1 then\n", +" d(i)=s-1\n", +" break\n", +" end\n", +" si=a1;\n", +" s=s+1;\n", +" end \n", +" b(1)=a(1)\n", +" for j=1:3\n", +" for k=1:4-j\n", +" b(k+1)=a(k+1)+b(k)*d(i)\n", +" a(k+1)=b(k+1)\n", +" end\n", +" end\n", +" for j=1:3\n", +" a(j+1)=10^j*a(j+1)\n", +" end\n", +"end\n", +"printf('The positive root is %i.',d(1))\n", +"for i=2:10\n", +" printf('%i',d(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.1: Bisection_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.1\n", +"//Bisection Method\n", +"//Page no. 145\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=2^x-3*x')\n", +"x1=0;x2=2;e=0.001;i=0;\n", +"printf('Iteration\tx1\t\tx2\t\tz\t\tf(z)\n')\n", +"printf('--------------------------------------------------------------------------\n')\n", +"while abs(x1-x2)>e\n", +" z=(x1+x2)/2\n", +" printf(' %i\t\t%f\t%f\t%f\t%f\n',i,x1,x2,z,f(z))\n", +" if f(z)*f(x1)>0\n", +" x1=z\n", +" else\n", +" x2=z\n", +" end\n", +" i=i+1\n", +"end\n", +"printf('\n\nThe solution of this equation is %g after %i Iterations',z,i-1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.20: Laguerre_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.20\n", +"//Laguerre Method\n", +"//Page no. 180\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=x^3+x^2+10*x-20')\n", +"deff('y=f1(x)','y=3*x^2+2*x+10')\n", +"deff('y=f2(x)','y=6*x+2')\n", +"n=3;\n", +"printf('i\tn\txi\t\tP(x)\t\tP1(x)\t\tP2(x)\t\tProot\t\tNroot\n')\n", +"printf('---------------------------------------------------------------------------------------------------------\n')\n", +"xi=1\n", +"for i=0:9\n", +" Proot=xi-(n*f(xi))/(f1(xi)+sqrt((n-1)*f1(xi)^2-n*f(xi)*f2(xi)))\n", +" Nroot=xi-(n*f(xi))/(f1(xi)-sqrt((n-1)*f1(xi)^2-n*f(xi)*f2(xi)))\n", +" printf(' %i\t%i\t%f\t%f\t%f\t%f\t%f\t%f\n',i,n,xi,f(xi),f1(xi),f2(xi),Proot,Nroot)\n", +" xi=Proot\n", +"end\n", +"printf('\n\nProot = %f\n\nNroot = %f',Proot,Nroot)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.21: Mullers_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.21\n", +"//Mullers Method\n", +"//Page no. 182\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^3-x-4')\n", +"zi=[1;2;3];\n", +"s=['i','z2','z0','z1','f2','f0','f1','a0','a1','a2','zr+','zr-']\n", +"li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", +"hi(1)=zi(3,1)-zi(2,1);\n", +"for i=2:6\n", +" for j=1:3\n", +" fz(j,i-1)=f(zi(j,i-1))\n", +" end\n", +" di(i-1)=1+li(i-1)\n", +" gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", +" D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" if abs(D1(i-1))>abs(D2(i-1)) then\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", +" else\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", +" end\n", +" hi(i)=li(i)*hi(i-1);\n", +" z(i-1)=zi(3,i-1)+hi(i)\n", +" for j=1:2\n", +" zi(j,i)=zi(j+1,i-1)\n", +" end\n", +" zi(3,i)=z(i-1)\n", +"end\n", +"for i=1:12\n", +" if i==1 then\n", +" printf(s(i))\n", +" for j=1:5\n", +" printf('\t\t\t%i',j-1)\n", +" end\n", +" elseif i<=4\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',zi(i-1,j))\n", +" end\n", +" elseif i<=7\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',fz(i-4,j))\n", +" end\n", +" elseif i<=8\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',li(j))\n", +" end\n", +" elseif i<=9\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',di(j))\n", +" end\n", +" elseif i<=10\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',gi(j))\n", +" end\n", +" elseif i<=11\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',z(j))\n", +" end\n", +" elseif i<=12\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',zi(j))\n", +" end\n", +" end\n", +"end\n", +"printf('\n\nAt the end of the %i iteration, the root of the equation is %.10f',j-2,z(j))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.22: Mullers_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.22\n", +"//Mullers Method\n", +"//Page no. 183\n", +"clc;clear;close;\n", +"\n", +"deff('y=f(x)','y=x^3-x-4')\n", +"zi=[1;2;3];\n", +"s=['i','z0','z1','z2','f0','f1','f2','li','di','gi','li+1','hi','hi+1','zi+1','D+','D_']\n", +"li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", +"hi(1)=zi(3,1)-zi(2,1);\n", +"for i=2:6\n", +" for j=1:3\n", +" fz(j,i-1)=f(zi(j,i-1))\n", +" end\n", +" di(i-1)=1+li(i-1)\n", +" gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", +" D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" if abs(D1(i-1))>abs(D2(i-1)) then\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", +" else\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", +" end\n", +" hi(i)=li(i)*hi(i-1);\n", +" z(i-1)=zi(3,i-1)+hi(i)\n", +" for j=1:2\n", +" zi(j,i)=zi(j+1,i-1)\n", +" end\n", +" zi(3,i)=z(i-1)\n", +"end\n", +"for i=1:16\n", +" if i==1 then\n", +" printf(s(i))\n", +" for j=1:5\n", +" printf('\t\t\t%i',j-1)\n", +" end\n", +" elseif i<=4\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',zi(i-1,j))\n", +" end\n", +" elseif i<=7\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',fz(i-4,j))\n", +" end\n", +" elseif i<=8\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',li(j))\n", +" end\n", +" elseif i<=9\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',di(j))\n", +" end\n", +" elseif i<=10\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',gi(j))\n", +" end\n", +" elseif i<=11\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',li(j+1))\n", +" end\n", +" elseif i<=12\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',hi(j))\n", +" end\n", +" elseif i<=13\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',hi(j+1))\n", +" end\n", +" elseif i<=14\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',z(j))\n", +" end\n", +" elseif i<=15\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',D1(j))\n", +" end\n", +" elseif i<=16\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',D2(j))\n", +" end\n", +" end\n", +"end\n", +"printf('\n\nAt the end of the %ith iteration, the root of the equation is %.10f',j-1,z(j))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.23: Bairstow_Hitchcock_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.23\n", +"//Bairstow Hitchcock Method\n", +"//Page no. 187\n", +"clc;clear;close;\n", +"deff('y=f(x,p,q)','y=x^2+p*z+q')\n", +"a=[1,-1,1,-1,1]\n", +"a=a';a=[a,a,a,a,a]\n", +"printf('Iteration-->')\n", +"for i=1:5\n", +" printf('\t%i\t',i)\n", +"end\n", +"printf('\n------------------------------------------------------------------------------------------')\n", +"p(1,1)=-1.2;q(1,1)=0.95;\n", +"s=['b1','b2','b3','b4','c1','c2','c3','c4','c','dp','dq','p','q']\n", +"//s1=[b1;b2;b3;b4;c1;c2;c3;c4;c;dp;dq;p;q]\n", +"for i=1:5\n", +" b(1,i)=0;b(2,i)=a(1,i);c(1,i)=0;c(2,i)=a(1,i);\n", +" for k=1:4\n", +" b(k+2,i)=a(k+1,i)-p(1,i)*b(k+1,i)-q(1,i)*b(k,i)\n", +" c(k+2,i)=b(k+2,i)-p(1,i)*c(k+1,i)-q(1,i)*c(k,i)\n", +" end\n", +" cb(1,i)=c(6,i)-b(6,i);\n", +" dq(1,i)=(b(6,i)*c(4,i)-b(5,i)*cb(1,i))/(c(4,i)^2-cb(1,i)*c(3,i))\n", +" dp(1,i)=(b(5,i)*c(4,i)-b(6,i)*c(3,i))/(c(4,i)^2-cb(1,i)*c(3,i))\n", +" p(1,i+1)=p(1,i)+dp(1,i);q(1,i+1)=q(1,i)+dq(1,i);\n", +"end\n", +"for j=1:13\n", +" printf('\n %s\t\t',s(j))\n", +" if j<5 then\n", +" for i=1:5\n", +" printf('%.9f\t',b(j+2,i))\n", +" end\n", +" elseif j<9 then\n", +" for i=1:5\n", +" printf('%.9f\t',c(j-2,i))\n", +" end\n", +" elseif j<10\n", +" for i=1:5\n", +" printf('%.9f\t',cb(1,i))\n", +" end\n", +" elseif j<11\n", +" for i=1:5\n", +" printf('%.9f\t',dp(1,i))\n", +" end\n", +" elseif j<12\n", +" for i=1:5\n", +" printf('%.9f\t',dq(1,i))\n", +" end\n", +" elseif j<13\n", +" for i=1:5\n", +" printf('%.9f\t',p(1,i+1))\n", +" end\n", +" else\n", +" for i=1:5\n", +" printf('%.9f\t',q(1,i+1))\n", +" end\n", +" end\n", +"end\n", +"z=poly(0,'z');\n", +"a=f(z,p(1,i+1),q(1,i+1));\n", +"printf('\n\nRoots for Quadratic Equation Q = ')\n", +"disp(a)\n", +"a=roots(a)\n", +"printf('\n\tare\n')\n", +"disp(a(1))\n", +"disp(a(2))\n", +"\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.24: Bernoulli_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.24\n", +"//Bernoulli Method\n", +"//Page no. 189\n", +"clc;clear;close;\n", +"\n", +"a=[1,-8,-15,10];\n", +"for i=1:2\n", +" c(i)=0;\n", +"end\n", +"c(3)=1;\n", +"for k=4:13\n", +" c(k)=-(a(2)*c(k-1)+a(3)*c(k-2)+a(4)*c(k-3))\n", +" r(k-3)=c(k)/c(k-1) \n", +"end\n", +"disp(c,'Ck Values')\n", +"disp(r,'Rk Values')\n", +"disp(r(k-3),'Therefore the exact root is = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.25: Graeffe_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.25\n", +"//Graeffe Method\n", +"//Page no. 191\n", +"clc;clear;close;\n", +"\n", +"a=[1,-6,11,-6]\n", +"k=0;\n", +"for k=2:6\n", +" for i=1:4\n", +" a(k,i)=(-1)^(i-1)*(a(k-1,i))^2\n", +" j=1;\n", +" while i+j<5 & i+j>2\n", +" a(k,i)=a(k,i)+(-1)^(i-j-1)*2*(a(k-1,i-j))*a(k-1,i+j)\n", +" break\n", +" j=j+1;\n", +" end\n", +" end\n", +"end\n", +"printf('\t\t\t\ta1\t\t\t\ta2\t\t\t\ta3\n k\ta0\ta1\t\t--\t\ta2\t\t--\t\ta3\t\t--\t\n\t\t\t\ta0\t\t\t\ta1\t\t\t\ta2')\n", +"printf('\n----------------------------------------------------------------------------------------------------\n')\n", +"for i=1:4\n", +" printf(' %i\t%g\t%.4g\t\t%.5g\t\t%.9g\t\t%.8g\t%g\t\t%.10g\n',i-1,a(i,1),a(i,2),abs(a(i,2)/a(i,1))^(1/(2^(i-1))),a(i,3),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),a(i,4),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))\n", +"end\n", +"for i=5:6\n", +" printf(' %i\t%g\t%.4g\t%.5g\t\t%.9g\t%.8g\t%.7g\t%.10g\n',i-1,a(i,1),a(i,2),abs(a(i,2)/a(i,1))^(1/(2^(i-1))),a(i,3),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),a(i,4),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))\n", +"end\n", +"printf('\n\nThe Absolute Values of the roots are %g, %.8g and %g',abs(a(i,2)/a(i,1))^(1/(2^(i-1))),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.26: QD_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.26\n", +"//QD Method\n", +"//Page no. 194\n", +"clc;clear;close;\n", +"\n", +"a=[32,-48,18,-1]\n", +"for i=1:5\n", +" e(i,1)=0;\n", +" e(i,4)=0;\n", +"end\n", +"q(1,1)=-a(2)/a(1);\n", +"q(1,2)=0;q(1,3)=0;\n", +"e(1,2)=a(3)/a(2);\n", +"e(1,3)=a(4)/a(3);\n", +"for i=2:16\n", +" for j=1:3\n", +" q(i,j)=e(i-1,j+1)+q(i-1,j)-e(i-1,j)\n", +" end\n", +" for j=1:2\n", +" e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j)\n", +" end\n", +"end\n", +"printf('e0\t\tq1\t\te1\t\tq2\t\te2\t\tq3\t\te3\n')\n", +"printf('------------------------------------------------------------------------------------------------------------\n')\n", +"for i=1:14\n", +" for j=1:3\n", +" printf('\t\t%.10f\t',q(i,j))\n", +" end\n", +" printf('\n')\n", +" for j=1:4\n", +" printf('%.10f\t\t\t',e(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\t\t%.10f\t\t\t%.10f\t\t\t%.10f\n',q(15,1),q(15,2),q(15,3))\n", +"printf('\nThe exact roots are \t%.10f and %.10f',q(15,1),q(15,3))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.27: Linear_Iteration_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.27\n", +"//Linear Iteration Method\n", +"//Page no. 198\n", +"clc;clear;close;\n", +"\n", +"deff('x=f(x)','x=20/(x^2+2*x+10)')\n", +"printf('n\tx\t\tf(x)\n')\n", +"printf('-------------------------------------\n')\n", +"x=1;\n", +"for i=1:19\n", +" printf(' %i\t%.10f\t%.10f\n',i,x,f(x))\n", +" x1=x;\n", +" x=f(x);\n", +"end\n", +"printf('\n\nx = %.10f',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.28: Aitkens_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.28\n", +"//Aitkens Method\n", +"//Page no. 199\n", +"clc;clear;close;\n", +"\n", +"deff('x=f(x)','x=20/(x^2+2*x+10)')\n", +"printf('n\tx0\t\tx1\t\tx2\t\tx3\t\ty\t\tdx0\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=1;e=0.0001\n", +"for i=1:3\n", +" x1=f(x0);x2=f(x1);x3=f(x2);\n", +" y=x3-((x3-x2)^2)/(x3-2*x2+x1)\n", +" dx0=y-x0;\n", +" \n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i,x0,x1,x2,x3,y,dx0)\n", +" x0=y;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,y)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.29: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.29\n", +"//Newton Raphson Method\n", +"//Page no. 199\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x^3+2*x^2+10*x-20')\n", +"deff('x=f1(x)','x=3*x^2+4*x+10')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=01;e=0.00001\n", +"for i=1:4\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.2: Bisection_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.2\n", +"//Bisection Method\n", +"//Page no. 147\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=x^x-2*x+2')\n", +"x1=0;x2=2;e=0.001;i=0;\n", +"printf('Iteration\tx1\t\tx2\t\tz\t\tf(z)\n')\n", +"printf('--------------------------------------------------------------------------\n')\n", +"while abs(x1-x2)>e\n", +" z=(x1+x2)/2\n", +" printf(' %i\t\t%f\t%f\t%f\t%f\n',i,x1,x2,z,f(z))\n", +" if f(z)*f(x1)>0\n", +" x1=z\n", +" else\n", +" x2=z\n", +" end\n", +" i=i+1\n", +"end\n", +"printf('\n\nThe solution of this equation is %g after %i Iterations',z,i-1)\n", +"\n", +"printf('\n\n\nNote : There are computational errors in the answer given by the book for this example')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.31: Secant_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.31\n", +"//Secant Method\n", +"//Page no. 200\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=(x-0.6)*(x-1.3)^2*(x-2)^3+0.01234*log(x)')\n", +"printf('n\txn\t\tf(xn)\t\tXn+1\t\tf(Xn+1)\t\tXn+2\t\tError\n')\n", +"printf('----------------------------------------------------------------------------------------------------------\n')\n", +"x0=0.1;x1=1.2;e=0.00001\n", +"for i=1:7\n", +" x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0))\n", +" e1=abs(x0-x2)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),x1,f(x1),x2,e1)\n", +" x0=x1;\n", +" x1=x2\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, the root is %.10f',x2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.32: Regula_Falsi_Newton_Raphson_and_Mullers_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.32\n", +"//Regula Falsi, Newton Raphson and Mullers Method\n", +"//Page no. 201\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x^5-3.7*x^4+7.4*x^3-10.8*x^2+10.8*x-6.8')\n", +"deff('x=f1(x)','x=5*x^4-4*3.7*x^3+3*7.4*x^2-21.6*x+10.8')\n", +"//newton raphson\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=1.5;e=0.00001\n", +"for i=1:4\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation by newton raphshon after %i Iterations is %.10f\n\n\n',i,x1)\n", +"\n", +"//regula falsi\n", +"x1=1;x2=2;e=0.00001\n", +"printf('n\tx1\t\tf(x1)\t\tx2\t\tf(x2)\t\tx3\t\tf(x3)')\n", +"printf('\n-------------------------------------------------------------------------------------------------\n')\n", +"for i=0:7\n", +" x3=x2*f(x1)/(f(x1)-f(x2))+x1*f(x2)/(f(x2)-f(x1))\n", +" printf(' %i\t%f\t%f\t%f\t%f\t%f\t%f\n',i,x1,f(x1),x2,f(x2),x3,f(x3))\n", +" if f(x1)*f(x3)>0 then\n", +" x1=x3\n", +" else\n", +" x2=x3\n", +" end\n", +" if abs(f(x3))<e then\n", +" break\n", +" end\n", +"end\n", +"printf('\n\nTherefore the solution by regula falsi method after %i iterations is %.10g',i,x3)\n", +"\n", +"//mullers method\n", +"zi=[1;2;3];\n", +"s=['i','z0','z1','z2','f0','f1','f2','li','di','gi','li+1','hi','hi+1','zi+1','D+','D_']\n", +"li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", +"hi(1)=zi(3,1)-zi(2,1);\n", +"for i=2:6\n", +" for j=1:3\n", +" fz(j,i-1)=f(zi(j,i-1))\n", +" end\n", +" di(i-1)=1+li(i-1)\n", +" gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", +" D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" if abs(D1(i-1))>abs(D2(i-1)) then\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", +" else\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", +" end\n", +" hi(i)=li(i)*hi(i-1);\n", +" z(i-1)=zi(3,i-1)+hi(i)\n", +" for j=1:2\n", +" zi(j,i)=zi(j+1,i-1)\n", +" end\n", +" zi(3,i)=z(i-1)\n", +"end\n", +"printf('\n\n ')\n", +"for i=1:16\n", +" if i==1 then\n", +" printf(s(i))\n", +" for j=1:5\n", +" printf('\t\t\t %i',j-1)\n", +" end\n", +" printf('\n----------------------------------------------------------------------------------------------------------------------------------')\n", +" elseif i<=4\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',zi(i-1,j))\n", +" end\n", +" elseif i<=7\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',fz(i-4,j))\n", +" end\n", +" elseif i<=8\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',li(j))\n", +" end\n", +" elseif i<=9\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',di(j))\n", +" end\n", +" elseif i<=10\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',gi(j))\n", +" end\n", +" elseif i<=11\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',li(j+1))\n", +" end\n", +" elseif i<=12\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',hi(j))\n", +" end\n", +" elseif i<=13\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',hi(j+1))\n", +" end\n", +" elseif i<=14\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',z(j))\n", +" end\n", +" elseif i<=15\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',D1(j))\n", +" end\n", +" elseif i<=16\n", +" printf('\n %s',s(i))\n", +" for j=1:5\n", +" printf('\t\t%.10f',D2(j))\n", +" end\n", +" end\n", +"end\n", +"printf('\n\nAt the end of the %ith iteration by mullers method, the root of the equation is %.10f',j-1,z(j))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.33: Newton_Raphson_and_Mullers_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.33\n", +"//Newton Raphson and Mullers Method\n", +"//Page no. 202\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x^4-8*x^3+18*x^2+0.12*x-24.24')\n", +"deff('x=f1(x)','x=4*x^3-24*x^2+36*x+0.12')\n", +"\n", +"//newton raphson\n", +"x9=[1.5,2.5,2.7,3.1;4,5,14,10]\n", +"for h=1:4\n", +" x0=x9(1,h);e=0.00001\n", +"for i=1:x9(2,h)\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\nThe solution of this equation by newton raphshon after %i Iterations is %.5f\n',i,x1)\n", +"end\n", +"\n", +"//mullers method\n", +"zx=[1,2,2.7,3.1;2,3,3.7,4.1;3,4,4.7,5.1]\n", +"zi=[1;2;3];\n", +"s=['i','z0','z1','z2','f0','f1','f2','li','di','gi','li+1','hi','hi+1','zi+1','D+','D_']\n", +"li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", +"hi(1)=zi(3,1)-zi(2,1);\n", +"for i=2:4\n", +" for j=1:3\n", +" fz(j,i-1)=f(zi(j,i-1))\n", +" end\n", +" di(i-1)=1+li(i-1)\n", +" gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", +" D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", +" if abs(D1(i-1))>abs(D2(i-1)) then\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", +" else\n", +" li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", +" end\n", +" hi(i)=li(i)*hi(i-1);\n", +" z(i-1)=zi(3,i-1)+hi(i)\n", +" for j=1:2\n", +" zi(j,i)=zi(j+1,i-1)\n", +" end\n", +" zi(3,i)=z(i-1)\n", +"end\n", +"printf('\n\nAt the end of the %ith iteration by mullers method, the root of the equation is %.10f',j+2,z(j))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.34: QD_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.34\n", +"//QD Method\n", +"//Page no. 202\n", +"clc;clear;close;\n", +"a=[1,2,10,-20]\n", +"for i=1:5\n", +" e(i,1)=0;\n", +" e(i,4)=0;\n", +"end\n", +"q(1,1)=-a(2)/a(1);\n", +"q(1,2)=0;q(1,3)=0;\n", +"e(1,2)=a(3)/a(2);\n", +"e(1,3)=a(4)/a(3);\n", +"for i=2:7\n", +" for j=1:3\n", +" q(i,j)=e(i-1,j+1)+q(i-1,j)-e(i-1,j)\n", +" end\n", +" for j=1:2\n", +" e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j)\n", +" end\n", +"end\n", +"printf('e0\t\tq1\t\te1\t\tq2\t\te2\t\tq3\t\te3\n')\n", +"printf('------------------------------------------------------------------------------------------------------------\n')\n", +"for i=1:7\n", +" for j=1:3\n", +" printf('\t\t%.10f\t',q(i,j))\n", +" end\n", +" printf('\n')\n", +" for j=1:4\n", +" printf('%.10f\t\t\t',e(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\t\t%.10f\t\t\t%.10f\t\t\t%.10f\n',q(7,1),q(7,2),q(7,3))\n", +"printf('\nThe exact roots are \t%.10f and %.10f',q(7,1),q(7,3))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.35: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.35\n", +"//Newton Raphson Method\n", +"//Page no. 203\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x^3-30*x^2+2552')\n", +"deff('x=f1(x)','x=3*x^2-60*x')\n", +"//newton raphson\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=10;e=0.00001\n", +"for i=1:4\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThus the ball is submerged upto height of %.10f cm\n\n\n',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.36: Secant_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.36\n", +"//Secant Method\n", +"//Page no. 204\n", +"clc;clear;close;\n", +"a=8670;c=10^-8;t2=1.4*10^-4;\n", +"deff('x=f(x)','x=-t2+log((1-2*x/a)/(2-x/a))*(a*x*c)/(a+x)')\n", +"\n", +"printf('n\txn\t\tf(xn)\t\tXn+1\t\tf(Xn+1)\t\tXn+2\t\tError\n')\n", +"printf('----------------------------------------------------------------------------------------------------------\n')\n", +"x0=20000;x1=25000;e=0.00001\n", +"for i=1:8\n", +" x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0))\n", +" e1=abs(x0-x2)\n", +" printf(' %i\t%f\t%.10f\t%f\t%.10f\t%f\t%.10f\n',i-1,x0,f(x0),x1,f(x1),x2,e1)\n", +" x0=x1;\n", +" x1=x2\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, Rb = %.10f ohm',x2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.37: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.37\n", +"//Newton Raphson Method\n", +"//Page no. 204\n", +"clc;clear;close;\n", +"p=1.1;T=250;R=0.082;a=3.6;b=0.043;\n", +"deff('y=f(v)','y=p*v^3-(b*p+R*T)*v^2+a*v-a*b')\n", +"deff('y=f1(v)','y=3*p*v^2-2*(b*p+R*T)*v')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=0.1;e=0.00001\n", +"for i=1:10\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, Volume v = %.10f ltr',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.38: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.38\n", +"//Newton Raphson Method\n", +"//Page no. 205\n", +"clc;clear;close;\n", +"deff('y=f(p)','y=p^3-9*p^2+33*p-65')\n", +"deff('y=f1(p)','y=3*p^2-18*p+33')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=6;e=0.00001\n", +"for i=1:10\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, Market Price at equilibrium = Rs. %.f',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.39: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.39\n", +"//Newton Raphson Method\n", +"//Page no. 205\n", +"clc;clear;close;\n", +"deff('y=f(v)','y=v^3-20*v+30')\n", +"deff('y=f1(v)','y=3*v^2-20')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=10;e=0.00001\n", +"for i=1:10\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, sides are = %.5f m x %.5f m x %.5f m',x1,x1,20/x1^2)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3: Regula_Falsi_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.3\n", +"//Regula Falsi Method\n", +"//Page no. 149\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=x^3-3*x-5')\n", +"x1=2;x2=3;e=0.00001\n", +"printf('n\tx1\t\tf(x1)\t\tx2\t\tf(x2)\t\tx3\t\tf(x3)')\n", +"printf('\n-------------------------------------------------------------------------------------------------\n')\n", +"for i=0:19\n", +" x3=x2*f(x1)/(f(x1)-f(x2))+x1*f(x2)/(f(x2)-f(x1))\n", +" printf(' %i\t%f\t%f\t%f\t%f\t%f\t%f\n',i,x1,f(x1),x2,f(x2),x3,f(x3))\n", +" if f(x1)*f(x3)>0 then\n", +" x1=x3\n", +" else\n", +" x2=x3\n", +" end\n", +" if abs(f(x3))<e then\n", +" break\n", +" end\n", +"end\n", +"printf('\n\nTherefore the solution is %.10g',x3)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.40: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.40\n", +"//Newton Raphson Method\n", +"//Page no. 206\n", +"clc;clear;close;\n", +"deff('y=f(F)','y=-10*F^3-21*F+10')\n", +"deff('y=f1(F)','y=-21-30*F^2')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=1;e=0.00001\n", +"for i=1:10\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.6f\t%.5f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\t\t\t\t\t2\n Therefore, Magnetic Flux = %.5f Wb m',x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.4: Ridders_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.4\n", +"//Ridders Method\n", +"//Page no. 153\n", +"clc;clear;close;\n", +"deff('y=f(x)','y=x^3-3*x-5')\n", +"x1=2;x2=3;e=0.00001\n", +"printf('n\tx1\t\tf(x1)\t\tx2\t\tf(x2)\t\tx3\t\tf(x3)\t sign\t x4')\n", +"printf('\n----------------------------------------------------------------------------------------------------------\n')\n", +"for i=0:8\n", +" x3=(x1+x2)/2\n", +" a=f(x1)-f(x2);\n", +" s=a*abs(1/a)\n", +" x4=x3+(x3-x2)*(s*f(x3))/sqrt(f(x3)-f(x1)*f(x2))\n", +" printf(' %i\t%f\t%f\t%f\t%f\t%f\t%f %i\t%f\n',i,x1,f(x1),x2,f(x2),x3,f(x3),s,x4)\n", +" if f(x1)*f(x4)>0 then\n", +" x1=x4\n", +" else\n", +" x2=x4\n", +" end\n", +" if abs(f(x4))<e then\n", +" break\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation is %g after %i Iterations',x4,i)\n", +"printf('\n\n\nThere are computation error in the answers given by the book in this example\n\n(value of x1 is used instead of x2)')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.5: General_Iterative_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.5\n", +"//General Iterative Method\n", +"//Page no. 154\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=sqrt(3+5/x)')\n", +"printf('n\tx\t\tf(x)\n')\n", +"printf('------------------------------------\n')\n", +"x=2;\n", +"for i=1:8\n", +" printf(' %i\t%.10f\t%.10f\n',i,x,f(x))\n", +" x=f(x);\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,x)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.6: Linear_Iterative_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.6\n", +"//Linear Iterative Method\n", +"//Page no. 159\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=1+sin(x)/10')\n", +"printf('n\tx\t\tf(x)\n')\n", +"printf('-------------------------------------\n')\n", +"x=0;\n", +"for i=1:7\n", +" printf(' %i\t%.10f\t%.10f\n',i,x,f(x))\n", +" x=f(x);\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,x)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.7: Aitkens_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.7\n", +"//Aitkens Method\n", +"//Page no. 161\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=exp(-x)')\n", +"printf('n\tx0\t\tx1\t\tx2\t\tx3\t\ty\t\tdx0\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=0.5;e=0.0001\n", +"for i=1:3\n", +" x1=f(x0);x2=f(x1);x3=f(x2);\n", +" y=x3-((x3-x2)^2)/(x3-2*x2+x1)\n", +" dx0=y-x0;\n", +" \n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i,x0,x1,x2,x3,y,dx0)\n", +" x0=y;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,y)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.8: Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.8\n", +"//Newton Raphson Method\n", +"//Page no. 163\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=x-exp(-x)')\n", +"deff('x=f1(x)','x=1+exp(-x)')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=0.5;e=0.00001\n", +"for i=1:4\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nThe solution of this equation after %i Iterations is %.10f',i,x1)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.9: Modified_Newton_Raphson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 5.9\n", +"//Modified Newton Raphson Method\n", +"//Page no. 165\n", +"clc;clear;close;\n", +"deff('x=f(x)','x=exp(x)-3*x-sin(x)')\n", +"deff('x=f1(x)','x=exp(x)-3-cos(x)')\n", +"printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", +"printf('-----------------------------------------------------------------------------------------------------\n')\n", +"x0=0;e=0.00001\n", +"for i=1:4\n", +" x1=x0-f(x0)/f1(x0)\n", +" e1=abs(x0-x1)\n", +" printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", +" x0=x1;\n", +" if abs(x0)<e then\n", +" break;\n", +" end\n", +"end\n", +"printf('\n\nTherefore, the root is %.10f',x1)" + ] + } +], +"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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/6-Numerical_Methods_of_Linear_Equations_Direct_Methods.ipynb b/Numerical_Methods_Principles_by_Analysis/6-Numerical_Methods_of_Linear_Equations_Direct_Methods.ipynb new file mode 100644 index 0000000..31322e6 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/6-Numerical_Methods_of_Linear_Equations_Direct_Methods.ipynb @@ -0,0 +1,504 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 6: Numerical Methods of Linear Equations Direct Methods" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.10: Solving_Matrices.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.10\n", +"//Solving Matrices\n", +"//Page no. 244\n", +"clc;close;clear;\n", +"warning('off')\n", +"for i=1:7\n", +" s=0;\n", +" for j=1:7\n", +" A(i,j)=360360/(i+j)\n", +" end\n", +" B(i,1)=1;\n", +"end\n", +"X=inv(A)*B\n", +"disp(360360*X,'The Solution by 360360*X= ')\n", +"disp(X,'Final Solution = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.1: Gaussian_Elimination_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.1\n", +"//Gaussian Elimination Method\n", +"//Page no. 220\n", +"clc;clear;close;\n", +"\n", +"A=[5,10,1,28;1,1,1,6;4,8,3,29]; //augmented matrix\n", +"\n", +"//triangularization\n", +"for i=1:4\n", +" B(1,i)=A(1,i)\n", +" B(2,i)=A(2,i)-(A(2,1)/A(1,1))*A(1,i)\n", +" B(3,i)=A(3,i)-(A(3,1)/A(1,1))*A(1,i)\n", +"end\n", +"disp(A,'Augmented Matrix=')\n", +"disp(B,'Triangulated Matrix=')\n", +"//back substitution\n", +"x(3)=B(3,4)/B(3,3);\n", +"printf('\nx(3)=%f\n',x(3))\n", +"for i=2:-1:1\n", +" k=0\n", +" for j=i+1:3\n", +" k=k+B(i,j)*x(j)\n", +" end\n", +" x(i)=(1/B(i,i))*(B(i,4)-k)\n", +" printf('\nx(%i)=%f\n',i,x(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.2: Gaussian_Elimination_Method_for_TriDiagonal_System.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.2\n", +"//Gaussian Elimination Method for Tri-Diagonal System\n", +"//Page no. 222\n", +"clc;clear;close;\n", +"\n", +"//equation matrix\n", +"A=[1,2,0,0;2,3,-1,0;0,4,2,3;0,0,2,-1];\n", +"K=[5;5;11;10];i=1;\n", +"\n", +"//initialization\n", +"w(1)=A(1,2)/A(1,1);\n", +"g(1)=K(1)/A(1,1);\n", +"printf('\nw(%i)=%f',i,w(i));printf('\ng(%i)=%f',i,g(i))\n", +"\n", +"//computation\n", +"for i=2:3\n", +" w(i)=(A(i,i+1))/(A(i,i)-A(i,i-1)*w(i-1))\n", +" g(i)=(K(i)-A(i,i-1)*g(i-1))/(A(i,i)-A(i,i-1)*w(i-1))\n", +" printf('\nw(%i)=%f',i,w(i))\n", +" printf('\ng(%i)=%f',i,g(i))\n", +"end\n", +"i=4\n", +"m=-2\n", +"g(i)=m*(K(i)-A(i,i-1)*g(i-1))/(A(i,i)-A(i,i-1)*w(i-1))\n", +"x(i)=g(i)\n", +"printf('\ng(%i)=%f',i,g(i))\n", +"printf('\n\nx(%i)=%f',i,x(i))\n", +"\n", +"//solution\n", +"for i=3:-1:1\n", +" x(i)=g(i)-w(i)*x(i+1)\n", +" printf('\n\nx(%i)=%f',i,x(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.3: Gauss_Jordan_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.3\n", +"//Gauss-Jordan Method\n", +"//Page no. 224\n", +"\n", +"clc;clear;close;\n", +"\n", +"A=[5,10,1,28;4,8,3,29;1,1,1,6]; //augmented matrix\n", +"\n", +"for i=1:3\n", +" j=i\n", +" while (A(i,i)==0 & j<=3)\n", +" for k=1:4\n", +" B(1,k)=A(j+1,k)\n", +" A(j+1,k)=A(i,k)\n", +" A(i,k)=B(1,k)\n", +" end\n", +" disp(A)\n", +" j=j+1\n", +" end\n", +" disp(A)\n", +" for k=4:-1:i\n", +" A(i,k)=A(i,k)/A(i,i)\n", +" end\n", +" disp(A)\n", +" for k=1:3\n", +" if(k~=i) then\n", +" l=A(k,i)/A(i,i)\n", +" for m=i:4\n", +" A(k,m)=A(k,m)-l*A(i,m)\n", +" end\n", +" end\n", +" \n", +" end\n", +" disp(A)\n", +"end\n", +"\n", +"for i=1:3\n", +" printf('\nx(%i) = %g\n',i,A(i,4))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.4: Gaussian_Elimination_Method_without_Pivoting.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.4\n", +"//Gaussian Elimination Method without Pivoting\n", +"//Page no. 227\n", +"clc;clear;close;\n", +"\n", +"A=[0.3*10^-11,1,0.7;1,1,0.9]; //augmented matrix\n", +"\n", +"//triangularization\n", +"for i=1:3\n", +" B(1,i)=A(1,i)\n", +" B(2,i)=A(2,i)-(A(2,1)/A(1,1))*A(1,i)\n", +"end\n", +"disp(A,'Augmented Matrix=')\n", +"disp(B,'Triangulated Matrix=')\n", +"\n", +"//back substitution\n", +"x(2)=B(2,3)/B(2,2);\n", +"printf('\nx(2)=%f\n',x(2))\n", +"for i=1:-1:1\n", +" k=0\n", +" for j=i+1:2\n", +" k=k+B(i,j)*x(j)\n", +" end\n", +" x(i)=(1/B(i,i))*(B(i,3)-k)\n", +" printf('\nx(%i)=%f\n',i,x(i))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.5: Dolittle_Factorization_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.5\n", +"//Dolittle Factorization Method\n", +"//Page no. 233\n", +"clc;clear;close;\n", +"\n", +"A=[2,1,1;1,3,1;1,1,4];\n", +"printf('\tL\t\t *\t\tU\t\t =\t\tA')\n", +"U(2,1)=0;U(3,1)=0;U(3,2)=0;\n", +"L(1,2)=0;L(1,3)=0;L(2,3)=0;\n", +"for i=1:3\n", +" L(i,i)=1\n", +"end\n", +"for i=1:3\n", +" U(1,i)=A(1,i)\n", +"end\n", +"L(2,1)=1/U(1,1);\n", +"for i=2:3\n", +" U(2,i)=A(2,i)-U(1,i)*L(2,1);\n", +"end\n", +"L(3,1)=1/U(1,1);\n", +"L(3,2)=(A(3,2)-U(1,2)*L(3,1))/U(2,2);\n", +"U(3,3)=A(3,3)-U(1,3)*L(3,1)-U(2,3)*L(3,2);\n", +"printf('\n')\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%.2f\t',L(i,j))\n", +" end\n", +" \n", +" if(i==2)\n", +" printf(' * ')\n", +" else\n", +" printf('\t')\n", +" end\n", +" \n", +" for j=1:3\n", +" printf('%.2f\t',U(i,j))\n", +" end\n", +" if(i==2)\n", +" printf(' = ')\n", +" else\n", +" printf('\t')\n", +" end\n", +" for j=1:3\n", +" printf('%.2f\t',A(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.6: Trangularization_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.6\n", +"//Trangularization Method\n", +"//Page no. 236\n", +"clc;clear;close;\n", +"\n", +"A=[2,1,1;1,3,1;1,1,4];\n", +"B=[7;10;15];\n", +"printf('A can be factorizaed as follows:\n')\n", +"printf('\tL\t\t *\t\tU\t\t =\t\tA')\n", +"U(2,1)=0;U(3,1)=0;U(3,2)=0;\n", +"L(1,2)=0;L(1,3)=0;L(2,3)=0;\n", +"for i=1:3\n", +" L(i,i)=1\n", +"end\n", +"for i=1:3\n", +" U(1,i)=A(1,i)\n", +"end\n", +"L(2,1)=1/U(1,1);\n", +"for i=2:3\n", +" U(2,i)=A(2,i)-U(1,i)*L(2,1);\n", +"end\n", +"L(3,1)=1/U(1,1);\n", +"L(3,2)=(A(3,2)-U(1,2)*L(3,1))/U(2,2);\n", +"U(3,3)=A(3,3)-U(1,3)*L(3,1)-U(2,3)*L(3,2);\n", +"printf('\n')\n", +"for i=1:3\n", +" for j=1:3\n", +" printf('%.2f\t',L(i,j))\n", +" end\n", +" \n", +" if(i==2)\n", +" printf(' * ')\n", +" else\n", +" printf('\t')\n", +" end\n", +" \n", +" for j=1:3\n", +" printf('%.2f\t',U(i,j))\n", +" end\n", +" if(i==2)\n", +" printf(' = ')\n", +" else\n", +" printf('\t')\n", +" end\n", +" for j=1:3\n", +" printf('%.2f\t',A(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"printf('\nY=U*X')\n", +" Y=inv(L)*B\n", +" X=inv(U)*Y\n", +"printf('\n\nX=')\n", +"for i=1:3\n", +" printf('\n %i',X(i,1))\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.7: Wilkinson_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.7\n", +"//Wilkinson Method\n", +"//Page no. 240\n", +"clc;clear;close;\n", +"\n", +"A=[0.3*10^5,0.212,0.332;0.216,0.376,0.477;0.173,0.663,0.626];\n", +"B=[0.235;0.128;0.285];\n", +"X=inv(A)\n", +"disp(X*B,'Final Solution = ')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.8: Choleskys_Factorizatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.8\n", +"//Cholesky's Factorization\n", +"//Page no. 243\n", +"clc;clear;close;\n", +"\n", +"A=[1,2,3;2,5,8;3,8,22];\n", +"U(2,1)=0;U(3,1)=0;U(3,2)=0;\n", +"for i=1:3\n", +" for j=1:3\n", +" if(i==j)\n", +" k=0;\n", +" for m=1:i-1\n", +" k=k+U(m,i)^2; \n", +" end\n", +" U(i,j)=sqrt(A(i,j)-k)\n", +" end\n", +" if(j>i)\n", +" k=0;\n", +" for m=1:i-1\n", +" k=k+U(m,j)*U(m,i);\n", +" end\n", +" U(i,j)=(A(i,j)-k)/U(i,i)\n", +" end\n", +" end\n", +"end\n", +"disp(U,'Required Matrix (U)=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6.9: Complex_System_of_Linear_Equation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 6.9\n", +"//Complex System of Linear Equation\n", +"//Page no. 244\n", +"clc;clear;close;\n", +"\n", +"for i=1:7\n", +" s=0;\n", +" for j=1:7\n", +" A(i,j)=j^i\n", +" s=s+(-1)^(j+1)*A(i,j)\n", +" end\n", +" B(i,1)=s;\n", +"end\n", +"X=inv(A)*B\n", +"disp(X,'The Solution = ')" + ] + } +], +"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 +} 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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/8-Numerical_Solutions_of_Linear_Systems_of_Equations_Iterative_Methods.ipynb b/Numerical_Methods_Principles_by_Analysis/8-Numerical_Solutions_of_Linear_Systems_of_Equations_Iterative_Methods.ipynb new file mode 100644 index 0000000..1881c60 --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/8-Numerical_Solutions_of_Linear_Systems_of_Equations_Iterative_Methods.ipynb @@ -0,0 +1,666 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 8: Numerical Solutions of Linear Systems of Equations Iterative Methods" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.1: Jacobi_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.1\n", +"//Jacobi Method\n", +"//Page no. 273\n", +"clc;clear;close;\n", +"\n", +"A=[8,-3,2;4,11,-1;6,3,12]; //equation matrix\n", +"B=[20;33;36] //solution matrix\n", +"for i=0:19\n", +" X(i+1,1)=i;\n", +"end\n", +"for i=2:4\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:19\n", +" for i=1:3\n", +" k=0;\n", +" for j=1:3\n", +" if(i~=j)\n", +" k=k-A(i,j)*X(r,j+1);\n", +" end\n", +" end\n", +" X(r+1,i+1)=(k+B(i,1))/A(i,i);\n", +" end\n", +"end\n", +"printf(' r\t x(r)\t\ty(r)\t z(r)');\n", +"printf('\n ------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 18 iterations exact solution is:\nx=%i\ty=%i\tz=%i',X(19,2),X(19,3),X(19,4))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.2: Gauss_Seidel_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.2\n", +"//Gauss-Seidel Method\n", +"//Page no. 274\n", +"clc;clear;close;\n", +"\n", +"A=[8,-3,2;4,11,-1;6,3,12]; //equation matrix\n", +"B=[20;33;36] //solution matrix\n", +"for i=0:10\n", +" X(i+1,1)=i;\n", +"end\n", +"for i=2:4\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:10\n", +" for i=1:3\n", +" k1=0;\n", +" for j=1:i-1\n", +" \n", +" k1=k1-A(i,j)*X(r+1,j+1);\n", +" \n", +" end\n", +" k2=0;\n", +" for j=i+1:3\n", +" \n", +" k2=k2-A(i,j)*X(r,j+1);\n", +" \n", +" end\n", +" X(r+1,i+1)=(k1+k2+B(i,1))/A(i,i);\n", +" end\n", +"end\n", +"printf(' r\t x(r)\t\ty(r)\t z(r)');\n", +"printf('\n ------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 9 iterations exact solution is:\nx=%i\ty=%i\tz=%i',X(10,2),X(10,3),X(10,4))" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.3: SOR_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.3\n", +"//SOR Method\n", +"//Page no. 275\n", +"clc;clear;close;\n", +"\n", +"A=[5,2,1;-1,4,2;2,-3,10];\n", +"B=[-12;20;3];\n", +"w=0.9;\n", +"for i=0:13\n", +" X(i+1,1)=i;\n", +"end\n", +"X(1,2)=-2.4;\n", +"X(1,3)=5;\n", +"X(1,4)=0.3;\n", +"for r=1:13\n", +" for i=1:3\n", +" k1=0;\n", +" for j=1:i-1\n", +" \n", +" k1=k1-A(i,j)*X(r+1,j+1);\n", +" \n", +" end\n", +" k2=0;\n", +" for j=i+1:3\n", +" \n", +" k2=k2-A(i,j)*X(r,j+1);\n", +" \n", +" end\n", +" X(r+1,i+1)=(1-w)*X(r,i+1)+(w*k1+w*k2+w*B(i,1))/A(i,i);\n", +" end\n", +"end\n", +"printf(' r\t x(r)\t\ty(r)\t z(r)');\n", +"printf('\n ------------------------------------');\n", +"disp(X);\n", +"printf('\n\nAfter 12 iterations exact solution is:\nx=%i\ty=%i\tz=%i',X(13,2),X(13,3),X(13,4));" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.4: Gauss_Seidel_Point_Iterative_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.4\n", +"//Gauss-Seidel Point Iterative Method\n", +"//Page no. 278\n", +"clc;clear;close;\n", +"\n", +"\n", +"A=[10,1,0,0,0,-1;1,10,1,0,0,0;2,0,20,1,0,0;0,0,1,10,-1,0;0,3,0,0,30,3;0,0,0,2,-2,20]; //equation matrix\n", +"B=[5;10;10;0;0;5] //solution matrix\n", +"for i=1:6\n", +" for j=1:6\n", +" if(A(j,j)==0)\n", +" for k=1:6\n", +" C(j,k)=A(j,k);\n", +" A(j,k)=A(j+1,k);\n", +" A(j+1,k)=C(j,k);\n", +" end\n", +" end\n", +" end\n", +"end\n", +"for i=0:7\n", +" X(i+1,1)=i;\n", +"end\n", +"for i=2:7\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:7\n", +" for i=1:6\n", +" k1=0;\n", +" for j=1:i-1\n", +" \n", +" k1=k1-A(i,j)*X(r+1,j+1);\n", +" \n", +" end\n", +" k2=0;\n", +" for j=i+1:6\n", +" \n", +" k2=k2-A(i,j)*X(r,j+1);\n", +" \n", +" end\n", +" X(r+1,i+1)=(k1+k2+B(i,1))/A(i,i);\n", +" end\n", +"end\n", +"printf(' r ');\n", +"for i=1:6\n", +" printf('x%i ',i);\n", +"end\n", +"printf('\n --------------------------------------------------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 6 iterations exact solution is:\n');\n", +"for i=1:6\n", +" printf('x%i=%f ',i,X(7,i+1));\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.5: Gauss_Seidel_Point_Iterative_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.5\n", +"//Gauss-Seidel Point Iterative Method\n", +"//Page no. 279\n", +"clc;clear;close;\n", +"\n", +"A=[2,3,-4,1;1,-2,-5,1;5,-3,1,-4;10,2,-1,2]; //equation matrix\n", +"B=[3;2;1;-4]; //solution matrix\n", +"\n", +"//transformation of the equations\n", +"for i=1:4\n", +" A1(1,i)=A(4,i);\n", +" B1(1,1)=B(4,1);\n", +"end\n", +"for i=1:4\n", +" A1(3,i)=A(2,i);\n", +" B1(3,1)=B(2,1);\n", +"end\n", +"for i=1:4\n", +" A1(2,i)=A(1,i)-A(2,i);\n", +" B1(2,1)=B(1,1)-B(2,1);\n", +"end\n", +"for i=1:4\n", +" A1(4,i)=2*A(1,i)-A(2,i)+2*A(3,i)-A(4,i);\n", +" B1(4,1)=2*B(1,1)-B(2,1)+2*B(3,1)-B(4,1);\n", +"end\n", +"\n", +"//printing of transformed equations\n", +"printf('\nTransformed Equations are=\n\n')\n", +"for i=1:4\n", +" for j=1:4\n", +" printf('(%ix(%i))',A1(i,j),j);\n", +" if(j<4)\n", +" printf(' + ')\n", +" end\n", +" end\n", +" printf('= %i\n',B1(i,1));\n", +"end\n", +"\n", +"for i=1:4\n", +" for j=1:4\n", +" if(A(j,j)==0)\n", +" for k=1:4\n", +" C(j,k)=A(j,k);\n", +" A(j,k)=A(j+1,k);\n", +" A(j+1,k)=C(j,k);\n", +" end\n", +" end\n", +" end\n", +"end\n", +"for i=0:12\n", +" X(i+1,1)=i;\n", +"end\n", +"for i=2:5\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:12\n", +" for i=1:4\n", +" k1=0;\n", +" for j=1:i-1\n", +" \n", +" k1=k1-A1(i,j)*X(r+1,j+1);\n", +" \n", +" end\n", +" k2=0;\n", +" for j=i+1:4\n", +" \n", +" k2=k2-A1(i,j)*X(r,j+1);\n", +" \n", +" end\n", +" X(r+1,i+1)=(k1+k2+B1(i,1))/A1(i,i);\n", +" end\n", +"end\n", +"printf('\n\n r ');\n", +"for i=1:4\n", +" printf('x%i ',i);\n", +"end\n", +"printf('\n ------------------------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 11 iterations exact solution is:\n');\n", +"for i=1:4\n", +" printf('x%i=%f ',i,X(12,i+1));\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.6: Block_Jacobi_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.6\n", +"//Block Jacobi Method\n", +"//Page no. 281\n", +"clc;clear;close;\n", +"\n", +"A=[10,1,0,0,0,-1;1,10,1,0,0,0;2,0,20,1,0,0;0,0,1,10,-1,0;0,3,0,0,30,3;0,0,0,2,-2,20]; //equation matrix\n", +"B=[5;10;10;0;0;5] //solution matrix\n", +"disp(B,'B=',A,'A=')\n", +"for i=1:3\n", +" for j=1:3\n", +" A11(i,j)=A(i,j);\n", +" end\n", +" B1(i,1)=B(i,1);\n", +"end\n", +"for i=1:3\n", +" for j=1:3\n", +" A12(i,j)=A(i,j+3);\n", +" end\n", +"end\n", +"for i=1:3\n", +" for j=1:3\n", +" A21(i,j)=A(i+3,j);\n", +" end\n", +"end\n", +"for i=1:3\n", +" for j=1:3\n", +" A22(i,j)=A(i+3,j+3);\n", +" end\n", +" B2(i,1)=B(i+3,1);\n", +"end\n", +"disp(B2,'B2=',B1,'B1=',A22,'A22=',A21,'A21=',A12,'A12=',A11,'A11=');\n", +"A11_1=inv(A11);A22_1=inv(A22);\n", +"disp(A22_1,'Inverse of A22=',A11_1,'Inverse of A11=')\n", +"for i=1:3\n", +" X1(i,1)=0;\n", +" X2(i,1)=0;\n", +"end\n", +"for r=1:2\n", +" X11=A11_1*(-1*A12*X2+B1);\n", +" X22=A22_1*(-1*A21*X1+B2);\n", +" X1=X11;\n", +" X2=X22;\n", +" disp(X1,'X1=')\n", +" disp(X2,'X2=')\n", +"end\n", +"for i=1:6\n", +" if(i<4)\n", +" X(i,1)=X1(i,1);\n", +" else\n", +" X(i,1)=X2(i-3,1);\n", +" end\n", +"end \n", +"disp(X,'X=')\n", +"printf('\n\n\nNote : There is a computation error in calculation of X1(2)')\n", +" \n", +" \n", +" " + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.7: Block_Gauss_Seidel_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.7\n", +"//Block Gauss-Seidel Method\n", +"//Page no. 283\n", +"clc;clear;close;\n", +"\n", +"A=[10,1,0,0,0,-1;1,10,1,0,0,0;2,0,20,1,0,0;0,0,1,10,-1,0;0,3,0,0,30,3;0,0,0,2,-2,20]; //equation matrix\n", +"B=[5;10;10;0;0;5] //solution matrix\n", +"disp(B,'B=',A,'A=')\n", +"\n", +"for i=1:2\n", +" for j=1:2\n", +" A11(i,j)=A(i,j);\n", +" end\n", +" B1(i,1)=B(i,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A12(i,j)=A(i,j+2);\n", +" end\n", +" B2(i,1)=B(i+2,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A13(i,j)=A(i,j+4);\n", +" end\n", +" B3(i,1)=B(i+4,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A21(i,j)=A(i+2,j);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A22(i,j)=A(i+2,j+2);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A23(i,j)=A(i+2,j+4);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A31(i,j)=A(i+4,j);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A32(i,j)=A(i+4,j+2);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A33(i,j)=A(i+4,j+4);\n", +" end\n", +"end\n", +"disp(B3,'B3=',B2,'B2=',B1,'B1=',A33,'A33=',A32,'A32=',A31,'A31=',A23,'A23=',A22,'A22=',A21,'A21=',A13,'A13=',A12,'A12=',A11,'A11=');\n", +"A11_1=inv(A11);A22_1=inv(A22);A33_1=inv(A33);\n", +"disp(A33_1,'Inverse of Matrix A33=',A22_1,'Inverse of Matrix A22=',A11_1,'Inverse of Matrix A11=');\n", +"for i=1:2\n", +" X1(i,1)=0;\n", +" X2(i,1)=0;\n", +" X3(i,1)=0;\n", +"end\n", +"for i=1:6\n", +" X(i,1)=i-1;\n", +"end\n", +"for i=2:7\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:5\n", +" X11=A11_1*(-1*A12*X2+(-1)*A13*X3+B1);\n", +" X22=A22_1*(-1*A21*X11+(-1)*A23*X3+B2);\n", +" X33=A33_1*(-1*A31*X11+(-1)*A32*X22+B3);\n", +" X1=X11;\n", +" X2=X22;\n", +" X3=X33;\n", +" disp(X3,'X3=',X2,'X2=',X1,'X1=')\n", +" for i=2:7\n", +" if(i<4)\n", +" X(r+1,i)=X1(i-1,1);\n", +" end\n", +" if(i<6 & i>3)\n", +" X(r+1,i)=X2(i-3,1);\n", +" end\n", +" if(i<8 & i>5)\n", +" X(r+1,i)=X3(i-5,1);\n", +" end\n", +" end\n", +"end\n", +"printf('\n\nIteration');\n", +"for i=1:6\n", +" printf(' x%i ',i);\n", +"end\n", +"printf('\n --------------------------------------------------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 4 iterations exact solution is:\n');\n", +"for i=1:6\n", +" printf('x%i=%f ',i,X(5,i+1));\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8.8: Block_SOR_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 8.8\n", +"//Block SOR Method\n", +"//Page no. 284\n", +"clc;clear;close;\n", +"\n", +"\n", +"A=[10,1,0,0,0,-1;1,10,1,0,0,0;2,0,20,1,0,0;0,0,1,10,-1,0;0,3,0,0,30,3;0,0,0,2,-2,20]; //equation matrix\n", +"B=[5;10;10;0;0;5] //solution matrix\n", +"disp(B,'B=',A,'A=')\n", +"w=0.8\n", +"for i=1:2\n", +" for j=1:2\n", +" A11(i,j)=A(i,j);\n", +" end\n", +" B1(i,1)=B(i,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A12(i,j)=A(i,j+2);\n", +" end\n", +" B2(i,1)=B(i+2,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A13(i,j)=A(i,j+4);\n", +" end\n", +" B3(i,1)=B(i+4,1);\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A21(i,j)=A(i+2,j);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A22(i,j)=A(i+2,j+2);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A23(i,j)=A(i+2,j+4);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A31(i,j)=A(i+4,j);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A32(i,j)=A(i+4,j+2);\n", +" end\n", +"end\n", +"for i=1:2\n", +" for j=1:2\n", +" A33(i,j)=A(i+4,j+4);\n", +" end\n", +"end\n", +"disp(B3,'B3=',B2,'B2=',B1,'B1=',A33,'A33=',A32,'A32=',A31,'A31=',A23,'A23=',A22,'A22=',A21,'A21=',A13,'A13=',A12,'A12=',A11,'A11=');\n", +"A11_1=inv(A11);A22_1=inv(A22);A33_1=inv(A33);\n", +"disp(A33_1,'Inverse of Matrix A33=',A22_1,'Inverse of Matrix A22=',A11_1,'Inverse of Matrix A11=');\n", +"for i=1:2\n", +" X1(i,1)=0;\n", +" X2(i,1)=0;\n", +" X3(i,1)=0;\n", +"end\n", +"for i=1:7\n", +" X(i,1)=i-1;\n", +"end\n", +"for i=2:7\n", +" X(1,i)=0;\n", +"end\n", +"for r=1:6\n", +" X11=A11_1*((1-w)*X1+(-1)*w*A12*X2+(-1)*w*A13*X3+w*B1);\n", +" X22=A22_1*((1-w)*X2+(-1)*w*A21*X11+(-1)*w*A23*X3+w*B2);\n", +" X33=A33_1*((1-w)*X3+(-1)*w*A31*X11+(-1)*w*A32*X22+w*B3);\n", +" X1=X11;\n", +" X2=X22;\n", +" X3=X33;\n", +" disp(X3,'X3=',X2,'X2=',X1,'X1=')\n", +" for i=2:7\n", +" if(i<4)\n", +" X(r+1,i)=X1(i-1,1);\n", +" end\n", +" if(i<6 & i>3)\n", +" X(r+1,i)=X2(i-3,1);\n", +" end\n", +" if(i<8 & i>5)\n", +" X(r+1,i)=X3(i-5,1);\n", +" end\n", +" end\n", +"end\n", +"printf('\n\nIteration');\n", +"for i=1:6\n", +" printf(' x%i ',i);\n", +"end\n", +"printf('\n --------------------------------------------------------------------------------')\n", +"disp(X)\n", +"printf('\n\nAfter 5 iterations exact solution is:\n');\n", +"for i=1:6\n", +" printf('x%i=%f ',i,X(6,i+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 +} diff --git a/Numerical_Methods_Principles_by_Analysis/9-Linear_Least_Squares_Problem.ipynb b/Numerical_Methods_Principles_by_Analysis/9-Linear_Least_Squares_Problem.ipynb new file mode 100644 index 0000000..0e7132d --- /dev/null +++ b/Numerical_Methods_Principles_by_Analysis/9-Linear_Least_Squares_Problem.ipynb @@ -0,0 +1,479 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 9: Linear Least Squares Problem" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.1: Moore_Penrose_Generalized_Inverse.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.1\n", +"//Moore-Penrose Generalized Inverse\n", +"//Page no. 292\n", +"clc;clear;close;\n", +"\n", +"AT=[3,0,3;0,3,3];\n", +"A=AT'; //transpose\n", +"I=inv(AT*A); //inverse\n", +"disp(I,'Inverse of AT*A=',AT*A,'AT*A=',A,'A=',AT,'AT=');\n", +"A#=I*AT;\n", +"disp(A#,'Moore-Penrose Generalized Inverse of A=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.2: Curve_Fitting.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.2\n", +"//Curve Fitting\n", +"//Page no. 293\n", +"clc;clear;close;\n", +"x(1)=0.25;\n", +"for i=2:6\n", +" x(1,i)=x(1,i-1)+0.25;\n", +"end //x values\n", +"y(1,1)=3.1;y(1,2)=1.7;y(1,3)=1;y(1,4)=0.68;y(1,5)=0.42;y(1,6)=0.26; //y values\n", +"\n", +"//construction of normal equations\n", +"for i=1:6\n", +" Y(1,i)=log10(y(1,i));\n", +"end\n", +"Ex=0;\n", +"for i=1:6\n", +" Ex=Ex+x(1,i);\n", +"end\n", +"EY=0;\n", +"for i=1:6\n", +" EY=EY+Y(1,i);\n", +"end\n", +"Ex2=0;\n", +"for i=1:6\n", +" Ex2=Ex2+x(1,i)^2;\n", +"end\n", +"ExY=0;\n", +"for i=1:6\n", +" ExY=ExY+x(1,i)*Y(1,i);\n", +"end\n", +"printf('E x(k)\t\t y(k)\t\tE Y(k)\t\tE x2(k)\t\tE x(k)*Y(k)')\n", +"printf('\n----------------------------------------------------------------------------')\n", +"for i=1:6\n", +" printf('\n%f\t%f\t%f\t%f\t%f',x(1,i),y(1,i),Y(1,i),x(1,i)^2,x(1,i)*Y(1,i))\n", +"end\n", +"printf('\n----------------------------------------------------------------------------')\n", +"printf('\n%f\t%f\t%f\t%f\t%f',Ex,0,EY,Ex2,ExY)\n", +"printf('\n----------------------------------------------------------------------------\n\n')\n", +"A=[6,Ex;Ex,Ex2]; //system of normal equations\n", +"B=[EY;ExY];\n", +"X=inv(A)*B;\n", +"a=exp(X(1,1));\n", +"b=-1*X(2,1);\n", +"for i=1:2\n", +" for j=1:2\n", +" printf('%f ',A(i,j))\n", +" end\n", +" if(i==1)\n", +" printf(' *')\n", +" end\n", +" \n", +" printf('\ta%i',i);\n", +" if(i==1)\n", +" printf(' =')\n", +" end\n", +" \n", +" printf('\t%f\n',B(i,1))\n", +"end\n", +"printf('\n\na1=%f\na2=%f\n\na=%f\nb=%f\n\n',X(1,1),X(2,1),a,b)\n", +"printf('The fitted curve is:\n %fx\ny=%f e',b,a)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.3: Gram_Schmidt_Orthogonalization_or_Orthonormalization_Process.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.3\n", +"//Gram-Schmidt Orthogonalization/Orthonormalization Process\n", +"//Page no. 294\n", +"clc;clear;close;\n", +"deff('y=f(x,a)','y=sqrt(x(1,a)^2+x(2,a)^2+x(3,a)^2+x(4,a)^2)');\n", +"deff('y=f1(g,a,h,b)','y=g(1,a)*h(1,b)+g(2,a)*h(2,b)+g(3,a)*h(3,b)+g(4,a)*h(4,b)');\n", +"\n", +"U=[1/sqrt(3),-2/sqrt(7),1,0,0,0;0,1/sqrt(7),0,1,0,0;1/sqrt(3),1/sqrt(7),0,0,1,0;-1/sqrt(3),-1/sqrt(7),0,0,0,1];\n", +"for i=1:4\n", +" V(i,1)=U(i,1);\n", +"end\n", +"for i=1:4\n", +" if(f(V,1)~=0)\n", +" W(i,1)=V(i,1)/f(V,1);\n", +" else\n", +" W(i,1)=0;\n", +" end \n", +"end\n", +"for j=2:6\n", +" for i=1:4\n", +" for l=1:4\n", +" k(l,1)=0;\n", +" end\n", +" for l=1:j-1\n", +" for m=1:4\n", +" w(m,1)=W(m,l);\n", +" end\n", +" k=k-(f1(U,j,W,l))*w;\n", +" end\n", +" V(i,j)=U(i,j)+k(i,1);\n", +" end\n", +" for i=1:4\n", +" if(j~=4)\n", +" if(f(V,j)~=0)\n", +" W(i,j)=V(i,j)/f(V,j);\n", +" else\n", +" W(i,j)=0;\n", +" end \n", +" else\n", +" W(i,j)=0;\n", +" end\n", +" end\n", +" \n", +"end\n", +"disp(U,'U=')\n", +"disp('W=')\n", +"printf('\n')\n", +"for i=1:4\n", +" for j=1:6\n", +" printf('%.4f\t\t',W(i,j))\n", +" end\n", +" printf('\n')\n", +"end\n", +"disp('V=')\n", +"printf('\n')\n", +"for i=1:4\n", +" for j=1:6\n", +" printf('%.4f\t\t',V(i,j))\n", +" end\n", +" printf('\n')\n", +"end" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.4: QR_Decompositio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.4\n", +"//QR Decomposition\n", +"//Page no. 296\n", +"clc;clear;close;\n", +"\n", +"A=[2,1,1;1,3,1;1,1,4];\n", +"B=A*A';\n", +"disp(B,'AT*A=')\n", +"//cholesky factorization to find R\n", +"R(2,1)=0;R(3,1)=0;R(3,2)=0; \n", +"for i=1:3\n", +" for j=1:3\n", +" if(i==j)\n", +" k=0;\n", +" for m=1:i-1\n", +" k=k+R(m,i)^2; \n", +" end\n", +" R(i,j)=sqrt(B(i,j)-k)\n", +" end\n", +" if(j>i)\n", +" k=0;\n", +" for m=1:i-1\n", +" k=k+R(m,j)*R(m,i);\n", +" end\n", +" R(i,j)=(B(i,j)-k)/R(i,i)\n", +" end\n", +" end\n", +"end\n", +"//cholesky factorization end\n", +"disp(R,'Upper Triangular Matrix (R)=')\n", +"R_1=inv(R);\n", +"disp(R_1,'Inverse of R')\n", +"Q=A*R_1;\n", +"disp(Q,'Orthogonal Matrix Q=')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.5: Vector_Computatio.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.5\n", +"//Vector Computation\n", +"//Page no. 299\n", +"clc;clear;close;\n", +"\n", +"X=[2,3,0,1];\n", +"n=X(1);\n", +"for i=2:4\n", +" if(n<X(i))\n", +" n=X(i);\n", +" end\n", +"end\n", +"printf('\nMaximum Value (n)=%i\n',n)\n", +"for i=1:4\n", +" X(i)=X(i)/n;\n", +"end\n", +"disp(X,'Normalized X=')\n", +"k=0;\n", +"for i=1:4\n", +" k=k+X(i)^2;\n", +"end\n", +"sigma=X(1)*abs(1/X(1))*sqrt(k);\n", +"printf('\nsigma=%f\n',sigma);\n", +"X(1)=X(1)+sigma;\n", +"printf('\nModified x1 = %g\n',X(1))\n", +"for i=1:4\n", +" U(1,i)=X(i);\n", +"end\n", +"disp(U,'U=')\n", +"p=sigma*X(1);sigma=n*sigma;\n", +"printf('\n p = %f\n\n sigma = %f',p,sigma);\n", +"printf('\n\n\nNote : There is a computation error in calculation of U1')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.6: House_Holder_Transformation.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.6\n", +"//House Holder Transformation\n", +"//Page no. 300\n", +"clc;clear;close;\n", +"\n", +"A=[4,2,1;2,5,-2;1,-2,7]\n", +"disp(A,'A=')\n", +"k=0;\n", +"for j=2:3\n", +" k=k+A(j,1)^2;\n", +"end\n", +"a=A(2,1)*abs(1/A(2,1))*sqrt(k);\n", +"disp(a,'alpha=')\n", +"U=[0;a+A(2,1);A(3,1)];\n", +"disp(U,'U=')\n", +"U1=U'*U;\n", +"disp(U1,'UT*U=')\n", +"U2=U*U';\n", +"disp(U2,'U*UT=')\n", +"P=eye(3,3)-(2*U2)/U1;\n", +"disp(P,'P=');\n", +"B=P*A*P;\n", +"disp(B,'B=');\n", +"printf('\n\n\nThere are computation error in the answers given by the book in this example\n\n(a22 value error in U*UT)')" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.7: Givens_QR_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.7\n", +"//Givens QR Method\n", +"//Page no. 303\n", +"clc;clear;close;\n", +"\n", +"A=[4,2,1;2,5,-2;1,-2,7]\n", +"deff('y=c(i,j)','y=A(j,j)/sqrt((A(i,j)^2+A(j,j)^2))')\n", +"deff('y=s(i,j)','y=A(i,j)/sqrt((A(i,j)^2+A(j,j)^2))')\n", +"disp(A,'A=')\n", +"R=A;Q=eye(3,3);\n", +"m=1;\n", +"for j=1:2\n", +" for i=j+1:3\n", +" for k=1:3 //C matrix evaluation\n", +" for l=1:3\n", +" if(k==l)\n", +" if(k==i | k==j)\n", +" C(k,l)=c(i,j)\n", +" else\n", +" C(k,l)=1\n", +" end\n", +" end\n", +" if(k>l)\n", +" if(k==i & l==j)\n", +" C(k,l)=-1*s(i,j)\n", +" else\n", +" C(k,l)=0\n", +" end\n", +" end\n", +" if(k<l)\n", +" if(k==j & l==i)\n", +" C(k,l)=s(i,j)\n", +" else\n", +" C(k,l)=0\n", +" end\n", +" end\n", +" end\n", +" end\n", +" printf('\n\n Iteration %i',m)\n", +" m=m+1\n", +" disp(C,'C=');\n", +" R=C*R;\n", +" Q=Q*C';\n", +" disp(Q,'Q=',R,'R=')\n", +" end\n", +"end\n", +"disp(Q*R,'Q*R=A=') //verification" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9.8: Recursive_Least_Square_Method.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Example 9.8\n", +"//Recursive Least-Square Method\n", +"//Page no. 308\n", +"clc;clear;close;\n", +"\n", +"A0=[3,0;0,3;3,3];\n", +"B0=[2;2;2];\n", +"A1=[6,3];B1=[6];\n", +"A0T=A0';\n", +"G0=A0T*A0;\n", +"disp(G0,'G0=')\n", +"G0_1=inv(G0);\n", +"disp(G0_1,'Inverse of G0=')\n", +"X0=G0_1*A0T*B0;\n", +"disp(X0,'X0=')\n", +"\n", +"//by recursive least square algorithm\n", +"G1=G0+A1'*A1;\n", +"disp(G1,'G1=');\n", +"G1_1=inv(G1);\n", +"disp(G1_1,'Inverse of G1')\n", +"X1=X0+G1_1*A1'*(B1-A1*X0);\n", +"disp(X1,'X1=')\n", +"\n", +"//verification\n", +"A=[3,0;0,3;3,3;6,3];\n", +"B=[2;2;2;6];\n", +"AT=A';\n", +"G=AT*A;\n", +"disp(G,'G=')\n", +"G_1=inv(G);\n", +"disp(G_1,'Inverse of G=')\n", +"X=G_1*AT*B;\n", +"disp(X,'X=')\n", +"disp('Thus X and X1 are Same')" + ] + } +], +"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 +} |