# 1. Print out data in columns
height = [134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178]
fev = [1.7, 1.9, 2., 2.1, 2.2, 2.5, 2.7, 3.,3.1, 3.4, 3.8, 3.9]
help

# 2. Import libraries - NumPy and matplotlib
import numpy as np
import matplotlib.pyplot as plt

# 3. Plot the data; add labels to axes and plot title
height = [134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178]
fev = [1.7, 1.9, 2., 2.1, 2.2, 2.5, 2.7, 3.,3.1, 3.4, 3.8, 3.9]
x = np.array (height)
y= np.array(fev)
plt.scatter (x,y)
plt.title ('FEV graph')
plt.xlabel('height')
plt.ylabel('scatter')
plt.plot (x, y, 'ro')

# 4. Input functions to find interpolating polynomial
def compute_int_coeff (n, x, y) :
coeff = [] # initialize list
for i in range (0,n+1): # loop to calculate n+1 coefficients
numerator = y[i]
denominator = 1. # because denominator is a product we must initialize to 1
for j in range (0,n+1):
if ( i != j) : # denominator is product of all terms except
# when x_i=x_j which would give 0
denominator = denominator * (x[i] - x[j])
coeff.append (numerator / denominator) # add term to list
return (coeff)
#
#
def eval_int_polynomial (n, x, x_eval, coeff) :
my_sum = 0. # formula is sum of n+1 terms so initialize
for i in range (0,n+1) : # loop to form sum of terms
product = 1. # initialize product in numerator of L_i
for j in range (0, n+1) : # loop to form product of terms in numerator
if ( i != j ) : # include all terms except when i = j
product = product * (x_eval - x[j])
# when this j loop is complete we have the numerator of L_i for one term
yi_times_Li = product * coeff[i] # multiplies numerator times C_i (y_i/ product)
my_sum = my_sum + yi_times_Li # keeps running tally of sum of terms
return (my_sum)
x=[134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178];
y=[1.7, 1.9, 2., 2.1, 2.2, 2.5, 2.7, 3.,3.1, 3.4, 3.8, 3.9]
n=2
coeff = compute_int_coeff (n, x, y)
print ("coefficients are ", coeff)
x=[134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178];
y=[1.7, 1.9, 2., 2.1, 2.2, 2.5, 2.7, 3.,3.1, 3.4, 3.8, 3.9]
n=3
coeff = compute_int_coeff(n,x,y)
x_eval = [2.,3.]
n_eval = 2
for i in range (0,n_eval) :
xx = x_eval[i]
poly_at_x = eval_int_polynomial (n, x, xx, coeff)
print (f" the polynomial evaluated at x={xx} is {poly_at_x}")

```
coefficients are [0.053125, -0.11875, 0.0625]
the polynomial evaluated at x=2.0 is -715.5000000000018
the polynomial evaluated at x=3.0 is -700.2757812500004
```

# 5. Plot the interpolating polynomial from a height of 134 cm to 178 cm
x_plt = np.linspace( 134,178,50 ) # create NumPy array of points to plot
y_plt=[]
for i in range (0,len(x_plt)):
xx = x_plt[i]
poly_at_x = eval_int_polynomial (n, x, xx, coeff)
y_plt.append (poly_at_x)
#
plt.plot(x_plt,y_plt)
plt.title("Polynomials")
xs=[134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178]; ys=[1.7, 1.9, 2., 2.1, 2.2, 2.5, 2.7, 3.,3.1, 3.4, 3.8, 3.9]
plt.plot(xs,ys,"ro")

# 6. Use the polynomial to predict the FEV for a height of 165 cm and of 175 cm.
# Which do you think is a better prediction?
#I think that the FEV is a better prediction.

# Import libraries
import matplotlib.pyplot as plt
import numpy as np

xplot = np.linspace(165,175); yplot = np.interp(xplot, x, y )
plt.plot(x,y,'ro'); plt.plot(xplot,yplot); plt.show()

# Use the piecewise polynomial to predict the FEV for a height of 165 cm and of 176 cm.
# How does this compare with 11th degree polynomial which predicted 3.104 for 165 cm and
# 2.697 for 176 cm
#this compares differently.