Least Square Fit
from pylab import*
print 'enter number of data points'
n=int(input())
sumx=0
sumy=0
sumxx=0
sumxy=0
xpoints=[]
ypoints=[]
newy=[]
print 'enter x,y data points one by one'
for i in range (1,n+1):
x,y=input()
xpoints.append(x)
ypoints.append(y)
sumx=sumx+x
sumy=sumy+y
xx=x*x
sumxx=sumxx+xx
xy=x*y
sumxy=sumxy+xy
denom=n*sumxx-sumx*sumx
a=(n*sumxy-sumx*sumy)/float(denom)
b=(sumxx*sumy-sumx*sumxy)/float(denom)
print "the pair of points are"
for i in range(0,len(xpoints)):
print xpoints[i],ypoints[i]
newyvalue=a*xpoints[i]+b
newy.append(newyvalue)
print"the required straight line is y=",a,"x+",b
plot (xpoints,ypoints,'o')
plot(xpoints,newy,'r')
show()
Output:
enter number of data points
5
enter x,y data points one by one
0,1
1,5
2,10
3,22
4,38
the pair of points are
0 1
1 5
2 10
3 22
4 38
the required straight line is y= 9.1 x+ -3.0
from pylab import*
n=input('No. of data value(x,y) pairs: ')
print 'Enter %d(x,y) pairs one by one: '%n
xy=[input() for i in range(n)]
x=[xy[i][0] for i in range(n)]
y=[xy[i][1] for i in range(n)]
sumx=sum(x)
sumy=sum(y)
sumxsqr=sum([i*i for i in x])
sumxy=sum(x[i]*y[i] for i in range(n))
den=n*sumxsqr-sumx*sumx
m=(n*sumxy-sumx*sumy)/float(den)
c=(sumy*sumxsqr-sumx*sumxy)/float(den)
print '\nSlope(m)= ',m,'y-intercept(c)= ',c
dx=(max(x)-min(x))/float(n)
dy=(max(y)-min(y))/float(n)
axis ([min(x)-dx,max(x)+dx,min(y)-dy,max(y)+dy])
title('Linear least square fit')
xlabel=('xdata')
ylabel('ydata')
for i in range(n):
plot(x[i],y[i],'b*')
y1=[(m*x[i]+c) for i in range(n)]
plot(x,y1)
show()
output
for above same input
print 'enter number of data points'
n=int(input())
sumx=0
sumy=0
sumxx=0
sumxy=0
xpoints=[]
ypoints=[]
newy=[]
print 'enter x,y data points one by one'
for i in range (1,n+1):
x,y=input()
xpoints.append(x)
ypoints.append(y)
sumx=sumx+x
sumy=sumy+y
xx=x*x
sumxx=sumxx+xx
xy=x*y
sumxy=sumxy+xy
denom=n*sumxx-sumx*sumx
a=(n*sumxy-sumx*sumy)/float(denom)
b=(sumxx*sumy-sumx*sumxy)/float(denom)
print "the pair of points are"
for i in range(0,len(xpoints)):
print xpoints[i],ypoints[i]
newyvalue=a*xpoints[i]+b
newy.append(newyvalue)
print"the required straight line is y=",a,"x+",b
plot (xpoints,ypoints,'o')
plot(xpoints,newy,'r')
show()
Output:
enter number of data points
5
enter x,y data points one by one
0,1
1,5
2,10
3,22
4,38
the pair of points are
0 1
1 5
2 10
3 22
4 38
the required straight line is y= 9.1 x+ -3.0
from pylab import*
n=input('No. of data value(x,y) pairs: ')
print 'Enter %d(x,y) pairs one by one: '%n
xy=[input() for i in range(n)]
x=[xy[i][0] for i in range(n)]
y=[xy[i][1] for i in range(n)]
sumx=sum(x)
sumy=sum(y)
sumxsqr=sum([i*i for i in x])
sumxy=sum(x[i]*y[i] for i in range(n))
den=n*sumxsqr-sumx*sumx
m=(n*sumxy-sumx*sumy)/float(den)
c=(sumy*sumxsqr-sumx*sumxy)/float(den)
print '\nSlope(m)= ',m,'y-intercept(c)= ',c
dx=(max(x)-min(x))/float(n)
dy=(max(y)-min(y))/float(n)
axis ([min(x)-dx,max(x)+dx,min(y)-dy,max(y)+dy])
title('Linear least square fit')
xlabel=('xdata')
ylabel('ydata')
for i in range(n):
plot(x[i],y[i],'b*')
y1=[(m*x[i]+c) for i in range(n)]
plot(x,y1)
show()
output
for above same input
Comments
Post a Comment