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