I am having trouble typing the code in especially the function. Also I am using
ID: 3711514 • Letter: I
Question
I am having trouble typing the code in especially the function. Also I am using PYTHON as the program.
I will copy the Proj4linear.txt file as well as send a screen shot
0 4.835
0.04 4.92
0.1 5.143
0.12 5.227
0.22 5.591
0.24 5.638
0.26 5.704
0.32 5.906
0.34 6.023
0.36 6.026
0.42 6.262
0.44 6.331
0.46 6.399
0.52 6.614
0.58 6.86
0.6 6.872
0.62 6.98
0.64 7.025
0.7 7.3
0.72 7.323
0.8 7.631
0.82 7.654
0.84 7.77
0.9 7.91
1 8.256
1.08 8.556
1.1 8.615
1.12 8.715
1.14 8.777
1.16 8.873
1.22 9.106
1.24 9.119
1.34 9.476
1.48 9.958
1.5 10.032
1.52 10.083
1.54 10.171
1.58 10.337
1.6 10.366
1.62 10.468
1.7 10.751
1.72 10.8
1.74 10.845
1.76 10.955
1.8 11.051
1.88 11.361
1.9 11.433
1.92 11.475
1.98 11.768
2.04 11.973
2.2 12.536
2.22 12.57
2.24 12.665
2.3 12.891
2.32 12.917
2.38 13.09
2.42 13.223
2.48 13.49
2.5 13.512
2.6 13.91
2.62 13.996
2.64 14.062
2.7 14.271
2.76 14.457
2.82 14.688
2.84 14.773
2.9 14.967
2.92 15.034
2.98 15.26
3 15.34
3.18 15.915
3.22 16.034
3.28 16.305
3.3 16.339
3.32 16.4
3.38 16.66
3.4 16.655
3.42 16.763
3.44 16.877
3.5 17.045
Goals: Developing problem-solving skills, declaring variables, reading data from a file, using loops, and using lists and functions. can be manipulated to a format of a straight line, y - mx + b, where m is the slope of the line and b is the y intercept. However, because most measurements have some inherent error, the formula for the line may not be obvious. Therefore, linear regression may be used to determine the best fit of a line to the measured data. Linear regression uses the following two equations for calculations of slope and y-intercept. 2x*2y - nE(x *y) (2x) - n(x) Slope 2y - slope* 2x y-intercept In where n is the number of (x.y) data sets that were measured. The "goodness of fit" of this straight line can be estimated by summing the square of the residuals. The residual is defined as the difference between the measured y value and the calculated y value (using the calculated slope and y-intercept) for a given x. The equation is shown below where ym is the measured "goodness of fit',- (yin-ye)2 y value and ye is the calculated y value. A lower "goodness of fit" value indicates a better match of the line to the data For this assignment you write the PYTHON source code that uses a set of data to determine the slope and y-intercept of the line the best fits this data. The data is stored in a fileExplanation / Answer
Screenshot
--------------------------------------------------------------------------------------------------------------
Program
#iter through more than one list in one for loop
import itertools
#read file function to store values in lists
def readFile(aFile):
#declare empty lists
list_x = []
list_y = []
#open file
with open(aFile) as f:
for line in f:
if not line.strip():
continue
x,y = line.split()
#store values of x and y in seperate lists
list_x.append(float(x))
list_y.append(float(y))
#return lists
return list_x,list_y
#find goodness of fit using the given equations
def goodFit(list1,list2):
#variables for calculation
sumX=0
sumXX=0
sumY=0
goodFitValue=0
#length of list
n=len(list1)
#sum of all x values
for i in list1:
sumX+=i
sumXX+=(i**2)#sum of square of x values
#sum of all y values
for i in list2:
sumY+=i
#iterate through list elements
for i,j in itertools.zip_longest(list1,list2):
slope=float(((sumX*sumY)-(n*(i*j)))/((sumX**2)-(n*sumXX)))#find slope
yIntercept=(sumY-(slope*sumX))/n#find yIntercept
#iterate throug lists to generate goodfit
for x,Ym in itertools.zip_longest(list1,list2):
Yc=slope*x+yIntercept
val=Ym-Yc
goodFitValue+=(val**2)
return goodFitValue
#search the index of particular x value
def search(listxVal,xVal):
return listxVal.index(xVal)
#Main function definition
def main():
#pass file name to re0ad the function and return x value list and y value list
listX,listY=readFile("C:/Users/deept/Desktop/linear.txt")
#goodness of fit value
gooFitVal=goodFit(listX,listY)
#call search to get x value index
xIndex=search(listX,2.42)
#take corresponding y value
yVal=listY[xIndex]
#print y value
print('Y value corresponding to x=2.42 is:',yVal)
#main function
if __name__== "__main__":
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.