Computer science Answers 9/10/2020

in #answers4 years ago

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:

1599626512199_image.png

The formula to calculate the intercept in part b is:

1599626610671_image.png

The calculation part of slope in part b is:

1599659899646_WhatsAppImage2020-09-09at7.26.49PM.png

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 58139.39
ETH 2454.15
USDT 1.00
SBD 2.36