{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 11: Numerical Differentiation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.1: First_order_Forward_Difference.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_01\n", "//First order forward difference\n", "//Pg No. 348\n", "clear ;close ;clc ;\n", "\n", "x = poly(0,'x');\n", "deff('F = f(x)','F = x^2');\n", "deff('DF = df(x,h)','DF = (f(x+h)-f(x))/h');\n", "dfactual = derivat(f(x));\n", "h = [0.2 ; 0.1 ; 0.05 ; 0.01 ]\n", "for i = 1:4\n", " y(i) = df(1,h(i));\n", " err(i) = y(i) - horner(dfactual,1)\n", "end\n", "table = [h y err];\n", "disp(table)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.2: Three_Point_Formula.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_02\n", "//Three-Point Formula\n", "//Pg No. 350\n", "clear ;close ;clc ;\n", "\n", "x = poly(0,'x');\n", "deff('F = f(x)','F = x^2');\n", "deff('DF = df(x,h)','DF = (f(x+h)-f(x-h))/(2*h)');\n", "dfactual = derivat(f(x));\n", "h = [0.2 ; 0.1 ; 0.05 ; 0.01 ]\n", "for i = 1:4\n", " y(i) = df(1,h(i));\n", " err(i) = y(i) - horner(dfactual,1)\n", "end\n", "table = [h y err];\n", "disp(table)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.3: Error_Analysis.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_03\n", "//Pg No. 353\n", "close ;clear ;clc ;\n", "\n", "x = 0.45;\n", "deff('F = f(x)','F = sin(x)');\n", "deff('DF = df(x,h)','DF = (f(x+h) - f(x))/h');\n", "dfactual = cos(x);\n", "h = 0.01:0.005:0.04;\n", "n = length(h);\n", "for i = 1:n\n", " y(i) = df(x,h(i))\n", " err(i) = y(i) - dfactual ;\n", "end\n", "table = [ h' y err];\n", "disp(table)\n", "//scilab uses 16 significant digits so the bound for roundoff error is 0.5*10^(-16)\n", "e = 0.5*10^(-16)\n", "M2 = max(sin(x+h));\n", "hopt = 2*sqrt(e/M2);\n", "disp(hopt,'hopt = ',M2,'M2 = ')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.4: Approximate_Second_Derivative.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_04\n", "//Approximate second derivative\n", "//Pg No. 354\n", "clear ;close ;clc ;\n", "\n", "x = 0.75;\n", "h = 0.01;\n", "deff('F = f(x)','F = cos(x)');\n", "deff('D2F = d2f(x,h)','D2F = ( f(x+h) - 2*f(x) + f(x-h) )/h^2 ');\n", "y = d2f(0.75,0.01);\n", "d2fexact = -cos(0.75)\n", "err = d2fexact - y ;\n", "disp(err,'err = ',d2fexact,'d2fexact = ',y,'y = ')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.5: Differentiation_of_Tabulated_Data.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_05\n", "//Differentiation of tabulated data\n", "//Pg No. 358\n", "clear ;close ;clc ;\n", "\n", "T = 5:9 ;\n", "s = [10 14.5 19.5 25.5 32 ];\n", "h = T(2)-T(1);\n", "n = length(T)\n", "function V = v(t)\n", " if find(T == t) == 1 then\n", " V = [ -3*s( find(T == t) ) + 4*s( find(T==(t+h)) ) - s( find( T == (t+2*h) ) ) ]/(2*h) //Three point forward difference formula\n", " elseif find(T == t) == n\n", " V = [ 3*s( find(T == t) ) - 4*s( find(T==(t-h)) ) + s( find( T == (t-2*h) ) ) ]/(2*h) //Backward difference formula\n", " else\n", " V = [ s( find(T == (t+h)) ) - s( find(T == (t-h)) ) ]/(2*h) //Central difference formula\n", " end\n", "endfunction\n", "\n", "v_5 = v(5)\n", "v_7 = v(7)\n", "v_9 = v(9)\n", "\n", "disp(v_9,'v(9) = ',v_7,'v(7) = ',v_5,'v(5) = ')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.6: Three_Point_Central_Difference_Formula.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_06\n", "//Three Point Central Difference formula\n", "//Pg No. 359\n", "clear ;close ;clc ;\n", "\n", "T = 5:9 ;\n", "s = [10 14.5 19.5 25.5 32 ];\n", "h = T(2)-T(1);\n", "deff('A = a(t)','A = [ s( find( T == (t+h) ) ) - 2*s( find( T == t) ) + s( find( T == (t-h) ) ) ]/h^2')\n", "a_7 = a(7)\n", "\n", "disp(a_7,'a(7) = ')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.7: Second_order_Derivative.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 11_7\n", "//Pg No. 359\n", "clear ; close ; clc ;\n", "\n", "h = 0.25 ;\n", "//y''(x) = e^(x^2)\n", "//y(0) = 0 , y(1) = 0\n", "// y''(x) = y(x+h) - 2*y(x) + y(x-h)/h^2 = e^(x^2)\n", "//( y(x + 0.25) - 2*y(x) + y(x-0.25) )/0.0625 = e^(x^2)\n", "//y(x+0.25) - 2*y(x) + y(x - 0.25) = 0.0624*e^(x^2)\n", "//y(0.5) - 2*y(0.25) + y(0) = 0.0665\n", "//y(0.75) - 2*y(0.5) + y(0.25) = 0.0803\n", "//y(1) - 2*y(0.75) + y(0.5) = 0.1097\n", "//given y(0) = y(1) = 0\n", "//\n", "//0 + y2 - 2y1 = 0.06665\n", "//y3 - 2*y2 + y1 = 0.0803\n", "//-2*y3 + y2 + 0 = 0.1097\n", "//Therefore\n", "A = [0 1 -2 ; 1 -2 1 ; -2 1 0 ]\n", "B = [ 0.06665 ; 0.0803 ; 0.1097 ]\n", "C = A\B \n", "mprintf('solving the above equations we get \n\n y1 = y(0.25) = %f \n y2 = y(0.5) = %f \n y3 = y(0.75) = %f \n ',C(3),C(2),C(1))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 11.8: Richardsons_Extrapolation_Technique.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 9_01\n", "//Richardson's Extrapolation Technique\n", "//Pg No. 362\n", "clear ;close ;clc ;\n", "\n", "x = -0.5:0.25:1.5\n", "h = 0.5 ;\n", "r = 1/2 ;\n", "\n", "deff('F = f(x)','F = exp(x)');\n", "deff('D = D(x,h)' ,'D = [ f(x + h) - f(x-h) ]/(2*h) ' );\n", "deff('df = df(x,h,r)','df = [D(x,r*h) - r^2*D(x,h)]/(1-r^2)');\n", "\n", "df_05 = df(0.5,0.5,1/2);\n", "disp(df_05,'richardsons technique - df(0.5) = ',D(0.5,0.25),'D(rh) = D(0.25) = ',D(0.5,0.5),'D(0.5) = ')\n", "dfexact = derivative(f,0.5)\n", "disp(dfexact,'Exact df(0.5) = ')\n", "disp('The result by richardsons technique is much better than other results')\n", "\n", "//r = 2\n", "disp(df(0.5,0.5,2),'df(x) = ',D(0.5,2*0.5),'D(rh) = ','for r = 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 }