{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 4 : Truncation Errors and the Taylor Series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: 4.1 Page No:79" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of f at x=0 : 1.2\n", "The value of f at x=1 due to zero order approximation : 1.2\n", "Truncation error : -1.0\n", "----------------------------------------------\n", "The value of first derivative of f at x=0 : -0.4\n", "The value of f at x=1 due to first order approximation : 0.8\n", "Truncation error : -0.6\n", "----------------------------------------------\n", "The value of second derivative of f at x=0 : -1.8\n", "The value of f at x=1 due to second order approximation : -0.1\n", "Truncation error : 0.3\n", "----------------------------------------------\n", "The value of third derivative of f at x=0 : -0.9\n", "The value of f at x=1 due to third order approximation : -0.25\n", "Truncation error : 0.45\n", "----------------------------------------------\n", "The value of fourth derivative of f at x=0 : -2.4\n", "The value of f at x=1 due to fourth order approximation : -0.35\n", "Truncation error : 0.55\n", "----------------------------------------------\n" ] } ], "source": [ "from math import factorial\n", "from scipy.misc import derivative\n", "def f(x):\n", " y=-0.1*x**4-0.15*x**3-0.5*x**2-0.25*x+1.2#\n", " return y\n", "xi=0#\n", "xf=1#\n", "h=xf-xi#\n", "fi=f(xi)##function value at xi\n", "ffa=f(xf)##actual function value at xf\n", "\n", "#for n=0, i.e, zero order approximation\n", "ff=fi#\n", "Et_1=ffa-ff##truncation error at x=1\n", "print \"The value of f at x=0 :\",fi\n", "print \"The value of f at x=1 due to zero order approximation :\",ff\n", "print \"Truncation error :\",Et_1\n", "print \"----------------------------------------------\"\n", "\n", "#for n=1, i.e, first order approximation\n", "def f1(x):\n", " y=derivative(f,x)\n", " return y\n", "f1i=f1(xi)##value of first derivative of function at xi\n", "f1f=fi+f1i*h##value of first derivative of function at xf\n", "Et_2=ffa-f1f##truncation error at x=1\n", "print \"The value of first derivative of f at x=0 :\",f1i\n", "print \"The value of f at x=1 due to first order approximation :\",f1f\n", "print \"Truncation error :\",Et_2\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=2, i.e, second order approximation\n", "def f2(x):\n", " y=derivative(f1,x)\n", " return y\n", "f2i=f2(xi)##value of second derivative of function at xi\n", "f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf\n", "Et_3=ffa-f2f##truncation error at x=1\n", "print \"The value of second derivative of f at x=0 :\",f2i\n", "print \"The value of f at x=1 due to second order approximation :\",f2f\n", "print \"Truncation error :\",Et_3\n", "print \"----------------------------------------------\"\n", "\n", "#for n=3, i.e, third order approximation\n", "def f3(x):\n", " y=derivative(f2,x)\n", " return y\n", "f3i=f3(xi)##value of third derivative of function at xi\n", "f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf\n", "Et_4=ffa-f3f##truncation error at x=1\n", "print \"The value of third derivative of f at x=0 :\",f3i\n", "print \"The value of f at x=1 due to third order approximation :\",f3f\n", "print \"Truncation error :\", Et_4\n", "print \"----------------------------------------------\"\n", "\n", "#for n=4, i.e, fourth order approximation\n", "def f4(x):\n", " y=derivative(f3,x)\n", " return y\n", "f4i=f4(xi)##value of fourth derivative of function at xi\n", "f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf\n", "Et_5=ffa-f4f##truncation error at x=1\n", "print \"The value of fourth derivative of f at x=0 :\",f4i\n", "print \"The value of f at x=1 due to fourth order approximation :\",f4f\n", "print \"Truncation error :\",Et_5\n", "print \"----------------------------------------------\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.2: Page No:82" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of f at x=1 due to zero order approximation : 0.707106781187\n", "% relative error : -41.4213562373\n", "----------------------------------------------\n", "The value of f at x=1 due to first order approximation : 0.551333569463\n", "% relative error : -10.2667138927\n", "----------------------------------------------\n", "The value of f at x=1 due to second order approximation : 0.534175415889\n", "% relative error : -6.83508317772\n", "----------------------------------------------\n", "The value of f at x=1 due to third order approximation : 0.535435376789\n", "% relative error : -7.08707535775\n", "----------------------------------------------\n", "The value of f at x=1 due to fourth order approximation : 0.535504768061\n", "% relative error : -7.10095361216\n", "----------------------------------------------\n", "The value of f at x=1 due to fifth order approximation : 0.535501917392\n", "% relative error : -7.10038347839\n", "----------------------------------------------\n", "The value of f at x=1 due to sixth order approximation : 0.535501819651\n", "% relative error : -7.10036393016\n", "----------------------------------------------\n" ] } ], "source": [ "from math import pi,cos,factorial\n", "from scipy.misc import derivative\n", "def f(x):\n", " y=cos(x)\n", " return y\n", "xi=pi/4#\n", "xf=pi/3#\n", "h=xf-xi#\n", "fi=f(xi)##function value at xi\n", "ffa=f(xf)##actual function value at xf\n", "\n", "#for n=0, i.e, zero order approximation\n", "ff=fi#\n", "et1=(ffa-ff)*100/ffa##percent relative error at x=1\n", "print \"The value of f at x=1 due to zero order approximation :\",ff\n", "print \"% relative error :\",et1\n", "print \"----------------------------------------------\"\n", "\n", "#for n=1, i.e, first order approximation\n", "def f1(x):\n", " y=derivative(f,x)\n", " return y\n", "f1i=f1(xi)##value of first derivative of function at xi\n", "f1f=fi+f1i*h##value of first derivative of function at xf\n", "et2=(ffa-f1f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to first order approximation :\",f1f\n", "print \"% relative error :\",et2\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=2, i.e, second order approximation\n", "def f2(x):\n", " y=derivative(f1,x)\n", " return y\n", "f2i=f2(xi)##value of second derivative of function at xi\n", "f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf\n", "et3=(ffa-f2f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to second order approximation :\",f2f\n", "print \"% relative error :\",et3\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=3, i.e, third order approximation\n", "def f3(x):\n", " y=derivative(f2,x)\n", " return y\n", "f3i=f3(xi)##value of third derivative of function at xi\n", "f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf\n", "et4=(ffa-f3f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to third order approximation :\",f3f\n", "print \"% relative error :\",et4\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=4, i.e, fourth order approximation\n", "def f4(x):\n", " y=derivative(f3,x)\n", " return y\n", "f4i=f4(xi)##value of fourth derivative of function at xi\n", "f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf\n", "et5=(ffa-f4f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to fourth order approximation :\",f4f\n", "print \"% relative error :\",et5\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=5, i.e, fifth order approximation\n", "f5i=(f4(1.1*xi)-f4(0.9*xi))/(2*0.1)##value of fifth derivative of function at xi (central difference method)\n", "f5f=f4f+f5i*(h**5)/factorial(5)##value of fifth derivative of function at xf\n", "et6=(ffa-f5f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to fifth order approximation :\",f5f\n", "print \"% relative error :\",et6\n", "print \"----------------------------------------------\"\n", "\n", "\n", "#for n=6, i.e, sixth order approximation\n", "def f6(x):\n", " y=derivative(f5,x)\n", " return y\n", "f6i=(f4(1.1*xi)-2*f4(xi)+f4(0.9*xi))/(0.1**2)##value of sixth derivative of function at xi (central difference method)\n", "f6f=f5f+f6i*(h**6)/factorial(6)##value of sixth derivative of function at xf\n", "et6=(ffa-f6f)*100/ffa##% relative error at x=1\n", "print \"The value of f at x=1 due to sixth order approximation :\",f6f\n", "print \"% relative error :\", et6\n", "print \"----------------------------------------------\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.3 : Page No:85" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input value of m:4\n", "Input value of h:5\n", "\n", "Remainder: 21 \n", "The value by first order approximation: 1275\n", "True Value at x2: 1296\n" ] } ], "source": [ "from math import pi,cos,factorial\n", "m=input(\"Input value of m:\")\n", "h=input(\"Input value of h:\")\n", "def f(x):\n", " y=x**m\n", " return y\n", "x1=1#\n", "x2=x1+h#\n", "fx1=f(x1)#\n", "fx2=fx1+m*(fx1**(m-1))*h#\n", "if m==1:\n", " R=0#\n", "elif m==2 :\n", " R=2*(h**2)/factorial(2)#\n", " \n", "elif m==3:\n", " R=(6*(x1)*(h**2)/factorial(2))+(6*(h**3)/factorial(3))#\n", " \n", "elif m==4:\n", " R=(12*(x1**2)*(h**2)/factorial(2))+(24*(x1)*(h**3)/factorial(3))+(24*(h**4)/factorial(4))\n", " \n", "print \"\\nRemainder:\",fx2,\"\\nThe value by first order approximation:\",R\n", "print \"True Value at x2:\",f(x2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.4: Page No:92" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input h:1.232323\n", "For h= 1.232323\n", "and percent error= -2.70944264922 Derivative at x by forward difference method= 114.60931875\n", "and percent error= -0.178591334206 Derivative at x by backward difference method= 85.854151746\n", "and percent error= -1.44401699172 Derivative at x by central difference method= 14.3775835022\n" ] } ], "source": [ "from scipy.misc import derivative\n", "def f(x):\n", " y=-0.1*(x**4)-0.15*(x**3)-0.5*(x**2)-0.25*(x)+1.2\n", " return y\n", "x=0.5#\n", "h=input(\"Input h:\")\n", "x1=x-h#\n", "x2=x+h#\n", "#forward difference method\n", "fdx1=(f(x2)-f(x))/h##derivative at x\n", "et1=abs((fdx1-derivative(f,x))/derivative(f,x))*100#\n", "#backward difference method\n", "fdx2=(f(x)-f(x1))/h##derivative at x\n", "et2=abs((fdx2-derivative(f,x))/derivative(f,x))*100#\n", "#central difference method\n", "fdx3=(f(x2)-f(x1))/(2*h)##derivative at x\n", "et3=abs((fdx3-derivative(f,x))/derivative(f,x))*100#\n", "print \"For h=\",h\n", "print \"and percent error=\",fdx1,\"Derivative at x by forward difference method=\",et1\n", "print \"and percent error=\",fdx2,\"Derivative at x by backward difference method=\",et2\n", "print \"and percent error=\",fdx3,\"Derivative at x by central difference method=\",et3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.5: Page No: 95" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true value is between : 15.4275 and 15.8225\n" ] } ], "source": [ "from scipy.misc import derivative\n", "def f(x):\n", " y=x**3\n", " return y\n", "x=2.5#\n", "delta=0.01#\n", "deltafx=abs(derivative(f,x))*delta#\n", "fx=f(x)#\n", "print \"true value is between : \",fx-deltafx,\"and\",fx+deltafx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.6: Page No: 96" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of y is between: 0.528721343471 and 0.596278656529\n", "ymin is calculated at lower extremes of F, L, E, I values as = 0.524066539965\n", "ymax is calculated at higher extremes of F, L, E, I values as = 0.602846335915\n" ] } ], "source": [ "def f(F,L,E,I):\n", " y=(F*(L**4))/(8*E*I)\n", " return y\n", "Fbar=50##lb/ft\n", "Lbar=30##ft\n", "Ebar=1.5*(10**8)##lb/ft**2\n", "Ibar=0.06##ft**4\n", "deltaF=2##lb/ft\n", "deltaL=0.1##ft\n", "deltaE=0.01*(10**8)##lb/ft**2\n", "deltaI=0.0006##ft**4\n", "ybar=(Fbar*(Lbar**4))/(8*Ebar*Ibar)#\n", "def f1(F):\n", " y=(F*(Lbar**4))/(8*Ebar*Ibar)\n", " return y\n", "def f2(L):\n", " y=(Fbar*(L**4))/(8*Ebar*Ibar)\n", " return y\n", "def f3(E):\n", " y=(Fbar*(Lbar**4))/(8*E*Ibar)\n", " return y\n", "def f4(I):\n", " y=(Fbar*(Lbar**4))/(8*Ebar*I)\n", " return y\n", "\n", "deltay=abs(derivative(f1,Fbar))*deltaF+abs(derivative(f2,Lbar))*deltaL+abs(derivative(f3,Ebar))*deltaE+abs(derivative(f4,Ibar))*deltaI#\n", "\n", "print \"The value of y is between:\",ybar-deltay,\"and\",ybar+deltay\n", "ymin=((Fbar-deltaF)*((Lbar-deltaL)**4))/(8*(Ebar+deltaE)*(Ibar+deltaI))#\n", "ymax=((Fbar+deltaF)*((Lbar+deltaL)**4))/(8*(Ebar-deltaE)*(Ibar-deltaI))#\n", "print \"ymin is calculated at lower extremes of F, L, E, I values as =\",ymin\n", "print \"ymax is calculated at higher extremes of F, L, E, I values as =\",ymax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4.7 : Page No:98" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The condition number of function for x = 0.18201112073 is : 1.72787595947\n", "The condition number of function for x = 0.0160083243793 is : 1.58650429006\n" ] } ], "source": [ "from math import pi,tan\n", "from scipy.misc import derivative\n", "def f(x):\n", " y=tan(x)\n", " return y\n", "x1bar=(pi/2)+0.1*(pi/2)#\n", "x2bar=(pi/2)+0.01*(pi/2)#\n", "#computing condition number for x1bar\n", "condnum1=x1bar*derivative(f,x1bar)/f(x1bar)#\n", "print \"The condition number of function for x =\",condnum1,\"is :\",x1bar\n", "if abs(condnum1)>1:\n", " print \"Function is ill-conditioned for x =\",x1bar\n", "\n", "#computing condition number for x2bar\n", "condnum2=x2bar*derivative(f,x2bar)/f(x2bar)#\n", "print \"The condition number of function for x =\",condnum2,\"is :\",x2bar\n", "if abs(condnum2)>1:\n", " print \"Function is ill-conditioned for x =\",x2bar" ] } ], "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 }