#example 4.1
#least square curve fitting procedure
#page 128
import math
from __future__ import division
x=[0,1, 2, 3, 4, 5]
x_2=[0,0,0,0,0,0]
x_y=[0,0,0,0,0,0]
y=[0,0.6, 2.4, 3.5, 4.8, 5.7]
for i in range(1,5):
x_2[i]=x[i]**2
x_y[i]=x[i]*y[i]
S_x=0
S_y=0
S_x2=0
S_xy=0
S1=0
S2=0
for i in range(1,5):
S_x=S_x+x[i]
S_y=S_y+y[i]
S_x2=S_x2+x_2[i]
S_xy=S_xy+x_y[i]
a1=(5*S_xy-S_x*S_y)/(5*S_x2-S_x**2)
a0=S_y/5-a1*S_x/5
print "x\t y\t x^2\t x*y\t (y-avg(S_y)) \t (y-a0-a1x)^2\n\n"
for i in range (1,6):
print "%d\t %0.2f\t %d\t %0.2f\t %0.2f\t %.4f\t\n" %(x[i],y[i],x_2[i],x_y[i],(y[i]-S_y/5)**2,(y[i]-a0-a1*x[i])**2)
S1=S1+(y[i]-S_y/5)**2
S2=S2+(y[i]-a0-a1*x[i])**2
print "---------------------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %0.2f\t %d\t %0.2f\t %0.2f\t %0.4f\t\n\n" %(S_x,S_y,S_x2,S_xy,S1,S2)
cc=math.sqrt((S1-S2)/S1) #correlation coefficient
print "the correlation coefficient is:%0.4f" %(cc)
#example 4.2
#least square curve fitting procedure
#page 129
from numpy import matrix
x=[0, 2, 5, 7]
y=[-1, 5, 12, 20]
x_2=[0,0,0,0]
xy=[0,0,0,0,]
for i in range (0,4):
x_2[i]=x[i]**2
xy[i]=x[i]*y[i]
print "x\t y\t x^2\t xy\t \n\n"
S_x=0
S_y=0
S_x2=0
S_xy=0
for i in range(0,4):
print "%d\t %d\t %d\t %d\t\n" %(x[i],y[i],x_2[i],xy[i])
S_x=S_x+x[i]
S_y=S_y+y[i]
S_x2=S_x2+x_2[i]
S_xy=S_xy+xy[i]
print "%d\t %d\t %d\t %d\t\n" %(S_x,S_y,S_x2,S_xy)
A=matrix([[4,S_x],[S_x,S_x2]])
B=matrix([[S_y],[S_xy]])
C=A.I*B
print "Best straight line fit Y=%.4f+x(%.4f)" %(C[0][0],C[1][0])
#example 4.3
#least square curve fitting procedure
#page 130
from numpy import matrix
x=[0, 1, 2, 4, 6]
y=[0, 1, 3, 2, 8]
z=[2, 4, 3, 16, 8]
x2=[0,0,0,0,0]
y2=[0,0,0,0,0]
z2=[0,0,0,0,0]
xy=[0,0,0,0,0]
yz=[0,0,0,0,0]
zx=[0,0,0,0,0]
for i in range(0,5):
x2[i]=x[i]**2
y2[i]=y[i]**2
z2[i]=z[i]**2
xy[i]=x[i]*y[i]
zx[i]=z[i]*x[i]
yz[i]=y[i]*z[i]
S_x=0
S_y=0
S_z=0
S_x2=0
S_y2=0
S_z2=0
S_xy=0
S_zx=0
S_yz=0
for i in range(0,5):
S_x=S_x+x[i]
S_y=S_y+y[i]
S_z=S_z+z[i]
S_x2=S_x2+x2[i]
S_y2=S_y2+y2[i]
S_z2=S_z2+z2[i]
S_xy=S_xy+xy[i]
S_zx=S_zx+zx[i]
S_yz=S_yz+yz[i]
print "x\t y\t z\t x^2\t xy\t zx\t y^2\t yz\n\n"
for i in range(0,5):
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n" %(x[i],y[i],z[i],x2[i],xy[i],zx[i],y2[i],yz[i])
print "-------------------------------- --------------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n\n" %(S_x,S_y,S_z,S_x2,S_xy,S_zx,S_y2,S_yz)
A=matrix([[5,13,14],[13,57,63],[14,63,78]])
B=matrix([[33],[122],[109]])
C=A.I*B
print "solution of above equation is:a=%d b=%d c=%d" %(C[0][0],C[1][0],C[2][0])
#example 4.4
#linearization of non-linear law
#page 131
import math
x=[1, 3, 5, 7, 9]
Y=[0,0,0,0,0]
x2=[0,0,0,0,0]
xy=[0,0,0,0,0]
y=[2.473, 6.722, 18.274, 49.673, 135.026]
for i in range(0,5):
Y[i]=math.log(y[i])
x2[i]=x[i]**2
xy[i]=x[i]*Y[i]
S_x=0
S_y=0
S_x2=0
S_xy=0
print "X\t Y=lny\t X^2\t XY\n\n"
for i in range(0,5):
print "%d\t %0.3f\t %d\t %0.3f\n" %(x[i],Y[i],x2[i],xy[i])
S_x=S_x+x[i]
S_y=S_y+Y[i]
S_x2=S_x2+x2[i]
S_xy=S_xy+xy[i]
print "----------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %0.3f\t %d\t %0.3f\t\n\n" %(S_x,S_y,S_x2,S_xy)
A1=((S_x/5)*S_xy-S_x*S_y)/((S_x/5)*S_x2-S_x**2)
A0=(S_y/5)-A1*(S_x/5)
a=math.exp(A0)
print "y=%0.3fexp(%0.2fx)" %(a,A1)
#example 4.5
#linearization of non-linear law
#page 131
from __future__ import division
x=[3, 5, 8, 12]
X=[0,0,0,0]
Y=[0,0,0,0]
X2=[0,0,0,0]
XY=[0,0,0,0]
y=[7.148, 10.231, 13.509, 16.434]
for i in range(0,4):
X[i]=1/x[i]
Y[i]=1/y[i]
X2[i]=X[i]**2
XY[i]=X[i]*Y[i]
S_X=0
S_Y=0
S_X2=0
S_XY=0
print "X\t Y\t X^2\t XY\t\n\n"
for i in range(0,4):
print "%0.3f\t %0.3f\t %0.3f\t %0.3f\t\n" %(X[i],Y[i],X2[i],XY[i])
S_X=S_X+X[i]
S_Y=S_Y+Y[i]
S_X2=S_X2+X2[i]
S_XY=S_XY+XY[i]
print "----------------------------------------------------------------------------------------\n\n"
print "%0.3f\t %0.3f\t %0.3f\t %0.3f\n\n" %(S_X,S_Y,S_X2,S_XY)
A1=(4*S_XY-S_X*S_Y)/(4*S_X2-S_X**2)
Avg_X=S_X/4
Avg_Y=S_Y/4
A0=Avg_Y-A1*Avg_X
print "y=x/(%f+%f*x)" %(A1,A0)
#example 4.6
#curve fitting by polynomial
#page 134
from numpy import matrix
x=[0, 1, 2]
y=[1, 6, 17]
x2=[0,0,0]
x3=[0,0,0]
x4=[0,0,0]
xy=[0,0,0]
x2y=[0,0,0]
for i in range(0,3):
x2[i]=x[i]**2
x3[i]=x[i]**3
x4[i]=x[i]**4
xy[i]=x[i]*y[i]
x2y[i]=x2[i]*y[i]
print "x\t y\t x^2\t x^3\t x^4\t x*y\t x^2*y\t\n\n"
S_x=0
S_y=0
S_x2=0
S_x3=0
S_x4=0
S_xy=0
S_x2y=0
for i in range(0,3):
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\n" %(x[i],y[i],x2[i],x3[i],x4[i],xy[i],x2y[i])
S_x=S_x+x[i]
S_y=S_y+y[i]
S_x2=S_x2+x2[i]
S_x3=S_x3+x3[i]
S_x4=S_x4+x4[i]
S_xy=S_xy+xy[i]
S_x2y=S_x2y+x2y[i]
print "--------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\n " %(S_x,S_y,S_x2,S_x3,S_x4,S_xy,S_x2y)
A=matrix([[3,S_x,S_x2],[S_x,S_x2,S_x3],[S_x2,S_x3,S_x4]])
B=matrix([[S_y],[S_xy],[S_x2y]])
C=A.I*B
print "a=%d b=%d c=%d \n\n" %(C[0][0],C[1][0],C[2][0])
print "exact polynomial :%d + %d*x +%d*x^2" %(C[0][0],C[1][0],C[2][0])
#example 4.7
#curve fitting by polynomial
#page 134
from numpy import matrix
x=[1, 3, 4, 6]
y=[0.63, 2.05, 4.08, 10.78]
x2=[0,0,0,0]
x3=[0,0,0,0]
x4=[0,0,0,0]
xy=[0,0,0,0]
x2y=[0,0,0,0]
for i in range(0,4):
x2[i]=x[i]**2
x3[i]=x[i]**3
x4[i]=x[i]**4
xy[i]=x[i]*y[i]
x2y[i]=x2[i]*y[i]
print "x\t y\t x^2\t x^3\t x^4\t x*y\t x^2*y\t\n\n"
S_x=0
S_y=0
S_x2=0
S_x3=0
S_x4=0
S_xy=0
S_x2y=0
for i in range(0,4):
print "%d\t %0.3f\t %d\t %d\t %d\t %0.3f\t %d\n" %(x[i],y[i],x2[i],x3[i],x4[i],xy[i],x2y[i])
S_x=S_x+x[i]
S_y=S_y+y[i]
S_x2=S_x2+x2[i]
S_x3=S_x3+x3[i]
S_x4=S_x4+x4[i]
S_xy=S_xy+xy[i]
S_x2y=S_x2y+x2y[i]
print "---------------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %0.3f\t %d\t %d\t %d\t %0.3f\t %0.3f\n " %(S_x,S_y,S_x2,S_x3,S_x4,S_xy,S_x2y)
A=matrix([[4,S_x,S_x2],[S_x,S_x2,S_x3],[S_x2,S_x3,S_x4]])
B=matrix([[S_y],[S_xy],[S_x2y]])
C=A.I*B
print "a=%0.2f b=%0.2f c=%0.2f \n\n" %(C[0][0],C[1][0],C[2][0])
print "exact polynomial :%0.2f + %0.2f*x +%0.2f*x^2" %(C[0][0],C[1][0],C[2][0])
#curve fitting by sum of exponentials
#example 4.8
#page 137
import math
import numpy
from numpy import matrix
x=[1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8]
y=[1.54, 1.67, 1.81, 1.97, 2.15, 2.35, 2.58, 2.83, 3.11]
y1=[0,0,0,0,0,0,0,0,0]
y2=[0,0,0,0,0,0,0,0,0]
s1=y[0]+y[4]-2*y[2]
h=x[1]-x[0]
I1=0
for i in range(0,3):
if i==0|i==2:
I1=I1+y[i]
elif i%2==0:
I1=I1+4*y[i]
elif i%2!=0:
I1=I1+2*y[i]
I1=(I1*h)/3
I2=0
for i in range(2,4):
if i==2|i==4:
I2=I2+y(i)
elif i%2==0:
I2=I2+4*y[i]
elif i%2!=0:
I2=I2+2*y[i]
I2=(I2*h)/3
for i in range(0,4):
y1[i]=(1.0-x[i])*y[i]
for i in range(4,8):
y2[i]=(1.4-x[i])*y[i]
I3=0
for i in range(0,2):
if i==0|i==2:
I3=I3+y1[i]
elif i%2==0:
I3=I3+4*y1[i]
elif i%2!=0:
I3=I3+2*y1[i]
I3=(I3*h)/3
I4=0;
for i in range (2,4):
if i==2|i==4:
I4=I4+y2[i]
elif i%2==0:
I4=I4+4*y2[i]
elif i%2!=0:
I4=I4+2*y2[i]
I4=(I4*h)/3
s2=y[4]+y[8]-2*y[6]
I5=0
for i in range(4,6):
if i==4|i==6:
I5=I5+y[i]
elif i%2==0:
I5=I5+4*y[i]
elif i%2!=0:
I5=I5+2*y[i]
I5=(I5*h)/3
I6=0
for i in range(6,8):
if i==6|i==8:
I6=I6+y[i]
elif i%2==0:
I6=I6+4*y[i]
elif i%2!=0:
I6=I6+2*y[i]
I6=(I6*h)/3
I7=0
for i in range(4,6):
if i==4|i==6:
I7=I7+y2[i]
elif i%2==0:
I7=I7+4*y2[i]
elif i%2!=0:
I7=I7+2*y2[i]
I7=(I7*h)/3
I8=0
for i in range(6,8):
if i==8|i==8:
I8=I8+y2[i]
elif i%2==0:
I8=I8+4*y2[i]
elif i%2!=0:
I8=I8+2*y2[i]
I8=(I8*h)/3
A=matrix([[1.81, 2.180],[2.88, 3.104]])
C=matrix([[2.10],[3.00]])
Z=A.I*C
p = numpy.poly1d([1,Z[0][0],Z[1][0]])
print "the unknown value of equation is 1 -1 "
#linear weighted least approx
#example 4.9
#page 139
from numpy import matrix
x=[0, 2, 5, 7]
y=[-1, 5, 12, 20]
w=10 #given weight 10
W=[1, 1, 10, 1]
Wx=[0,0,0,0]
Wx2=[0,0,0,0]
Wx3=[0,0,0,0]
Wy=[0,0,0,0]
Wxy=[0,0,0,0]
for i in range(0,4):
Wx[i]=W[i]*x[i]
Wx2[i]=W[i]*x[i]**2
Wx3[i]=W[i]*x[i]**3
Wy[i]=W[i]*y[i]
Wxy[i]=W[i]*x[i]*y[i]
S_x=0
S_y=0
S_W=0
S_Wx=0
S_Wx2=0
S_Wy=0
S_Wxy=0
for i in range(0,4):
S_x=S_x+x[i]
S_y=S_y+y[i]
S_W=S_W+W[i]
S_Wx=S_Wx+Wx[i]
S_Wx2=S_Wx2+Wx2[i]
S_Wy=S_Wy+Wy[i]
S_Wxy=S_Wxy+Wxy[i]
A=matrix([[S_W,S_Wx],[S_Wx,S_Wx2]])
C=matrix([[S_Wy],[S_Wxy]])
print "x\t y\t W\t Wx\t Wx^2\t Wy\t Wxy\t\n\n"
for i in range(0,4):
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t\n" %(x[i],y[i],W[i],Wx[i],Wx2[i],Wy[i],Wxy[i])
print "-------------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t\n" %(S_x,S_y,S_W,S_Wx,S_Wx2,S_Wy,S_Wxy)
X=A.I*C;
print "\n\nthe equation is y=%f+%fx" %(X[0][0],X[1][0])
#linear weighted least approx
#example 4.10
#page 139
from numpy import matrix
x=[0, 2, 5, 7]
y=[-1, 5, 12, 20]
w=100 #given weight 100
W=[1, 1, 100, 1]
Wx=[0,0,0,0]
Wx2=[0,0,0,0]
Wx3=[0,0,0,0]
Wy=[0,0,0,0]
Wxy=[0,0,0,0]
for i in range(0,4):
Wx[i]=W[i]*x[i]
Wx2[i]=W[i]*x[i]**2
Wx3[i]=W[i]*x[i]**3
Wy[i]=W[i]*y[i]
Wxy[i]=W[i]*x[i]*y[i]
S_x=0
S_y=0
S_W=0
S_Wx=0
S_Wx2=0
S_Wy=0
S_Wxy=0
for i in range(0,4):
S_x=S_x+x[i]
S_y=S_y+y[i]
S_W=S_W+W[i]
S_Wx=S_Wx+Wx[i]
S_Wx2=S_Wx2+Wx2[i]
S_Wy=S_Wy+Wy[i]
S_Wxy=S_Wxy+Wxy[i]
A=matrix([[S_W,S_Wx],[S_Wx,S_Wx2]])
C=matrix([[S_Wy],[S_Wxy]])
print "x\t y\t W\t Wx\t Wx^2\t Wy\t Wxy\t\n\n"
for i in range(0,4):
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t\n" %(x[i],y[i],W[i],Wx[i],Wx2[i],Wy[i],Wxy[i])
print "-------------------------------------------------------------------------------------------------------------------------------------\n\n"
print "%d\t %d\t %d\t %d\t %d\t %d\t %d\t\n" %(S_x,S_y,S_W,S_Wx,S_Wx2,S_Wy,S_Wxy)
X=A.I*C
print "\n\nthe equation is y=%f+%fx" %(X[0][0],X[1][0])
print "\n\nthe value of y(4) is %f" %(X[0][0]+X[1][0]*5)