# 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]}")
Height FEV
****** ******
134 1.7
138 1.9
142 2.0
146 2.1
150 2.2
154 2.5
158 2.7
162 3.0
166 3.1
170 3.4
174 3.8
178 3.9
# 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)
[1.7, 1.831465566428686, 1.9549829183002327, 2.0708503387293575, 2.1793579507611, 2.280787888366062, 2.3754144646540842, 2.463504337343432, 2.545316671517766, 2.6211032997071486, 2.691108879325121, 2.7555710474967055, 2.814720573309918, 2.8687815075235092, 2.9179713297642373, 2.9625010932449336, 3.002575567036398, 3.0383933759238335, 3.0701471378797107, 3.098023599183919, 3.122203767221992, 3.1428630409917235, 3.16017133934848, 3.174293227018951, 3.185388038412925, 3.1936099992622475, 3.199108346116185, 3.202027443721514, 3.202506900316113, 3.200681680863857, 3.1966822182588936, 3.1906345225266275, 3.182660288048929, 3.1728769988403496, 3.1613980319022303, 3.148332758681194, 3.133786644657848, 3.117861347092247, 3.10065481095115, 3.082261363042885, 3.062771804384791, 3.042273500828005, 3.020850471964495, 2.9985834783403984, 2.975550106999986, 2.951824855383992, 2.927479213605866, 2.902581745129478, 2.877198165871281, 2.85139142174961, 2.825221764704197, 2.798746827207741, 2.7720216952920076, 2.7450989801104213, 2.7180288880583667, 2.6908592894732832, 2.663635785935059, 2.6364017761883365, 2.6091985207068658, 2.5820652049210278, 2.5550390011281485, 2.528155129106375, 2.5014469154513006, 2.474945851655367, 2.448681650949402, 2.4226823039252365, 2.396974132958706, 2.3715818454514492, 2.346528585910436, 2.321835986883075, 2.2975242187665836, 2.273612038509019, 2.2501168372199727, 2.227054686708504, 2.2044403849652623, 2.1822875006063893, 2.1606084162956067, 2.139414371161674, 2.118715502227311, 2.098520884866273, 2.078838572304394, 2.0596756341807376, 2.0410381941845777, 2.022931466783632, 2.005359793059169, 1.9883266756628482, 1.9718348129106953, 1.955886132028609, 1.9404818215644668, 1.9256223629808988, 1.91130756144337, 1.8975365758174145, 1.884307947889077, 1.8716196308223956, 1.8594690168673187, 1.8478529643317265, 1.8367678238305625, 1.8262094638254198, 1.8161732954672414, 1.8066542967551518, 1.7976470360237757, 1.7891456947716922, 1.7811440898431425, 1.7736356949751553, 1.7666136617221297, 1.7600708397695184, 1.7539997966484293, 1.7483928368625066, 1.7432420204385253, 1.7385391809118456, 1.7342759427577752, 1.7304437382797957, 1.7270338239652743, 1.724037296319434, 1.7214451071879036, 1.7192480785782367, 1.7174369169905763, 1.7160022272674709, 1.7149345259727957, 1.7142242543094566, 1.7138617905856282, 1.7138374622388683, 1.714141557427616, 1.714764336199186, 1.7156960412434286, 1.7169269082409557, 1.7184471758148443, 1.7202470950944642, 1.7223169389000619, 1.724647010556507, 1.7272276523445869, 1.7300492535980403, 1.7331022584544251, 1.7363771732677633, 1.7398645736909002, 1.743555111435166, 1.7474395207151319, 1.7515086243858347, 1.7557533397798946, 1.7601646842518126, 1.7647337804366154, 1.7694518612298797, 1.774310274496076, 1.779300487512085, 1.784414091152593, 1.7896428038240118, 1.794978475153384, 1.8004130894387598, 1.8059387688672517, 1.811547776507094, 1.8172325190796728, 1.82298554951765, 1.828799569315006, 1.8346674306748267, 1.8405821384605676, 1.84653685195636, 1.8525248864419375, 1.8585397145875202, 1.8645749676741037, 1.870624436644251, 1.8766820729886664, 1.8827419894735347, 1.8887984607135762, 1.894845923595793, 1.9008789775585693, 1.9068923847309656, 1.9128810699367131, 1.918840120567542, 1.9247647863302098, 1.9306504788717018, 1.9364927712867805, 1.9422873975122035, 1.9480302516116788, 1.9537173869556037, 1.9593450152996263, 1.9649095057658335, 1.9704073837304914, 1.9758353296219924, 1.9811901776327667, 1.9864689143486691, 1.9916686772994587, 1.9967867534337818, 2.001820577522014, 2.0067677304903695, 2.011625937689413, 2.016393067100267, 2.0210671274815146, 2.025646266459948, 2.03012876856806, 2.0345130532312434, 2.038797672707555, 2.0429813099827676, 2.047062776623533, 2.0510410105912333, 2.054915074019191, 2.058684150955728, 2.062347545075613, 2.0659046773622642, 2.0693550837631385, 2.072698412820568, 2.0759344232803256, 2.079062981680136, 2.082084059920216, 2.084997732818035, 2.087804175649229, 2.0905036616767445, 2.093096559670073, 2.0955833314165373, 2.097964529226384, 2.1002407934335356, 2.102412849893686, 2.1044815074814602, 2.1064476555882736, 2.108312261622458, 2.1100763685132446, 2.1117410922200626, 2.113307619248646, 2.1147772041753337, 2.1161511671809574, 2.117430891595624, 2.118617821455686, 2.119713459074148, 2.1207193626256906, 2.121637143747515, 2.1224684651570764, 2.123215038287842, 2.1238786209440748, 2.12446101497569, 2.1249640639741236, 2.1253896509901566, 2.125739696274605, 2.126016155042717, 2.1262210152631296, 2.12635629547214, 2.1264240426141083, 2.126426329908651, 2.126365254745384, 2.126242936606829, 2.1260615150201425, 2.125823147538251, 2.1255300077509736, 2.1251842833266745, 2.1247881740849315, 2.1243438901007354, 2.1238536498406595, 2.123319678331419, 2.122744205361237, 2.1221294637143573, 2.121477687439108, 2.120791110149767, 2.1200719633625793, 2.1193224748661805, 2.118544867126651, 2.117741355727474, 2.1169141478445406, 2.1160654407564263, 2.1151974203900723, 2.1143122599019963, 2.1134121182951766, 2.1124991390716716, 2.1115754489210548, 2.110643156444723, 2.109704350916105, 2.1087611010767757, 2.107815453968498, 2.10686943380111, 2.1059250408562957, 2.104984250427111, 2.1040490117932147, 2.1031212472317318, 2.1022028510635957, 2.101295688735291, 2.10040159593582, 2.099522377748763, 2.098659807839249, 2.0978156276756557, 2.0969915457858543, 2.096189237047758, 2.095410342013985, 2.0946564662703855, 2.0939291798281685, 2.093230016549389, 2.0925604736055248, 2.091922010968831, 2.091316050936215, 2.0907439776852974, 2.0902071368623605, 2.0897068352018517, 2.0892443401771077, 2.0888208796819634, 2.0884376417428747, 2.08809577426122, 2.0877963847853813, 2.0875405403122542, 2.0873292671177808, 2.0871635506161312, 2.0870443352471097, 2.0869725243914146, 2.086948980313293, 2.0869745241302216, 2.0870499358091448, 2.08717595418887, 2.08735327702817, 2.0875825610791483, 2.0878644221854397, 2.088199435404773, 2.08858813515546, 2.0890310153863454, 2.089528529769744, 2.090081091916926, 2.090689075615659, 2.091352815089337, 2.092072605277241, 2.092848702135418, 2.093681322957746, 2.0945706467166585, 2.095516814423073, 2.096519929505032, 2.0975800582045627, 2.098697229992281, 2.099871437999235, 2.101102639465518, 2.10239075620514, 2.103735675086691, 2.105137248529277, 2.1065952950132756, 2.10810959960538, 2.1096799144974714, 2.1113059595588255, 2.1129874229011527, 2.1147239614560003, 2.1165152015640105, 2.1183607395755835, 2.1202601424624197, 2.122212948439493, 2.1242186675969648, 2.126276782541547, 2.1283867490468653, 2.130547996712326, 2.132759929630028, 2.135021927059245, 2.1373333441080207, 2.1396935124214025, 2.1421017408758622, 2.1445573162794496, 2.147059504077222, 2.149607549061482, 2.1522006760864274, 2.1548380907867015, 2.1575189802994674, 2.160242513989541, 2.163007844177137, 2.16581410686785, 2.1686604224843906, 2.1715458965997136, 2.1744696206710596, 2.1774306727745865, 2.1804281183400938, 2.1834610108855066, 2.186528392750712, 2.1896292958303136, 2.192762742304999, 2.1959277453710504, 2.199123309967705, 2.202348433501936, 2.2056021065703306, 2.20888331367767, 2.2121910339518993, 2.215524241855108, 2.2188819078901783, 2.2222629993028113, 2.2256664807785165, 2.2290913151343443, 2.232536464004935, 2.236000888522673, 2.239483549991551, 2.242983410554518, 2.246499433853953, 2.250030585685028, 2.2535758346416497, 2.2571341527546913, 2.260704516122304, 2.2642859055319593, 2.267877307074042, 2.271477712746687, 2.2750861210516744, 2.2787015375810644, 2.282322975594426, 2.285949456586374, 2.2895800108442224, 2.293213677995547, 2.2968495075454154, 2.3004865594031507, 2.304123904398358, 2.307760624786102, 2.3113958147409983, 2.3150285808400795, 2.3186580425342798, 2.322283332608318, 2.3259035976289155, 2.3295179983811116, 2.3331257102926344, 2.336725923846082, 2.3403178449788977, 2.3439006954709347, 2.3474737133195647, 2.351036153102149, 2.3545872863258515, 2.3581264017646557, 2.3616528057834794, 2.3651658226493733, 2.36866479482966, 2.3721490832769927, 2.3756180677012346, 2.3790711468281627, 2.382507738644865, 2.3859272806318788, 2.3893292299819504, 2.3927130638054606, 2.3960782793224427, 2.399424394041193, 2.4027509459234717, 2.406057493536275, 2.4093436161901973, 2.412608914064353, 2.415853008317951, 2.4190755411884277, 2.422276176076284, 2.4254545976165613, 2.4286105117370522, 2.4317436457032735, 2.434853748150228, 2.4379405891010615, 2.4410039599726066, 2.4440436735679505, 2.4470595640560395, 2.450051486938432, 2.453019319003286, 2.455962958266613, 2.4588823239009967, 2.461777356151741, 2.464648016240688, 2.4674942862576987, 2.4703161690399837, 2.4731136880393767, 2.475886887177668, 2.4786358306901404, 2.4813606029574276, 2.4840613083258605, 2.486738070916387, 2.489391034422292, 2.4920203618957864, 2.4946262355236883, 2.4972088563923114, 2.499768444241761, 2.502305237209765, 2.5048194915652546, 2.507311481431847, 2.509781498501404, 2.512229851737897, 2.514656867071687, 2.5170628870845038, 2.5194482706852406, 2.5218133927768203, 2.5241586439142965, 2.526484429954425, 2.528791171696898, 2.531079304517457, 2.5333492779931017, 2.5356015555196114, 2.537836613921609, 2.5400549430553774, 2.5422570454046762, 2.544443435669753, 2.5466146403498326, 2.548771197319273, 2.5509136553976486, 2.5530425739140004, 2.555158522265488, 2.557262079470685, 2.5593538337177715, 2.5614343819078833, 2.5635043291938215, 2.5655642885144436, 2.56761488012492, 2.5696567311231613, 2.5716904749726512, 2.5737167510219288, 2.5757362040210277, 2.577749483635062, 2.57975724395529, 2.5817601430078576, 2.583758842260546, 2.5857540061277087, 2.5877463014737594, 2.5897363971153737, 2.591724963322762, 2.593712671320205, 2.5957001927861723, 2.5976881993532586, 2.5996773621082134, 2.601668351092343, 2.603661834802519, 2.605658479693103, 2.6076589496790104, 2.6096639056402133, 2.6116740049279294, 2.6136899008727554, 2.615712242295038, 2.617741673017706, 2.619778831381857, 2.6218243497653435, 2.6238788541046314, 2.625942963420165, 2.6280172893455376, 2.630102435660672, 2.6321989978293123, 2.634307562541065, 2.636428707258206, 2.638562999767578, 2.640710997737749, 2.6428732482817305, 2.6450502875254687, 2.6472426401823808, 2.6494508191341297, 2.6516753250179455, 2.6539166458206385, 2.656175256479627, 2.6584516184911426, 2.6607461795258702, 2.663059373052257, 2.6653916179676718, 2.667743318237686, 2.670114862543651, 2.6725066239388022, 2.674918959513097, 2.677352210066998, 2.679806699794388, 2.682282735974834, 2.684780608675397, 2.6873005904621485, 2.6898429361216336, 2.692407882392417, 2.694995647706927, 2.697606431943747, 2.7002404161905527, 2.702897762517853, 2.705578613763683, 2.70828309332945, 2.7110113049870286, 2.7137633326973307, 2.7165392404404245, 2.719339072057401, 2.7221628511040827, 2.72501058071675, 2.727882243489969, 2.730777801366685, 2.7336971955406697, 2.736640346371453, 2.739607153311844, 2.742597494848132, 2.745611228453091, 2.7486481905518563, 2.7517081965007724, 2.754791040579295, 2.757896495995016, 2.761024314901894, 2.764174228431733, 2.767345946739021, 2.7705391590591035, 2.773753533779821, 2.776988718526585, 2.780244340260995, 2.7835200053929667, 2.7868152999064324, 2.790129789498627, 2.7934630197329726, 2.796814516205546, 2.800183784725175, 2.8035703115071224, 2.8069735633803408, 2.810392988008334, 2.8138280141235184, 2.8172780517751406, 2.820742492590636, 2.82422071005045, 2.827712059776241, 2.8312158798323908, 2.834731491040815, 2.8382581973089414, 2.8417952859708366, 2.8453420281413537, 2.8488976790832505, 2.8524614785871445, 2.8560326513642513, 2.8596104074517363, 2.8631939426306334, 2.8667824388561485, 2.870375064700256, 2.873970975806453, 2.877569315356493, 2.8811692145490087, 2.8847697930898066, 2.888370159693736, 2.8919694125978945, 2.8955666400860682, 2.8991609210241505, 2.9027513254064106, 2.9063369149124028, 2.9099167434742745, 2.9134898578543287, 2.9170552982325657, 2.9206120988040385, 2.9241592883857312, 2.9276958910327906, 2.9312209266638214, 2.934733411695012, 2.938232359682846, 2.941716781975126, 2.945185688370049, 2.948638087783051, 2.9520729889211563, 2.9554894009645203, 2.958886334254909, 2.962262800990774, 2.965617815928638, 2.9689503970905124, 2.972259566476943, 2.975544350785461, 2.9788037821340274, 2.9820368987891936, 2.985242745898591, 2.9884203762274435, 2.991568850898709, 2.994687240136526, 2.9977746240125707, 3.0008300931949816, 3.003852749699463, 3.0068417076421654, 3.0097960939940043, 3.012715049335977, 3.0155977286151145, 3.018443301900639, 3.021250955139945, 3.024019890913978, 3.026749329191591, 3.029438508082461, 3.0320866845881436, 3.034693135350843, 3.037257157399419, 3.039778068892259, 3.042255209856499, 3.044687942923219, 3.047075654058081, 3.0494177532870292, 3.051713675416547, 3.0539628807480157, 3.0561648557857093, 3.0583191139379773, 3.0604251962111007, 3.062482671895371, 3.064491139242926, 3.066450226136817, 3.0683595907508763, 3.0702189221998317, 3.072027941179267, 3.073786400594834, 3.0754940861803073, 3.077150817103949, 3.0787564465626667, 3.080310862363517, 3.081813987491997, 3.083265780666682, 3.0846662368796323, 3.0860153879221497, 3.087313302895303, 3.0885600887047735, 3.0897558905394766, 3.090900892333481, 3.0919953172107073, 3.093039427911895, 3.0940335272033583, 3.094977958266984, 3.0958731050710204, 3.0967193927211234, 3.0975172877911406, 3.0982672986331985, 3.0989699756665163, 3.0996259116445297, 3.100235741899755, 3.1008001445659645, 3.101319840777154, 3.101795594842821, 3.1022282143990867, 3.1026185505351522, 3.102967497894653, 3.103275994751411, 3.103545023059138, 3.1037756084745993, 3.103968820353826, 3.104125771720874, 3.104247619208718, 3.1043355629718223, 3.1043908465699563, 3.1044147568228277, 3.104408623635099, 3.104373819791391, 3.1043117607208464, 3.1042239042308415, 3.1041117502095, 3.1039768402965473, 3.1038207575221977, 3.1036451259136557, 3.103451610068896, 3.1032419146973558, 3.1030177841272053, 3.1027810017788586, 3.1025333896044067, 3.10227680749265, 3.1020131526394437, 3.101744358883051, 3.1014723960042407, 3.101199268990843, 3.1009270172665264, 3.1006577138835394, 3.100393464679209, 3.100136407395949, 3.0998887107646036, 3.0996525735509226, 3.099430223565005, 3.099223916633539, 3.0990359355347183, 3.0988685888956655, 3.0987242100523096, 3.0986051558715637, 3.098513805535766, 3.0984525592893055, 3.0984238371473833, 3.098430077566908, 3.0984737360794927, 3.0985572838865747, 3.0986832064167107, 3.0988540018450528, 3.0990721795751304, 3.0993402586829952, 3.0996607663238502, 3.100036236101311, 3.1004692063994486, 3.100962218677784, 3.101517815729468, 3.102138539902832, 3.1028269312866064, 3.1035855258590384, 3.1044168536012524, 3.105323436575148, 3.1063077869661977, 3.1073724050915223, 3.108519777373674, 3.109752374280506, 3.111072648231655, 3.112483031472081, 3.113985933913198, 3.1155837409421534, 3.117278811199824, 3.1190734743281476, 3.120970028687424, 3.1229707390442583, 3.1250778342308716, 3.1272935047764863, 3.1296199005115866, 3.1320591281458494, 3.134613248820574, 3.137284275636531, 3.140074171158067, 3.1429848448945057, 3.1460181507597684, 3.1491758845112305, 3.1524597811689636, 3.1558715124163106, 3.159412683983137, 3.163084833012719, 3.1668894254136726, 3.1708278531980985, 3.174901431807235, 3.17911139742603, 3.1834589042879635, 3.1879450219716006, 3.192570732690319, 3.1973369285767657, 3.202244408963581, 3.2072938776620687, 3.212485940240385, 3.2178211013030547, 3.2232997617735233, 3.2289222161815654, 3.2346886499574086, 3.240599136734501, 3.2466536356629025, 3.2528519887352343, 3.2591939181273877, 3.2656790235559696, 3.2723067796548233, 3.2790765333726806, 3.285987501394402, 3.293038767588053, 3.3002292804802136, 3.307557850762084, 3.315023148828756, 3.3226237023544005, 3.330357893905854, 3.3382239585974247, 3.3462199817896416, 3.3543438968347625, 3.3625934828719273, 3.3709663626749764, 3.37946000055591, 3.3880717003270227, 3.396798603325023, 3.405637686500176, 3.4145857605739445, 3.423639468268322, 3.432795282610417, 3.4420495053157674, 3.4513982652538884, 3.460837516999796, 3.4703630394751817, 3.4799704346830898, 3.489655126539843, 3.4994123598083777, 3.50923719913678, 3.5191245282063583, 3.5290690489932346, 3.5390652811478867, 3.549107561496916, 3.5591900436714385, 3.569306697866679, 3.5794513107373556, 3.5896174854335157, 3.5997986417815624, 3.609988016615462, 3.6201786642628484, 3.630363457191337, 3.6405350868198987, 3.650686064500717, 3.6608087226767445, 3.6708952162203037, 3.6809375239584003, 3.6909274503900775, 3.7008566276018033, 3.7107165173863708, 3.7204984135714168, 3.730193444563426, 3.7397925761133095, 3.7492866143097556, 3.758666208806627, 3.767921856290837, 3.777043904197042, 3.786022554675979, 3.794847868822898, 3.803509771173134, 3.8119980544715166, 3.8203023847228343, 3.8284123065303612, 3.836317248729777, 3.844006530325746, 3.8514693667387787, 3.85869487636984, 3.865672087490464, 3.8723899454662245, 3.8788373203214417, 3.8850030146532903, 3.890875771903398, 3.8964442849953054, 3.9016972053462196, 3.9066231522615746, 3.91121072272114, 3.915448501565459, 3.919325072091582, 3.922829027067111, 3.9259489801718463, 3.9286735778762543, 3.930991511766372, 3.932891531324586, 3.9343624571761557, 3.9353931948112937, 3.9359727487928313, 3.9360902374596085, 3.9357349081359185, 3.9348961528574113, 3.9335635246240495, 3.9317267541908407, 3.929375767407235, 3.9265007031161625, 3.9230919316239485, 3.9191400737523665, 3.914636020484308, 3.9095709532147587, 3.903936364618729, 3.8977240801482447, 3.8909262801703117, 3.883535522758258, 3.875544767148757, 3.8669473978771336, 3.8577372496037374, 3.847908632644213, 3.837456359216755, 3.8263757704195696, 3.8146627639520108, 3.802313822592811, 3.7893260434493428, 3.775697167991615, 3.7614256128853008, 3.7465105016378835, 3.7309516970724252, 3.7147498346436905, 3.6979063566111616, 3.6804235470842612, 3.6623045679545916, 3.643553495730883, 3.6241753592919257, 3.6041761785733026, 3.583563004203875, 3.562343958108123, 3.540528275090537, 3.5181263454185783, 3.495149758421098, 3.4716113471187224, 3.447525233903701, 3.4229068772860924, 3.3977731197241594, 3.3721422365563716, 3.346033986052989, 3.319469660605432, 3.2924721390716662, 3.265065940296047, 3.2372772778224377, 3.2091341158196602, 3.1806662262381002, 3.151905247217313, 3.1228847427636865, 3.0936402637185396, 3.0642094100362156, 3.0346318943926507, 3.004949607145001, 2.9752066826628085, 2.9454495670518015, 2.915727087291438, 2.8860905218077133, 2.8565936725025476, 2.827292938262254, 2.7982473899663747, 2.7695188470199747, 2.741171955431602, 2.7132742674596857, 2.6858963228509376, 2.659111731693244, 2.6329972589077686, 2.6076329104028058, 2.5831020209146085, 2.5594913435588014, 2.5368911411171515, 2.5153952790847907, 2.4951013205026182, 2.476110622600503, 2.458528435276869, 2.442464001440503, 2.428030659240683, 2.4153459462125184, 2.404531705363375, 2.3957141932282493, 2.389024189920665, 2.384597111207103, 2.3825731226324436, 2.3830972557246803, 2.3863195263072408, 2.392395054947602, 2.401484189571071, 2.413752630269041, 2.429371556331185, 2.448517755531581, 2.4713737556985302, 2.4981279585988587, 2.528974776167239, 2.5641147691114443, 2.603754787925159, 2.6481081163395017, 2.6973946172458434, 2.751840881121321, 2.811680376990356, 2.8771536059548017, 2.948508257325593, 3.0259993673896934, 3.109889480846183, 3.200448814945771, 3.2979554263673236, 3.402695380867748, 3.5149629257385033, 3.6350606651061628, 3.763299738110799, 3.9]
# 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
Height: 165 FEV: 3.1044129974208774
Height: 175 FEV: 3.3490242131985744
# 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
Height: 165.0 FEV: 3.075
Height: 176.0 FEV: 3.8499999999999996