# Chapter01:Errors in Numerical Calculations¶

## Ex1.1:pg-7¶

In [1]:
#example 1.1
#rounding off
#page 7
a1=1.6583
a2=30.0567
a3=0.859378
a4=3.14159
print "\nthe numbers after rounding to 4 significant figures are given below\n"
print "    %f       %.4g\n'" %(a1,a1)
print "    %f       %.4g\n" %(a2,a2)
print "    %f       %.4g\n" %(a3,a3)
print "    %f       %.4g\n" %(a4,a4)

the numbers after rounding to 4 significant figures are given below

1.658300       1.658
'
30.056700       30.06

0.859378       0.8594

3.141590       3.142



## Ex1.2:pg-9¶

In [8]:
#example 1.2
#percentage accuracy
#page 9
import math
x=0.51 # the number given
n=2 #correcting upto 2 decimal places
d=math.pow(10,-n)
d=d/2.0
p=(d/x)*100 #percentage accuracy
print "the percentage accuracy of %f after correcting to two decimal places is %f" %(x,p)

the percentage accuracy of 0.510000 after correcting to two decimal places is 0.980392


## Ex1.3:pg-9¶

In [10]:
#example 1.3
#absolute and relative errors
#page 9
X=3.1428571 #approximate value of pi
T_X=3.1415926 # true value of pi
A_E=T_X-X #absolute error
R_E=A_E/T_X #relative error
print "Absolute Error = %0.7f \n Relative Error = %0.7f" %(A_E,R_E)

Absolute Error = -0.0012645
Relative Error = -0.0004025


## Ex1.4:pg-10¶

In [4]:
#example 1.4
#best approximation
#page 10
import math
X=1/3 #the actual number
X1=0.30
X2=0.33
X3=0.34
E1=abs(X-X1)
E2=abs(X-X2)
E3=abs(X-X3)
if E1<E2:
if E1<E3:
B_A=X1
elif E2<E1:
if E2<E3:
B_A=X2
elif E3<E2:
if E3<E1:
B_A=X3
print "the best approximation of 1/3 is %f" %(B_A)

the best approximation of 1/3 is 0.300000


## Ex1.5:pg-10¶

In [5]:
#relative error
#example 1.5
#page 10
import math
n=8.6 # the corrected number
N=1 #the no is rounded to one decimal places
E_A=math.pow(10,-N)/2
E_R=E_A/n
print "the relative error of the number is:%0.4f" %(E_R)

the relative error of the number is:0.0058


## Ex1.6:pg-10¶

In [6]:
#example 1.6
#absolute error and relative error
#page 10
import math
s=math.sqrt(3)+math.sqrt(5)+math.sqrt(7)  #the sum square root of 3,5,7
n=4
Ea=3*(math.pow(10,-n)/2)  #absolute error
R_E=Ea/s
print "the sum of square roots  is %0.4g \n" %(s)
print "the absolute error is %f \n" %(Ea)
print "the relative error is %f" %(R_E)

the sum of square roots  is 6.614

the absolute error is 0.000150

the relative error is 0.000023


## Ex1.7:pg-10¶

In [8]:
#absolute error
#example 1.7
#page 10
import math
n=[0.1532, 15.45, 0.0000354, 305.1, 8.12, 143.3, 0.0212, 0.643, 0.1734]   #original numbers
#rounding all numbers to 2 decimal places
n=[305.1, 143.3, 0.15,15.45, 0.00, 8.12, 0.02, 0.64, 0.17]
sum=0;
#l=length(n);
for i in range(len(n)):
sum=sum+n[i];

E_A=2*math.pow(10,-1)/2+7*math.pow(10,-2)/2
print "the absolute error is:%0.2f" %(E_A)

the absolute error is:0.14


## Ex1.8:pg-11¶

In [1]:
#difference in 3 significant figures
#example 1.8
#page 11
import math
X1=math.sqrt(6.37)
X2=math.sqrt(6.36)
d=X1-X2  #difference between two numbers
print "the difference corrected to 3 significant figures is %0.3g" %(d)

the difference corrected to 3 significant figures is 0.00198


## Ex1.9:pg-12¶

