#Greatest precision
a = '4.3201'
b = '4.32'
c = '4.320106'
na = len(a)-(a.index('.')+1)
print '\n %s has a precision of 10**-%d\n'%(a,na)
nb = len(b)-(b.index('.')+1)
print '\n %s has a precision of 10**-%d\n'%(b,nb)
nc = len(c)-c.index('.')-1
print '\n %s has a precision of 10**-%d\n'%(c,nc)
e = max(na,nb,nc)
if e ==na:
print '\n The number with highest precision is %s\n'%(a)
elif e == nb:
print '\n The number with highest precision is %s\n'%(b)
else:
print '\n The number with highest precision is %s\n'%(c)
#Accuracy of numbers
def sd(x):
nd = x.index('.') #position of point
num = [ord(c) for c in x] #str2code(x)
if (nd)==None and num(length(x)) == 0:
print 'Accuracy is not specified\n'
n = 0 #
else:
if num[0]>= 1 and (nd==None):
n = len(x)
elif num[1] >= 1 and not((nd==None)):
n = len(x) - 1
else:
for i in range(0,len(x)):
if num[i] >= 1 and num[i] <= 9:
break;
n = len(x)- i + 1
return n
a = '95.763'
na = sd(a)
print '%s has %d significant digits\n'%(a,na)
b = '0.008472'
nb = sd(b)
print '%s has %d significant digits.The leading or higher order zeros are only place holders\n'%(b,nb)
c = '0.0456000'
nc = sd(c)
print '%s has %d significant digits\n'%(c,nc)
d = '36.0'
nd = sd(d)
print '%s has %d significant digits\n'%(d,nd)
e = '3600.0'
sd(e)
f = '3600.00'
nf = sd(f)
print '%s has %d significant digits\n'%(f,nf)
from __future__ import division
from math import floor
from numpy import arange,zeros
a = 0.1
b = 0.4
afrac=[];bfrac=[];
for i in range(0,8):
afrac.append(floor(a*2))
a = a*2 - floor(a*2)
bfrac.append(floor(b*2))
b = b*2 - floor(b*2)
afrac_s = '0' + '.' +(str(afrac)) #string form binary equivalent of a i.e 0.1
bfrac_s = '0' + '.' +(str(bfrac))
print '\n 0.1_10 = %s \n 0.4_10 = %s \n '%( afrac_s , bfrac_s)
summ=zeros(8)
for j in arange(7,0,-1):
summ[j] = afrac[j] + bfrac[j]
if summ[j] > 1:
summ[j] = summ[j]-2
afrac[(j-1)] = afrac[(j-1)] + 1
summ_dec = 0
for k in arange(7,0,-1):
summ_dec = summ_dec + summ[k]
summ_dec = summ_dec*1.0/2
print 'sum =',summ_dec
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.'
#Rounding-Off
fx = 0.7526
E =3
gx = 0.835
d = E - (-1)
#Chopping Method
Approx_x = fx*10**E
Err = gx*10**(E-d)
print '\n Chooping Method : \n Approximate x = %.4f*10**%d \n Error = %.4f \n '%(fx,E,Err)
#Symmetric Method
if gx >= 0.5:
Err = (gx -1)*10**(-1)
Approx_x = (fx + 10**(-d))*10**E
else:
Approx_x = fx*10**E
Err = gx * 10**(E-d)
print '\n Symmetric Rounding :\n Approximate x = %.4f*10**%d \n Error = %.4f \n '%(fx + 10**(-d),E,Err)
from scipy.misc import factorial
#Truncation Error
x = 1.0/5
#When first three terms are used
Trunc_err = x**3/factorial(3) + x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)
print '\n a) When first three terms are used \n Truncation error = %.6E \n '%(Trunc_err)
#When four terms are used
Trunc_err = x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)
print '\n b) When first four terms are used \n Truncation error = %.6E \n '%(Trunc_err)
#When Five terms are used
Trunc_err = x**5/factorial(5) + x**6/factorial(6)
print '\n c) When first five terms are used \n Truncation error = %.6E \n '%(Trunc_err)
from scipy.misc import factorial
#Truncation Error
x = -1.0/5
#When first three terms are used
Trunc_err = x**3/factorial(3) + x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)
print '\n a) When first three terms are used \n Truncation error = %.6E \n '%(Trunc_err)
#When four terms are used
Trunc_err = x**4/factorial(4) + x**5/factorial(5) + x**6/factorial(6)
print '\n b) When first four terms are used \n Truncation error = %.6E \n '%(Trunc_err)
#When Five terms are used
Trunc_err = x**5/factorial(5) + x**6/factorial(6)
print '\n c) When first five terms are used \n Truncation error = %.6E \n '%(Trunc_err)
#Absolute and Relative Errors
h_bu_t = 2945 #
h_bu_a = 2950 #
h_be_t = 30 #
h_be_a = 35 #
e1 = abs(h_bu_t - h_bu_a)
e1_r = e1/h_bu_t
e2 = abs(h_be_t - h_be_a)
e2_r = e2/h_be_t
print '\n For Building : \n Absolute error, e1 = %d \n Relative error , e1_r = %.2f percent \n '%(e1,e1_r*100)
print '\n For Beam : \n Absolute error, e2 = %d \n Relative error , e2_r = %.2G percent \n '%(e2,e2_r*100)
from math import log10
#Machine Epsilon
def Q(p):
q = 1 + (p-1)*log10(2)
return q
p = 24
q = Q(p)
print 'q = %.1f \n We can say that the computer can store numbers with %d significant decimal digits \n '%(q,q)
#Propagation of Error
x = 0.1234*10**4
y = 0.1232*10**4
d = 4
er_x = 10**(-d + 1)/2
er_y = 10**(-d + 1)/2
ex = x*er_x
ey = y*er_y
ez = abs(ex) + abs(ey)
er_z = abs(ez)/abs(x-y)
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)
#Errors in Sequence of Computations
x_a = 2.35 #
y_a = 6.74 #
z_a = 3.45 #
ex = abs(x_a)*10**(-3+1)/2
ey = abs(y_a)*10**(-3+1)/2
ez = abs(z_a)*10**(-3+1)/2
exy = abs(x_a)*ey + abs(y_a)*ex
ew = abs(exy) + abs(ez)
print '\n ex = %.5f \n ey = %.5f \n ez = %.5f \n exy = %.5f \n ew = %.5f \n'%(ex,ey,ez,exy,ew)
from math import floor
#Addition of Chain of Numbers
x = 9678 #
y = 678 #
z = 78 #
d = 4 # #length of mantissa
fx = x/10**4
fy = y/10**4
fu = fx + fy
Eu = 4
if fu >= 1:
fu = fu/10
Eu = Eu + 1
#since length of mantissa is only four we need to maintain only four places in decimal, so
fu = floor(fu*10**4)/10**4
u = fu * 10**Eu
w = u + z
n = len(str(w))
w = floor(w/10**(n-4))*10**(n-4) #To maintain length of mantissa = 4
print 'w =',w
True_w = 10444
ew = True_w - w
er_w = (True_w - w)/True_w
print 'True w =',True_w
print 'ew =',ew
print 'er,w =',er_w
#Addition of chain Numbers
x = 9678 #
y = 678 #
z = 78 #
d = 4 # #length of mantissa
n = max(len( str(y) ) , len(str(z)))
fy = y/10**n
fz = z/10**n
fu = fy + fz
Eu = n
if fu >= 1:
fu = fu/10
Eu = Eu + 1
u = fu * 10**Eu
n = max(len( str(x) ) , len(str(u)))
fu = u/10**4
fx = x/10**4
fw = fu + fx
Ew = 4
if fw >= 1:
fw = fw/10
Ew = Ew + 1
#since length of mantissa is only four we need to maintain only four places in decimal, so
fw = floor(fw*10**4)/10**4
w = fw*10**Ew
print 'w =',w
True_w = 10444
ew = True_w - w
er_w = (True_w - w)/True_w
print 'True w =',True_w
print 'ew =',ew
print 'er,w =',er_w
from math import sqrt
from scipy.misc import derivative
#Absolute & Relative Errors
xa = 4.000
def f(x):
f = sqrt(x) + x
return f
#Assuming x is correct to 4 significant digits
ex = 0.5 * 10**(-4 + 1)
df_xa = derivative(f,4)
ef = ex * df_xa
er_f = ef/f(xa)
print '\n ex = %.0E \n df(xa) = %.2f \n ef = %.2E \n er,f = %.2E \n'%( ex,df_xa,ef,er_f)
#Error Evaluation
x = 3.00 #
y = 4.00 #
def f(x,y):
f = x**2 + y**2
return f
def df_x(x):
df_x = 2*x
return df_x
def df_y(y):
df_y = 2*y
return df_y
ex = 0.005
ey = 0.005
ef = df_x(x)*ex + df_y(y)*ey
print 'ef =',ef
#Condition and Stability
C1 = 7.00 #
C2 = 3.00 #
m1 = 2.00 #
m2 = 2.01 #
x = (C1 - C2)/(m2 - m1)
y = m1*((C1 - C2)/(m2 - m1)) + C1
print 'x =',x
print 'y =',y
print 'Changing m2 from 2.01 to 2.005'
m2 = 2.005
x = (C1 - C2)/(m2 - m1)
y = m1*((C1 - C2)/(m2 - m1)) + C1
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)
from math import sqrt, floor
#Difference of Square roots
x = 497.0 #
y = 496.0 #
sqrt_x = sqrt(497)
sqrt_y = sqrt(496)
nx = len( str( floor( sqrt_x ) ) )
ny = len( str( floor( sqrt_y ) ) )
sqrt_x = floor(sqrt_x*10**(4-nx))/10**(4-nx)
sqrt_y = floor(sqrt_y*10**(4-ny))/10**(4-ny)
z1 = sqrt_x - sqrt_y
print 'z = sqrt(x) - sqrt(y) =',z1
z2 = ( x -y)/(sqrt_x + sqrt_y)
if z2 < 0.1:
z2 = z2*10**4
nz = len(str(floor(z2)))
z2 = floor(z2*10**(4-nz))/10**(8-nz)
print 'z = ( x-y )/( sqrt(x) + sqrt(y) ) =',z2
from __future__ import division
from math import exp,floor
x = -10
T_act=[1]
T_trc=[1]
e_x_cal = 1
TE=[]
for i in range(0,100):
T_act.append(T_act[i]*x/(i+1))
T_trc.append(floor(T_act[i+1]*10**5)/10**5)
TE.append(abs(T_act[i+1]-T_trc[i+1]))
e_x_cal = e_x_cal + T_trc[i+1]
e_x_act = exp(-10)
Sum=0
for s in TE:
Sum+=s
print 'Truncation Error =',Sum
print 'calculated e**x using roundoff =',e_x_cal
print 'actual e**x = ',e_x_act
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'