# Program to determine the value of slope by fitting the supplied data with a theoritical
# function y=mx+c
f1=open("lin1.d","r")
f2=open("least.d","w")
b=0
a=0
q1=0
q2=0
y1=[]
x=[]
y=[]
for k in f1:
z=k.split()
x.append(eval(z[0]))
y.append(eval(z[1]))
print("The entered values of the x's are:\n",x,"\nThe entered values of the y's are:\n",y)
n=len(x)
for i in range (0,n):
b=b+x[i]
a=a+x[i]*x[i]
q1=q1+x[i]*y[i]
q2=q2+y[i]
m=(q1*n-q2*b)/(a*n-b*b)
c=(a*q2-b*q1)/(a*n-b*b)
print("The value of the slope is:",m,"\nThe value of the c is:",c)
for i in range(0,n):
k=m*x[i]+c
y1.append(k)
print(x[i],y[i],y1[i],file=f2)
f2.close()
# function y=mx+c
f1=open("lin1.d","r")
f2=open("least.d","w")
b=0
a=0
q1=0
q2=0
y1=[]
x=[]
y=[]
for k in f1:
z=k.split()
x.append(eval(z[0]))
y.append(eval(z[1]))
print("The entered values of the x's are:\n",x,"\nThe entered values of the y's are:\n",y)
n=len(x)
for i in range (0,n):
b=b+x[i]
a=a+x[i]*x[i]
q1=q1+x[i]*y[i]
q2=q2+y[i]
m=(q1*n-q2*b)/(a*n-b*b)
c=(a*q2-b*q1)/(a*n-b*b)
print("The value of the slope is:",m,"\nThe value of the c is:",c)
for i in range(0,n):
k=m*x[i]+c
y1.append(k)
print(x[i],y[i],y1[i],file=f2)
f2.close()
Comments
Post a Comment