In [13]:
#relative error
#example 1.10
#page 12
import math
a=6.54
b=48.64
c=13.5
da=0.01
db=0.02
dc=0.03
s=math.pow(a,2)*math.sqrt(b)/math.pow(c,3)
print "s=%f" %(s)
r_err=2*(da/a)+(db/b)/2+3*(dc/c)
print "the relative error is :%f" %(r_err)

s=0.121241
the relative error is :0.009930


## Ex1.11:pg-13¶

In [19]:
#relative error
#example 1.11
#page 13
import math
x=1
y=1
z=1
u=(5*x*math.pow(y,3))/math.pow(z,3)
dx=0.001
dy=0.001
dz=0.001
max=((5*math.pow(y,2))/math.pow(z,3))*dx+((10*x*y)/math.pow(z,3))*dy+((15*x*math.pow(y,2))/math.pow(z,4))*dz
e=max/u
print " the relative error is :%f" %(e)

 the relative error is :0.006000


## Ex1.12:pg-12¶

In [7]:
#taylor series
#example 1.12
#page 12
import math
def f(x):
return math.pow(x,3)+5*x-10
def f1(x):
return 3*math.pow(x,2)-6*x+5
def f2(x):
return 6*x-6
def f3(x):
return 6
D=[0,f(0), f1(0), f2(0), f3(0)]
S1=0;
h=1;
for i in range(1,5):
S1=S1+math.pow(h,i-1)*D[i]/math.factorial(i-1)

print "the third order taylors series approximation of f(1) is :%d" %(S1)

the third order taylors series approximation of f(1) is :-7


## Ex1.13:pg-16¶

In [32]:
#taylor series
#example 1.13
#page 16
import math
def f(x):
return math.sin(x)
def f1(x):
return math.cos(x)
def f2(x):
return -1*math.sin(x)
def f3(x):
return -1*math.cos(x)
def f4(x):
return math.sin(x)
def f5(x):
return math.cos(x)
def f6(x):
return -1*math.sin(x)
def f7(x):
return -1*math.cos(x)
D=[0,f(math.pi/6), f1(math.pi/6), f2(math.pi/6), f3(math.pi/6), f4(math.pi/6), f5(math.pi/6), f6(math.pi/6), f7(math.pi/6)]
S1=0
h=math.pi/6
print "order of approximation  computed value of sin(pi/3)   absolute eror\n\n"
for j in range(1,10):
for i in range(1,j):
S1=S1+math.pow(h,i-1)*D[i]/math.factorial(i-1)
print "%d                       %0.9f                        %0.9f\n" %(j,S1,abs(math.sin(math.pi/3)-S1))
S1=0

order of approximation  computed value of sin(pi/3)   absolute eror

1                       0.000000000                        0.866025404

2                       0.500000000                        0.366025404

3                       0.953449841                        0.087424437

4                       0.884910922                        0.018885518

5                       0.864191614                        0.001833790

6                       0.865757475                        0.000267929

7                       0.866041490                        0.000016087

8                       0.866027181                        0.000001777

9                       0.866025327                        0.000000077



## Ex1.14:pg-18¶

In [1]:
#maclaurins expansion
#example 1.14
#page 18
import math
n=8  #correct to 8 decimal places
x=1
for i in range(1,50):
if x/math.factorial(i)<math.pow(10,-8)/2:
c=i
break
print "no. of terms needed to correct to 8 decimal places is : %d " %(c)

no. of terms needed to correct to 8 decimal places is : 2


## Ex1.15:pg-18¶

In [74]:
#series apprixamation
#example 1.15
#page 18
import math
x=.09090909  # 1/11 =.09090909
S1=0
for i in range(1,5,2):
S1=S1+math.pow(x,i)/i
print "value of log(1.2) is : %0.8f\n\n" %(2*S1)
c=0
for i in range(1,50):
if math.pow(.09090909,i)/i<2*math.pow(10,-7):
c=i
break
print "min no of terms needed to get value wuth same accuracy  is :%d" %(c)

value of log(1.2) is : 0.18231906

min no of terms needed to get value wuth same accuracy  is :6