Computer science Answers 9/10/2020
The code part a is:
def slope(x, y):
N = len(x) # store the length of list
xy = [] # storing x*y
x2 = [] # storing square of x
for i in range(N):
xy.append(x[i] * y[i])
x2.append(x[i] * x[i])
a = sum(xy) / sum(x2) # formula for calculation slope is summation of xy divided by summation of square of x
return a
x = [0, 1, 2]
y = [1, 1, 2]
print('The slope is: {}'.format(slope(x, y)))
The code for part b is:
def line(x, y):
N = len(x) # store the length of list
xy = [] # storing x*y
x2 = [] # storing square of x
for i in range(N):
xy.append(x[i] * y[i])
x2.append(x[i] * x[i])
a = (N * sum(xy) - sum(x) * sum(y)) / (N * sum(x2) - sum(x) * sum(x)) # formula for calculation slope
b = (sum(y) - a * sum(x)) / N # formula for calculating intercept
return a, b
x = [0, 1, 2]
y = [1, 1, 2]
a, b = line(x, y)
print('The slope is: {}\nThe intercept is: {}'.format(a, b))
The formula to calculate the slope in part b is:
The formula to calculate the intercept in part b is:
The calculation part of slope in part b is: