{ "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 }