# Chapter 4 : Truncation Errors and the Taylor Series¶

## Example: 4.1 Page No:79¶

In :
from math import factorial
from scipy.misc import derivative
def f(x):
y=-0.1*x**4-0.15*x**3-0.5*x**2-0.25*x+1.2#
return y
xi=0#
xf=1#
h=xf-xi#
fi=f(xi)##function value at xi
ffa=f(xf)##actual function value at xf

#for n=0, i.e, zero order approximation
ff=fi#
Et_1=ffa-ff##truncation error at x=1
print "The value of f at x=0 :",fi
print "The value of f at x=1 due to zero order approximation :",ff
print "Truncation error :",Et_1
print "----------------------------------------------"

#for n=1, i.e, first order approximation
def f1(x):
y=derivative(f,x)
return y
f1i=f1(xi)##value of first derivative of function at xi
f1f=fi+f1i*h##value of first derivative of function at xf
Et_2=ffa-f1f##truncation error at x=1
print "The value of first derivative of f at x=0 :",f1i
print "The value of f at x=1 due to first order approximation :",f1f
print "Truncation error :",Et_2
print "----------------------------------------------"

#for n=2, i.e, second order approximation
def f2(x):
y=derivative(f1,x)
return y
f2i=f2(xi)##value of second derivative of function at xi
f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf
Et_3=ffa-f2f##truncation error at x=1
print "The value of second derivative of f at x=0 :",f2i
print "The value of f at x=1 due to second order approximation :",f2f
print "Truncation error :",Et_3
print "----------------------------------------------"

#for n=3, i.e, third order approximation
def f3(x):
y=derivative(f2,x)
return y
f3i=f3(xi)##value of third derivative of function at xi
f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf
Et_4=ffa-f3f##truncation error at x=1
print "The value of third derivative of f at x=0 :",f3i
print "The value of f at x=1 due to third order approximation :",f3f
print "Truncation error :", Et_4
print "----------------------------------------------"

#for n=4, i.e, fourth order approximation
def f4(x):
y=derivative(f3,x)
return y
f4i=f4(xi)##value of fourth derivative of function at xi
f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf
Et_5=ffa-f4f##truncation error at x=1
print "The value of fourth derivative of f at x=0 :",f4i
print "The value of f at x=1 due to fourth order approximation :",f4f
print "Truncation error :",Et_5
print "----------------------------------------------"

The value of f at x=0 : 1.2
The value of f at x=1 due to zero order approximation : 1.2
Truncation error : -1.0
----------------------------------------------
The value of first derivative of f at x=0 : -0.4
The value of f at x=1 due to first order approximation : 0.8
Truncation error : -0.6
----------------------------------------------
The value of second derivative of f at x=0 : -1.8
The value of f at x=1 due to second order approximation : -0.1
Truncation error : 0.3
----------------------------------------------
The value of third derivative of f at x=0 : -0.9
The value of f at x=1 due to third order approximation : -0.25
Truncation error : 0.45
----------------------------------------------
The value of fourth derivative of f at x=0 : -2.4
The value of f at x=1 due to fourth order approximation : -0.35
Truncation error : 0.55
----------------------------------------------


## Example 4.2: Page No:82¶

In :
from math import pi,cos,factorial
from scipy.misc import derivative
def f(x):
y=cos(x)
return y
xi=pi/4#
xf=pi/3#
h=xf-xi#
fi=f(xi)##function value at xi
ffa=f(xf)##actual function value at xf

#for n=0, i.e, zero order approximation
ff=fi#
et1=(ffa-ff)*100/ffa##percent relative error at x=1
print "The value of f at x=1 due to zero order approximation :",ff
print "% relative error :",et1
print "----------------------------------------------"

