diff options
Diffstat (limited to 'Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb')
-rw-r--r-- | Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb | 891 |
1 files changed, 891 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb b/Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb new file mode 100644 index 00000000..832b55d6 --- /dev/null +++ b/Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb @@ -0,0 +1,891 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 4 - Approximations and errors in computing" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_01 Pg No. 63" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " 4.3201 has a precision of 10**-4\n", + "\n", + "\n", + " 4.32 has a precision of 10**-2\n", + "\n", + "\n", + " 4.320106 has a precision of 10**-6\n", + "\n", + "\n", + " The number with highest precision is 4.320106\n", + "\n" + ] + } + ], + "source": [ + "#Greatest precision\n", + "\n", + "a = '4.3201'\n", + "b = '4.32'\n", + "c = '4.320106'\n", + "na = len(a)-(a.index('.')+1)\n", + "print '\\n %s has a precision of 10**-%d\\n'%(a,na)\n", + "nb = len(b)-(b.index('.')+1)\n", + "print '\\n %s has a precision of 10**-%d\\n'%(b,nb)\n", + "nc = len(c)-c.index('.')-1\n", + "print '\\n %s has a precision of 10**-%d\\n'%(c,nc)\n", + "e = max(na,nb,nc)\n", + "if e ==na:\n", + " print '\\n The number with highest precision is %s\\n'%(a)\n", + "elif e == nb:\n", + " print '\\n The number with highest precision is %s\\n'%(b)\n", + "else:\n", + " print '\\n The number with highest precision is %s\\n'%(c)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_02 Pg No. 63" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "95.763 has 5 significant digits\n", + "\n", + "0.008472 has 7 significant digits.The leading or higher order zeros are only place holders\n", + "\n", + "0.0456000 has 8 significant digits\n", + "\n", + "36.0 has 3 significant digits\n", + "\n", + "3600.00 has 6 significant digits\n", + "\n" + ] + } + ], + "source": [ + "#Accuracy of numbers\n", + "\n", + "def sd(x):\n", + " nd = x.index('.') #position of point\n", + " num = [ord(c) for c in x] #str2code(x)\n", + " if (nd)==None and num(length(x)) == 0:\n", + " print 'Accuracy is not specified\\n'\n", + " n = 0 #\n", + " else:\n", + " if num[0]>= 1 and (nd==None):\n", + " n = len(x)\n", + " elif num[1] >= 1 and not((nd==None)):\n", + " n = len(x) - 1\n", + " else:\n", + " for i in range(0,len(x)):\n", + " if num[i] >= 1 and num[i] <= 9:\n", + " break;\n", + " \n", + " \n", + " n = len(x)- i + 1\n", + " return n\n", + " \n", + " \n", + "\n", + "a = '95.763'\n", + "na = sd(a)\n", + "print '%s has %d significant digits\\n'%(a,na)\n", + "b = '0.008472'\n", + "nb = sd(b)\n", + "print '%s has %d significant digits.The leading or higher order zeros are only place holders\\n'%(b,nb)\n", + "c = '0.0456000'\n", + "nc = sd(c)\n", + "print '%s has %d significant digits\\n'%(c,nc)\n", + "d = '36.0'\n", + "nd = sd(d)\n", + "print '%s has %d significant digits\\n'%(d,nd)\n", + "e = '3600.0'\n", + "sd(e)\n", + "f = '3600.00'\n", + "nf = sd(f)\n", + "print '%s has %d significant digits\\n'%(f,nf)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_03 Pg No. 64" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " 0.1_10 = 0.[0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0] \n", + " 0.4_10 = 0.[0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0] \n", + " \n", + "sum = 0.9921875\n", + "Note : The answer should be 0.5, but it is not so.This is due to the error in conversion from decimal to binary form.\n" + ] + } + ], + "source": [ + "from __future__ import division\n", + "from math import floor\n", + "from numpy import arange,zeros\n", + "a = 0.1\n", + "b = 0.4\n", + "afrac=[];bfrac=[];\n", + "for i in range(0,8):\n", + " afrac.append(floor(a*2))\n", + " a = a*2 - floor(a*2)\n", + " bfrac.append(floor(b*2))\n", + " b = b*2 - floor(b*2)\n", + "\n", + "afrac_s = '0' + '.' +(str(afrac)) #string form binary equivalent of a i.e 0.1\n", + "bfrac_s = '0' + '.' +(str(bfrac))\n", + "print '\\n 0.1_10 = %s \\n 0.4_10 = %s \\n '%( afrac_s , bfrac_s)\n", + " \n", + "summ=zeros(8)\n", + "for j in arange(7,0,-1):\n", + " summ[j] = afrac[j] + bfrac[j]\n", + " if summ[j] > 1:\n", + " summ[j] = summ[j]-2\n", + " afrac[(j-1)] = afrac[(j-1)] + 1\n", + "summ_dec = 0\n", + "for k in arange(7,0,-1):\n", + " summ_dec = summ_dec + summ[k]\n", + " summ_dec = summ_dec*1.0/2 \n", + "\n", + "print 'sum =',summ_dec\n", + "print 'Note : The answer should be 0.5, but it is not so.This is due to the error in conversion from decimal to binary form.' " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_04 Pg No. 66" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Chooping Method : \n", + " Approximate x = 0.7526*10**3 \n", + " Error = 0.0835 \n", + " \n", + "\n", + " Symmetric Rounding :\n", + " Approximate x = 0.7527*10**3 \n", + " Error = -0.0165 \n", + " \n" + ] + } + ], + "source": [ + "#Rounding-Off\n", + "\n", + "fx = 0.7526\n", + "E =3\n", + "gx = 0.835\n", + "d = E - (-1)\n", + "#Chopping Method\n", + "Approx_x = fx*10**E\n", + "Err = gx*10**(E-d)\n", + "print '\\n Chooping Method : \\n Approximate x = %.4f*10**%d \\n Error = %.4f \\n '%(fx,E,Err)\n", + "#Symmetric Method\n", + "if gx >= 0.5:\n", + " Err = (gx -1)*10**(-1)\n", + " Approx_x = (fx + 10**(-d))*10**E\n", + "else:\n", + " Approx_x = fx*10**E\n", + " Err = gx * 10**(E-d)\n", + "\n", + "print '\\n Symmetric Rounding :\\n Approximate x = %.4f*10**%d \\n Error = %.4f \\n '%(fx + 10**(-d),E,Err)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_05 Pg No. 68" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " a) When first three terms are used \n", + " Truncation error = 1.402756E-03 \n", + " \n", + "\n", + " b) When first four terms are used \n", + " Truncation error = 6.942222E-05 \n", + " \n", + "\n", + " c) When first five terms are used \n", + " Truncation error = 2.755556E-06 \n", + " \n" + ] + } + ], + "source": [ + "from scipy.misc import factorial\n", + "#Truncation Error\n", + "\n", + "x = 1.0/5\n", + "#When first three terms are used\n", + "Trunc_err = x**3/factorial(3) + x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n a) When first three terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)\n", + "\n", + "#When four terms are used\n", + "Trunc_err = x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n b) When first four terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)\n", + "\n", + "#When Five terms are used\n", + "Trunc_err = x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n c) When first five terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_06 Pg No. 68" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " a) When first three terms are used \n", + " Truncation error = -1.269244E-03 \n", + " \n", + "\n", + " b) When first four terms are used \n", + " Truncation error = 6.408889E-05 \n", + " \n", + "\n", + " c) When first five terms are used \n", + " Truncation error = -2.577778E-06 \n", + " \n" + ] + } + ], + "source": [ + "from scipy.misc import factorial\n", + "\n", + "#Truncation Error\n", + "\n", + "x = -1.0/5\n", + "#When first three terms are used\n", + "Trunc_err = x**3/factorial(3) + x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n a) When first three terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)\n", + "\n", + "#When four terms are used\n", + "Trunc_err = x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n b) When first four terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)\n", + "\n", + "#When Five terms are used\n", + "Trunc_err = x**5/factorial(5) + x**6/factorial(6)\n", + "print '\\n c) When first five terms are used \\n Truncation error = %.6E \\n '%(Trunc_err)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_07 Pg No. 71" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " For Building : \n", + " Absolute error, e1 = 5 \n", + " Relative error , e1_r = 0.17 percent \n", + " \n", + "\n", + " For Beam : \n", + " Absolute error, e2 = 5 \n", + " Relative error , e2_r = 17 percent \n", + " \n" + ] + } + ], + "source": [ + "#Absolute and Relative Errors\n", + "\n", + "h_bu_t = 2945 #\n", + "h_bu_a = 2950 #\n", + "h_be_t = 30 #\n", + "h_be_a = 35 #\n", + "e1 = abs(h_bu_t - h_bu_a)\n", + "e1_r = e1/h_bu_t\n", + "e2 = abs(h_be_t - h_be_a)\n", + "e2_r = e2/h_be_t\n", + "print '\\n For Building : \\n Absolute error, e1 = %d \\n Relative error , e1_r = %.2f percent \\n '%(e1,e1_r*100) \n", + "print '\\n For Beam : \\n Absolute error, e2 = %d \\n Relative error , e2_r = %.2G percent \\n '%(e2,e2_r*100)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_08 Pg No. 72" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "q = 7.9 \n", + " We can say that the computer can store numbers with 7 significant decimal digits \n", + " \n" + ] + } + ], + "source": [ + "from math import log10\n", + "#Machine Epsilon\n", + "\n", + "def Q(p):\n", + " q = 1 + (p-1)*log10(2)\n", + " return q\n", + "p = 24\n", + "q = Q(p)\n", + "print 'q = %.1f \\n We can say that the computer can store numbers with %d significant decimal digits \\n '%(q,q)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_09 Pg No. 75" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " |er_x| <= 0.05 o/o\n", + " |er_y| <= 0.05o/o \n", + " ex = 0.617 \n", + " ey = 0.616 \n", + " |ez| = 1.233 \n", + " |er_z| = 61.65o/o \n", + "\n" + ] + } + ], + "source": [ + "#Propagation of Error\n", + "\n", + "x = 0.1234*10**4\n", + "y = 0.1232*10**4\n", + "d = 4\n", + "er_x = 10**(-d + 1)/2\n", + "er_y = 10**(-d + 1)/2\n", + "ex = x*er_x\n", + "ey = y*er_y\n", + "ez = abs(ex) + abs(ey)\n", + "er_z = abs(ez)/abs(x-y)\n", + "\n", + "print '\\n |er_x| <= %.2f o/o\\n |er_y| <= %.2fo/o \\n ex = %.3f \\n ey = %.3f \\n |ez| = %.3f \\n |er_z| = %.2fo/o \\n'%(er_x *100,er_y*100,ex,ey,ez,er_z*100)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_10 Pg No. 77" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " ex = 0.01175 \n", + " ey = 0.03370 \n", + " ez = 0.01725 \n", + " exy = 0.15839 \n", + " ew = 0.17564 \n", + "\n" + ] + } + ], + "source": [ + "#Errors in Sequence of Computations\n", + "\n", + "x_a = 2.35 #\n", + "y_a = 6.74 #\n", + "z_a = 3.45 #\n", + "ex = abs(x_a)*10**(-3+1)/2\n", + "ey = abs(y_a)*10**(-3+1)/2\n", + "ez = abs(z_a)*10**(-3+1)/2\n", + "exy = abs(x_a)*ey + abs(y_a)*ex\n", + "ew = abs(exy) + abs(ez)\n", + "print '\\n ex = %.5f \\n ey = %.5f \\n ez = %.5f \\n exy = %.5f \\n ew = %.5f \\n'%(ex,ey,ez,exy,ew)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_11 Pg No. 77" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "w = 10000.0\n", + "True w = 10444\n", + "ew = 444.0\n", + "er,w = 0.0425124473382\n" + ] + } + ], + "source": [ + "from math import floor\n", + "#Addition of Chain of Numbers\n", + "\n", + "x = 9678 #\n", + "y = 678 #\n", + "z = 78 #\n", + "d = 4 # #length of mantissa\n", + "fx = x/10**4\n", + "fy = y/10**4\n", + "fu = fx + fy\n", + "Eu = 4\n", + "if fu >= 1:\n", + " fu = fu/10\n", + " Eu = Eu + 1\n", + "\n", + "#since length of mantissa is only four we need to maintain only four places in decimal, so\n", + "fu = floor(fu*10**4)/10**4\n", + "u = fu * 10**Eu\n", + "w = u + z\n", + "n = len(str(w))\n", + "w = floor(w/10**(n-4))*10**(n-4) #To maintain length of mantissa = 4\n", + "print 'w =',w\n", + "True_w = 10444\n", + "ew = True_w - w\n", + "er_w = (True_w - w)/True_w\n", + "print 'True w =',True_w\n", + "print 'ew =',ew\n", + "print 'er,w =',er_w " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_12 Pg No. 77" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "w = 10430.0\n", + "True w = 10444\n", + "ew = 14.0\n", + "er,w = 0.00134048257373\n" + ] + } + ], + "source": [ + "#Addition of chain Numbers\n", + "\n", + "x = 9678 #\n", + "y = 678 #\n", + "z = 78 #\n", + "d = 4 # #length of mantissa\n", + "n = max(len( str(y) ) , len(str(z)))\n", + "fy = y/10**n\n", + "fz = z/10**n\n", + "fu = fy + fz\n", + "Eu = n\n", + "if fu >= 1:\n", + " fu = fu/10\n", + " Eu = Eu + 1\n", + "\n", + "u = fu * 10**Eu\n", + "n = max(len( str(x) ) , len(str(u)))\n", + "fu = u/10**4\n", + "fx = x/10**4\n", + "fw = fu + fx\n", + "Ew = 4\n", + "if fw >= 1:\n", + " fw = fw/10\n", + " Ew = Ew + 1\n", + "\n", + "#since length of mantissa is only four we need to maintain only four places in decimal, so\n", + "fw = floor(fw*10**4)/10**4\n", + "w = fw*10**Ew\n", + "print 'w =',w\n", + "True_w = 10444\n", + "ew = True_w - w\n", + "er_w = (True_w - w)/True_w\n", + "print 'True w =',True_w\n", + "print 'ew =',ew \n", + "print 'er,w =',er_w" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_14 Pg No. 79" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " ex = 5E-04 \n", + " df(xa) = 1.25 \n", + " ef = 6.26E-04 \n", + " er,f = 1.04E-04 \n", + "\n" + ] + } + ], + "source": [ + "from math import sqrt\n", + "from scipy.misc import derivative\n", + "#Absolute & Relative Errors\n", + "\n", + "xa = 4.000\n", + "def f(x):\n", + " f = sqrt(x) + x\n", + " return f\n", + "#Assuming x is correct to 4 significant digits\n", + "ex = 0.5 * 10**(-4 + 1)\n", + "df_xa = derivative(f,4)\n", + "ef = ex * df_xa\n", + "er_f = ef/f(xa)\n", + "print '\\n ex = %.0E \\n df(xa) = %.2f \\n ef = %.2E \\n er,f = %.2E \\n'%( ex,df_xa,ef,er_f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_15 Pg No. 80" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ef = 0.07\n" + ] + } + ], + "source": [ + "#Error Evaluation\n", + "\n", + "x = 3.00 #\n", + "y = 4.00 #\n", + "def f(x,y):\n", + " f = x**2 + y**2\n", + " return f\n", + "def df_x(x):\n", + " df_x = 2*x\n", + " return df_x\n", + "def df_y(y):\n", + " df_y = 2*y\n", + " return df_y\n", + "ex = 0.005\n", + "ey = 0.005\n", + "ef = df_x(x)*ex + df_y(y)*ey\n", + "print 'ef =',ef" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_16 Pg No. 82" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x = 400.0\n", + "y = 807.0\n", + "Changing m2 from 2.01 to 2.005\n", + "\n", + " x = 800 \n", + " y = 1607 \n", + " From the above results we can see that for small change in m2 results in almost 100 percent change in the values of x and y.Therefore, the problem is absolutely ill-conditioned \n", + "\n" + ] + } + ], + "source": [ + "#Condition and Stability\n", + "\n", + "C1 = 7.00 #\n", + "C2 = 3.00 #\n", + "m1 = 2.00 #\n", + "m2 = 2.01 #\n", + "x = (C1 - C2)/(m2 - m1)\n", + "y = m1*((C1 - C2)/(m2 - m1)) + C1\n", + "print 'x =',x\n", + "print 'y =',y\n", + "print 'Changing m2 from 2.01 to 2.005'\n", + "m2 = 2.005\n", + "x = (C1 - C2)/(m2 - m1)\n", + "y = m1*((C1 - C2)/(m2 - m1)) + C1\n", + "print '\\n x = %d \\n y = %d \\n From the above results we can see that for small change in m2 results in almost 100 percent change in the values of x and y.Therefore, the problem is absolutely ill-conditioned \\n'%(x,y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 4_18 Pg No. 84" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z = sqrt(x) - sqrt(y) = 0.0\n", + "z = ( x-y )/( sqrt(x) + sqrt(y) ) = 0.022\n" + ] + } + ], + "source": [ + "from math import sqrt, floor\n", + "#Difference of Square roots\n", + "\n", + "x = 497.0 #\n", + "y = 496.0 #\n", + "sqrt_x = sqrt(497)\n", + "sqrt_y = sqrt(496)\n", + "nx = len( str( floor( sqrt_x ) ) )\n", + "ny = len( str( floor( sqrt_y ) ) )\n", + "sqrt_x = floor(sqrt_x*10**(4-nx))/10**(4-nx)\n", + "sqrt_y = floor(sqrt_y*10**(4-ny))/10**(4-ny)\n", + "z1 = sqrt_x - sqrt_y\n", + "print 'z = sqrt(x) - sqrt(y) =',z1\n", + "z2 = ( x -y)/(sqrt_x + sqrt_y)\n", + "if z2 < 0.1:\n", + " z2 = z2*10**4\n", + " nz = len(str(floor(z2)))\n", + " z2 = floor(z2*10**(4-nz))/10**(8-nz)\n", + "\n", + "print 'z = ( x-y )/( sqrt(x) + sqrt(y) ) =',z2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4_21 Pg No. 85" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Truncation Error = 0.000505399929331\n", + "calculated e**x using roundoff = -0.000459999999793\n", + "actual e**x = 4.53999297625e-05\n", + "Here we can see the difference between calculated e**x and actual e**x this is due to trucation error (which is greater than final value of e**x ), so the roundoff error totally dominates the solution\n" + ] + } + ], + "source": [ + "from __future__ import division\n", + "from math import exp,floor\n", + "x = -10\n", + "T_act=[1]\n", + "T_trc=[1]\n", + "e_x_cal = 1\n", + "TE=[]\n", + "for i in range(0,100):\n", + " T_act.append(T_act[i]*x/(i+1))\n", + " T_trc.append(floor(T_act[i+1]*10**5)/10**5)\n", + " TE.append(abs(T_act[i+1]-T_trc[i+1]))\n", + " e_x_cal = e_x_cal + T_trc[i+1]\n", + "e_x_act = exp(-10)\n", + "Sum=0\n", + "for s in TE:\n", + " Sum+=s\n", + "print 'Truncation Error =',Sum\n", + "print 'calculated e**x using roundoff =',e_x_cal\n", + "print 'actual e**x = ',e_x_act\n", + "print 'Here we can see the difference between calculated e**x and actual e**x this is due to trucation error (which is greater than final value of e**x ), so the roundoff error totally dominates the solution'" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |