Mahcad Lecure #8 In-class Workshee Curve Fiing and Inerpolaion A he end of his lecure, you will be able o: explain he difference beween curve fiing and inerpolaion decide wheher curve fiing or inerpolaion should be used for a paricular applicaion inerpolae values beween daa poins using linerp and inerp wih cspline. fi equaions using line, regress, linfi, and genfi.. Inerpolaion and Curve Fiing Defined Background Thermophysical and ranspor propery daa are ofen needed in engineering calculaions. Quaniies such as he capaciies, hermal conduciviies, diffusiviies, densiies, and viscosiies are measured experimenally and he resuls are found in handbooks and journal aricles. Usually, he daa are no published for every condiion ha may be experienced in real life problems. For example, vapor pressures migh be published for a K and K, bu you may need he value a 5 K. In such siuaions, wo opions are available: inerpolaing and curve fiing. Inerpolaion is deermining a new poin beween wo exising poins. For example, if I measured he hea capaciy o be J/mol/K a K and J/mol/K a K, I migh reasonably esimae he hea capaciy a 5 K o be 5 J/mol/K. Curve Fiing is deermining a funcion ha closely maches he daa. For example, he daa may look like a sraigh line, so we can deermine he slope and inercep ha gives a line ha maches he daa he bes. The Anoine Equaion is a familiar funcion ha "fis" he emperaure dependence of vapor pressures well. A few noes:. Inerpolaion can really be hough of as a special case of curve fiing where he funcion is forced o pass hrough every daa poin.. Inerpolaion is generally done linearly or wih cubic splines. Cubic splines means a hird-order polynomial is generaed connecing he poins raher han a sraigh line.. Exrapolaion is defined as predicing values beyond he range of he available daa. For example, vapor pressures were measured a K and K, I could inerpolae o obain a value for a value beween K and K, bu I would be exrapolaing if I used he same poins o obain a value for 5 K or 5 K. Thus, he uncerainies are greaer for exrapolaed values compared o inerpolaed values.. You generally use curve fiing when you know a funcion ha maches he daa well. When you don' know a funcion, you inerpolae. (Funcions for fiing can come from simple observaion, experience, rial and error, or heory.. Inerpolaing Demonsraion Consider he following ime/emperaure measured in an experimen daa. I would like o know he values of he emperaure a all imes, no jus he whole minues. Time 5 7 8 9 min Temp 98 5 5 8 7 K
Linear Inerpolaion Linear inerpolaion is done by using he linerp funcion which has hree argumens. I akes he form: y new := linerp(xdaa,ydaa,x new ). For single values. emp new linerp( TimeTemp.5min) emp new 7.5 K Can creae funcions Temp_linerp( ) linerp( TimeTemp ) Temp_linerp(.5min) 7.5 K Key Poins:. The las argumen of linerp is he value of x a which you wan o esimae y.. The las argumen can be a variable if a funcion is defined. This allows you o use he inerpolaion over and over which is useful for graphing or inegraing daa. Cubic Spline Inerpolaion Insead of using a line o inerpolae beween daa poins, a cubic polynomial may be used o connec he poins. This is done by using he inerp and cspline funcions. For single values. emp new inerp( cspline( TimeTemp) TimeTemp.5min) emp new 7.5 K Can creae funcions Temp_spline( ) inerp( cspline( TimeTemp) TimeTemp ) Temp_spline(.5min) 7.5 K Alernaively, you can firs calculae cspline and hen feed he resuls o inerp. vs cspline( TimeTemp) Temp_spline( ) inerp( vstimetemp ) Temp_spline(.5min) 7.5 K
Visualizaion Temp Temp_linerp() Temp_spline() 9 Time. Curve Fiing Background Mahcad has several uiliies o fi daa o curves. These are: line (or slope and inercep) regress linfi genfi Each uiliy can fi cerain ypes of equaions. line will reurn he slope and inercep of he linear relaionship ha bes fi he daa. regress is used o reurn he polynomial ha bes represens he daa. linfi may be used o fi daa o expressions which are linear combinaions of funcions. genfi may be used o fi daa o funcions of any ype. Each fiing mehod can fi he funcions of hose above i in he lis. For example, regress can do he same hing ha line can because a line is jus a s order polynomial. Since a line is also a linear combinaion of funcions, linfi can also be used. Genfi can be used in any case.. Fiing Daa o a Line Synax line( xdaaydaa) Demonsraion Deermine he bes fi line o he ime/emperaure daa used above.
Time 5 7 8 9 8 8 5 s Temp 5 7 8 9 98 5 5 8 7 K Use he line funcion o ge he slope and inercep. line Time Temp min K 9..7 Inercep Slope K K/min Quesion: Wha are he unis on he inercep and he slope? Key Poins:. The argumens o he line funcion mus be dimensionless.. You mus deermine he unis yourself. Alernae Soluion: Use slope and inercep funcions. slope( TimeTemp).7 K min inercep( TimeTemp) 9. K Key Poin: You don' need o remove he unis on he inpu daa if you use slope and inercep insead of line. Can also creae a funcion o use laer. Temp_linear( ) slope( TimeTemp) inercep( TimeTemp)
Visualizaion Temp Temp_linerp() Temp_spline() Temp_linear(). Fiing Daa o a Polynomial Synax 9 Time vs := regress(xdaa,ydaa,order_of_polynomial) inerp(vs,xdaa,ydaa,xnew) Demonsraion The hea capaciy of waer as a funcion of emperaures has been measured in a calorimeer. The daa are found in he marices below. Fi he daa o a fourh order polynomial. D 5 7 8 9 7.5 7.7 9.5 7.5.5 7.5.5 7.5 5.5 7.55 7.5 7.595 9.5 7.5.5 7.75.5 7.8 5.5 7.98 kmol mol Temp exp D K Cpexp D J kmolk
The regress funcion will give he coefficiens of each erm in he polynomial. vs regress Temp exp Cp exp K J molk vs 7.7.9 8.5. 5 9.7 9 Key Poin: The oupu of regress is a marix. The firs hree rows are NOT he coefficiens, hey are values needed if inerp is going o be used. The fourh row is he coefficien on he x erm (he consan) The fifh row is he coefficien on he x erm. The las row is he coefficien on he x n erm. Usually, you feed he oupu of regress o he inerp funcion. Temp exp Cp exp Cp regress ( ) inerpvs K J molk Cp regress ( 5) 75.58 Quesion: Wha are he unis on he emperaure and hea capaciy? Key Poin: Using regress wih inerp means you canno include unis in he funcion. Can creae your own funcion o use unis. Cp regress () vs vs vs K 5 vs K vs K 7 K J molk Cp regress ( 5K) 75.58 J molk
Check he Resuls 95 9 Cp exp 85 Cp regress () 8 75 5 Temp exp. Fiing Daa o a Linear Combinaion of Funcions Ofen, he expression we wish o use o fi he daa is no a polynomial bu a linear combinaion of funcions. A linear combinaion of funcions is one where each erm only has one fiing parameer. For example, y = A Bsin( x) Cexp( x) is a linear combinaion of funcions. Only one fiing parameer is found in each erm and hey are muliplying he erm. The following are NOT linear combinaions of funcions. y = A sin( Bx) Cexp( x) y = A Bsin( x C) Dexp x E In he firs expression, B is inside he sine funcion. Thus, he B dependence is no linear. In he second equaion, muliple fiing parameers appear in he las wo erms. Synax Fx ( ) = erm erm erm_n β = linfi( xdaaydaaf) The erms in he F(x) marix are he funcional forms of each erm wihou he coefficien. For example, if he funcion o fis is y = A Bsin( x) Cexp( x) erm would be, erm would be sin(x) and erm would be exp(x).
Demonsraion Experimenal daa for he liquid densiy of aceone is found in he marices below. Fi his daa o he following expression. T where Y A B C T T = T C T D C T E C.5 T T C The criical emperaure of aceone, Tc, is 58. K. D 8.5 5.79 98.5 5..97 5..5.9.5.75 T C 58. Temp exp D K ρexp D kmol m 5.79.7.5.5 7 5.5.7 8 55.7.5 9.5.8 Obain he coefficiens o he equaion using linfi. F( x) x T C x T C β linfi x T C x T C.5 Temp exp ρ exp K kmol m F β 5.7...88. Key Poin: Linfi does no like unis on is inpu daa so divide hem ou.
Creae a funcion wih he resuls of linfi..5 ρ() β β T C K β β T C K T C K β T C K kmol m Key Poin: Pu in he unis explicily when you define he funcion. Check he Resuls.. ρ exp ρ(). 8 5 Temp exp. Fiing Daa o an Expression of Any Form Using Genfi When he funcion o fi is no a line, polynomial, or linear combinaion, you mus use genfi. Key Poin: Only use genfi if you have o, genfi requires good guesses and i is easier o make errors wih genfi. Synax gxγ ( ) = yx ( ) δy( x) δγ δy( x) δγ δy( x) δγ n γ guess = guess A guess A guess An γ = genfi xdaaydaaγ guess g
Demonsraion Experimenal daa for he vapor pressure of aceone is conained in he marices below. Fi he daa o he Anoine Equaion: ln( Psa) = A B T C D.5 58.98.5 9.7 Temp exp D K Psaexp D Pa 5 7 8 9.5 797..5.58 5..8 59.8.7. 5.8 8..8 5..5 7.5.85 The Anoine Equaion mus be fi using genfi. The firs hing needed is he parial derivaives of he funcion wih respec o each parameer (consan). You can do he derivaives in your head or using Mahcad. RHS of Anoine Equaion a zero a one a wo Parial Derivaives of RHS of Anoine Equaion. d a zero da zero a one a wo d a zero da one a one a wo a wo Key Poin: If you use Mahcad o do he derivaives, you canno use [ noaion for he subscrips. d a zero da wo a one a wo a one a wo
Creae he g(x,a) marix. (Noe: his doesn' have o be called g.) ga ( ) a one a zero a wo a wo a one a wo Change he subscrips o [ noaion. g( a ) a a a a a a Key Poins:. Genfi requires a he consans be a marix variable. Use [ noaion.. One common misake is o inerchange he and he a order when defining g(,a). This mus no be done. Provide some guess values for a. 5 a g 5 Key Poin: You should have a guess value for each consan in your equaion. Use genfi o ge he correc consans. Temp exp a genfi ln K Psa exp Pa a g g a..997.7 Key Poins:. If genfi doesn' converge, ry a new guess value.. Noice ha he y daa is ln(psa) as required by he original equaion.. You canno have unis on he daa so divide hem ou.. The las argumen in genfi is he name of he marix conaining he parial derivaives. Do no pu g(,a); i is jus g.
Define he funcion wih he correc parameers. a Psa( ) expa K a Pa Psa( K). Pa Check he fi by ploing Key Poin: You mus always check o make sure he fi is good by ploing. If i doesn' fi well, change your guess and ensure you did no make a misake. 5 Psa exp Psa() 5 Temp exp Exra Pracice Fi he daa given in he marices o he righ o he following expression. yx ( ) = γ cos γ x γ x γ X 5 Y g( xγ ) γ cos γ x yx ( ) γ cos γ x cos γ x γ x γ γ xsin γ x γ γ x γ..5 γ g γ genfi XY γ. g g. γ.8..5.9
5 yxx ( ) Y xxx