#for n=1, i.e, first order approximation
def f1(x):
y=derivative(f,x)
return y
f1i=f1(xi)##value of first derivative of function at xi
f1f=fi+f1i*h##value of first derivative of function at xf
et2=(ffa-f1f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to first order approximation :",f1f
print "% relative error :",et2
print "----------------------------------------------"

#for n=2, i.e, second order approximation
def f2(x):
y=derivative(f1,x)
return y
f2i=f2(xi)##value of second derivative of function at xi
f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf
et3=(ffa-f2f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to second order approximation :",f2f
print "% relative error :",et3
print "----------------------------------------------"

#for n=3, i.e, third order approximation
def f3(x):
y=derivative(f2,x)
return y
f3i=f3(xi)##value of third derivative of function at xi
f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf
et4=(ffa-f3f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to third order approximation :",f3f
print "% relative error :",et4
print "----------------------------------------------"

#for n=4, i.e, fourth order approximation
def f4(x):
y=derivative(f3,x)
return y
f4i=f4(xi)##value of fourth derivative of function at xi
f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf
et5=(ffa-f4f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to fourth order approximation :",f4f
print "% relative error :",et5
print "----------------------------------------------"

#for n=5, i.e, fifth order approximation
f5i=(f4(1.1*xi)-f4(0.9*xi))/(2*0.1)##value of fifth derivative of function at xi (central difference method)
f5f=f4f+f5i*(h**5)/factorial(5)##value of fifth derivative of function at xf
et6=(ffa-f5f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to fifth order approximation :",f5f
print "% relative error :",et6
print "----------------------------------------------"

#for n=6, i.e, sixth order approximation
def f6(x):
y=derivative(f5,x)
return y
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)
f6f=f5f+f6i*(h**6)/factorial(6)##value of sixth derivative of function at xf
et6=(ffa-f6f)*100/ffa##% relative error at x=1
print "The value of f at x=1 due to sixth order approximation :",f6f
print "% relative error :", et6
print "----------------------------------------------"

The value of f at x=1 due to zero order approximation : 0.707106781187
% relative error : -41.4213562373
----------------------------------------------
The value of f at x=1 due to first order approximation : 0.551333569463
% relative error : -10.2667138927
----------------------------------------------
The value of f at x=1 due to second order approximation : 0.534175415889
% relative error : -6.83508317772
----------------------------------------------
The value of f at x=1 due to third order approximation : 0.535435376789
% relative error : -7.08707535775
----------------------------------------------
The value of f at x=1 due to fourth order approximation : 0.535504768061
% relative error : -7.10095361216
----------------------------------------------
The value of f at x=1 due to fifth order approximation : 0.535501917392
% relative error : -7.10038347839
----------------------------------------------
The value of f at x=1 due to sixth order approximation : 0.535501819651
% relative error : -7.10036393016
----------------------------------------------


## Example 4.3 : Page No:85¶

In :
from math import pi,cos,factorial
m=input("Input value of m:")
h=input("Input value of h:")
def f(x):
y=x**m
return y
x1=1#
x2=x1+h#
fx1=f(x1)#
fx2=fx1+m*(fx1**(m-1))*h#
if m==1:
R=0#
elif m==2 :
R=2*(h**2)/factorial(2)#

elif m==3:
R=(6*(x1)*(h**2)/factorial(2))+(6*(h**3)/factorial(3))#

elif m==4:
R=(12*(x1**2)*(h**2)/factorial(2))+(24*(x1)*(h**3)/factorial(3))+(24*(h**4)/factorial(4))

print "\nRemainder:",fx2,"\nThe value by first order approximation:",R
print "True Value at x2:",f(x2)

Input value of m:4
Input value of h:5

Remainder: 21
The value by first order approximation: 1275
True Value at x2: 1296


## Example 4.4: Page No:92¶

In :
from scipy.misc import derivative
def f(x):
y=-0.1*(x**4)-0.15*(x**3)-0.5*(x**2)-0.25*(x)+1.2
return y
x=0.5#
h=input("Input h:")
x1=x-h#
x2=x+h#
#forward difference method
fdx1=(f(x2)-f(x))/h##derivative at x
et1=abs((fdx1-derivative(f,x))/derivative(f,x))*100#
#backward difference method
fdx2=(f(x)-f(x1))/h##derivative at x
et2=abs((fdx2-derivative(f,x))/derivative(f,x))*100#
#central difference method
fdx3=(f(x2)-f(x1))/(2*h)##derivative at x
et3=abs((fdx3-derivative(f,x))/derivative(f,x))*100#
print "For h=",h
print "and percent error=",fdx1,"Derivative at x by forward difference method=",et1
print "and percent error=",fdx2,"Derivative at x by backward difference method=",et2
print "and percent error=",fdx3,"Derivative at x by central difference method=",et3

Input h:1.232323
For h= 1.232323
and percent error= -2.70944264922 Derivative at x by forward difference method= 114.60931875
and percent error= -0.178591334206 Derivative at x by backward difference method= 85.854151746
and percent error= -1.44401699172 Derivative at x by central difference method= 14.3775835022


## Example 4.5: Page No: 95¶

In :
from scipy.misc import derivative
def f(x):
y=x**3
return y
x=2.5#
delta=0.01#
deltafx=abs(derivative(f,x))*delta#
fx=f(x)#
print "true value is between : ",fx-deltafx,"and",fx+deltafx

true value is between :  15.4275 and 15.8225


## Example 4.6: Page No: 96¶

In :
from scipy.misc import derivative
def f(F,L,E,I):
y=(F*(L**4))/(8*E*I)
return y
Fbar=50##lb/ft
Lbar=30##ft
Ebar=1.5*(10**8)##lb/ft**2
Ibar=0.06##ft**4
deltaF=2##lb/ft
deltaL=0.1##ft
deltaE=0.01*(10**8)##lb/ft**2
deltaI=0.0006##ft**4
ybar=(Fbar*(Lbar**4))/(8*Ebar*Ibar)#
def f1(F):
y=(F*(Lbar**4))/(8*Ebar*Ibar)
return y
def f2(L):
y=(Fbar*(L**4))/(8*Ebar*Ibar)
return y
def f3(E):
y=(Fbar*(Lbar**4))/(8*E*Ibar)
return y
def f4(I):
y=(Fbar*(Lbar**4))/(8*Ebar*I)
return y

deltay=abs(derivative(f1,Fbar))*deltaF+abs(derivative(f2,Lbar))*deltaL+abs(derivative(f3,Ebar))*deltaE+abs(derivative(f4,Ibar))*deltaI#

print "The value of y is between:",ybar-deltay,"and",ybar+deltay
ymin=((Fbar-deltaF)*((Lbar-deltaL)**4))/(8*(Ebar+deltaE)*(Ibar+deltaI))#
ymax=((Fbar+deltaF)*((Lbar+deltaL)**4))/(8*(Ebar-deltaE)*(Ibar-deltaI))#
print "ymin is calculated at lower extremes of F, L, E, I values as =",ymin
print "ymax is calculated at higher extremes of F, L, E, I values as =",ymax

The value of y is between: 0.528721343471 and 0.596278656529
ymin is calculated at lower extremes of F, L, E, I values as = 0.524066539965
ymax is calculated at higher extremes of F, L, E, I values as = 0.602846335915


## Example 4.7 : Page No:98¶

In :
from math import pi,tan
from scipy.misc import derivative
def f(x):
y=tan(x)
return y
x1bar=(pi/2)+0.1*(pi/2)#
x2bar=(pi/2)+0.01*(pi/2)#
#computing condition number for x1bar
condnum1=x1bar*derivative(f,x1bar)/f(x1bar)#
print "The condition number of function for x =",condnum1,"is :",x1bar
if abs(condnum1)>1:
print "Function is ill-conditioned for x =",x1bar

#computing condition number for x2bar
condnum2=x2bar*derivative(f,x2bar)/f(x2bar)#
print "The condition number of function for x =",condnum2,"is :",x2bar
if abs(condnum2)>1:
print "Function is ill-conditioned for x =",x2bar

The condition number of function for x = 0.18201112073 is : 1.72787595947
The condition number of function for x = 0.0160083243793 is : 1.58650429006