# 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]
print("Height FEV")
print('****** ******')
for i in range(len(height)):
print(f" {height[i]} {fev[i]}")
# 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
plt.scatter(height,fev)
plt.title("FEV vs Height")
plt.xlabel("Height (cm)")
plt.ylabel("FEV")
# 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)
n = len(height)-1
coeff = compute_int_coeff(n,height,fev)
x_eval = np.linspace(134,178,1000)
y = []
for i in range(0,len(x_eval)):
poly = eval_int_polynomial(n,height,x_eval[i],coeff)
y.append(poly)
print(y)
# 5. Plot the interpolating polynomial from a height of 134 cm to 178 cm
plt.plot(x_eval,y)
plt.plot(height,fev,"ro")
plt.title("Approximation of FEV")
# 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?
x = [165,175]
z = []
for i in range(len(x)):
poly = eval_int_polynomial(n,height,x[i],coeff)
print(f"Height: {x[i]} FEV: {poly}")
z.append(poly)
plt.plot(x_eval,y)
plt.plot(x,z,'ro')
plt.title("Polynomial interpolating")
plt.xlabel("Height (cm)")
plt.ylabel("FEV")
# I would say the 165 cm is a better prediction as it follows the linear trend height vs FEV but, for 175 cm in the
# approximation the polynomial starts to go down and then up between 171 cm and 178 cm
# Similarly with 134 cm to 138 cm the approximation polynomial seems to be inaccurate near the ends of the polynomial
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot piecewise linear interpolating polynomial and data; use
x_int = np.linspace(134,178)
y_int = np.interp(x_int,height,fev)
plt.plot(height,fev,'ro')
plt.plot(x_int,y_int)
plt.title("Piecewise linear polynomial interpolating")
plt.xlabel("Height (cm)")
plt.ylabel("FEV")
# 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
x_int_1 = np.linspace(165,176)
y_int_1 = np.interp(x_int_1,height,fev)
print(f"Height: {x_int_1[0]} FEV: {y_int_1[0]}")
print(f"Height: {x_int_1[-1]} FEV: {y_int_1[-1]}")
plt.plot(height,fev,'ro')
plt.plot(x_int,y_int)
plt.title("Piecewise linear polynomial interpolating")
plt.xlabel("Height (cm)")
plt.ylabel("FEV")
# For 165 cm it is only off by a little but for 176 cm it is off by over 1
# In this case the interpolation from piecewise is linear with height while in polynomial near the ends it drops or peaks