Lecture 22 - Interpolation and Curve Fitting

Lecture 22 - Interpolation and Curve Fitting

Lecture 22 - Interpolation and Curve Fitting Prayer/Spiritual Thought Announcements Outline 1. Interpolation 2. Linear Regression 3. Nonlinear Regression 1. Interpolation A. Explanation Interpolation is the process of using (x,y) data at discrete points to infer the values in between those discrete points. We talked about two different types of interpolation: 1. Linear interpolation -- lines connect each data point 2. Cubic Spline -- cubic polynomials smoothly connect each data point Interpolation methods are valuable when the noise in the data is "small." How small is small enough is a judgement call. B. Recipes Read data from a file using: READPRN("<filename>") Linear interpolation: 1. Read/Define your x, y data points 2. Define the x point(s) you want to interpolate at 3. Find the y points from: y := linterp(<xdata>, <ydata>, <x interpolation points>) Cubic Spline: 1. Read/Define your x, y data points 2. Define the x point(s) you want to interpolate at 3. Find the coefficients for the cubic spline by calling: coeffs := cspline(<xdata>, <ydata>) 4. Find the y points from: y := interp(<coeffs>, <xdata>, <ydata>, <x interp points>) C. Examples (i) Linear interpolation, read data from file xydata ≔ READPRN (“Lec_22-Ex_A.dat”) ⟨0⟩ ⟨1⟩ xdata ≔ xydata ydata ≔ xydata Tips: The header before the data (text before the columns of Non-Commercial Use Only numbers) can't have a number in it. READPRN starts reading when it finds a number, and having numbers in the header will mess it up. Get the columns of the xy_data matrix using the column operator (Ribbon -> Vector/ Matrix Operators -> Matrix Column or the shortcut: <ctrl><shift>C). ⟨0⟩ ⟨1⟩ xdata ≔ xydata ydata ≔ xydata Tips: ⎡ 0 ⎤ ⎡ 2.404 ⋅ 10-4 ⎤ The header before the data ⎢ 0.714 ⎥ ⎢ 0.002 ⎥ ⎢ ⎥ ⎢ ⎥ (text before the columns of ⎢ 1.429 ⎥ ⎢ 0.014 ⎥ numbers) can't have a number ⎢ 2.143 ⎥ ⎢ 0.066 ⎥ in it. READPRN starts reading ⎢ 2.857 ⎥ ⎢ 0.216 ⎥ when it finds a number, and ⎢ 3.571 ⎥ ⎢ 0.506 ⎥ having numbers in the header ⎢ ⎥ ⎢ ⎥ x = 4.286 y = 0.844 will mess it up. data ⎢ ⎥ data ⎢ ⎥ Get the columns of the xy_data ⎢ 5 ⎥ ⎢ 1 ⎥ matrix using the column ⎢ 5.714 ⎥ ⎢ 0.844 ⎥ operator (Ribbon -> Vector/ ⎢ 6.429 ⎥ ⎢ 0.506 ⎥ Matrix Operators -> Matrix ⎢ ⎥ ⎢ ⎥ 7.143 ⎢ 0.216 ⎥ Column or the shortcut: ⎢ ⎥ <ctrl><shift>C). ⎢ 7.857 ⎥ ⎢ 0.066 ⎥ ⎣ ⋮ ⎦ ⎣⎢ ⋮ ⎦⎥ (get the x points for the interpolation) i N ≔ 100 i ≔ 0 ‥ N beg ≔ 0 end ≔ 10 xinterp ≔ beg + (end - beg) ― i N yinterp ≔ linterp⎝⎛xdata , ydata , xinterp⎠⎞ 1 0.9 0.8 0.7 0.6 0.5 y 0.4 data 0.3 yinterp 0.2 0.1 0 0 1 2 3 4 5 6 7 8 9 10 xdata xinterp (ii) Cubic spline, data in vector Non-Commercial Use Only (ii) Cubic spline, data in vector i ≔ 0 ‥ 10 ⎛ 2 ⎞ 10 ⋅ i x2data x2data ≔ ―― y2data ≔ cos ⎜-―――⎟ i 10 ⎝ 8 ⎠ j ≔ 0 ‥ 100 10 ⋅ j x2interp ≔ ―― j 100 fcspline ≔ cspline⎝⎛x2data , y2data⎠⎞ y2cspline ≔ interp⎝⎛fcspline , x2data , y2data , x2interp⎠⎞ 1 8⋅10⁻¹ 6⋅10⁻¹ 4⋅10⁻¹ 2⋅10⁻¹ 0 0 1 2 3 4 5 6 7 8 9 10 y2 -2⋅10⁻¹ data -4⋅10⁻¹ y2cspline -6⋅10⁻¹ -8⋅10⁻¹ -1 x2data x2interp 2. Polynomial Regression A. Explanation Regression or curve fitting is a littleNon-Commercial bit different than Use the Only interpolations above. For a regression, we assume the data has some non-trivial noise. We did polynomial regression in Excel using "Linest" and in Python using "polyfit." In Mathcad we will use: "polyfit" and "polyfitc" To find the actual polynomial, use polyfit: <best fit polynomial function> = polyfit(<x_data>, <y_data>, <order of polynomial) To find the coefficients in the fit, use polyfitc: <matrix of coefficients> = polyfit(<x_data>, <y_data>, <order of polynomial) B. Example 2. Polynomial Regression A. Explanation Regression or curve fitting is a little bit different than the interpolations above. For a regression, we assume the data has some non-trivial noise. We did polynomial regression in Excel using "Linest" and in Python using "polyfit." In Mathcad we will use: "polyfit" and "polyfitc" To find the actual polynomial, use polyfit: <best fit polynomial function> = polyfit(<x_data>, <y_data>, <order of polynomial) To find the coefficients in the fit, use polyfitc: <matrix of coefficients> = polyfit(<x_data>, <y_data>, <order of polynomial) B. Example T T xex3_data ≔ [ 0 1 2 3 4 5 ] yex3_data ≔ [ 0 0.8 0.9 0.1 -0.8 -1 ] fpoly_regress ≔ polyfit⎝⎛xex3_data , yex3_data , 3⎠⎞ ⎡ “Term” “Coefficient” “Std Error” “95% CI Low” “95% CI High” “VIF” “T” “P” ⎤ ⎢ “Intercept” -0.04 0.138 -0.634 0.554 NaN -0.287 0.4 ⎥ ⎛ ⎞ ⎢ ⎥ polyfitc⎝xex3_data , yex3_data , 3⎠ = ⎢ “A” 1.693 0.268 0.539 2.847 63.407 6.315 0.012 ⎥ ⎢ “AA” -0.813 0.133 -1.387 -0.24 424.9 -6.105 0.013 ⎥ ⎣⎢ “AAA” 0.087 0.017 0.012 0.162 186.381 4.974 0.019 ⎦⎥ i i ≔ 0 ‥ 100 xplt ≔ 9 ⋅ ――- 1 yplt ≔ fpoly_regress ⎛xplt ⎞ i 100 i ⎝⎜ i⎠⎟ 6.3 5.4 4.5 3.6 2.7 1.8 y 0.9 ex3_data 0 -1 1 3 5 7 yplt -0.9 -1.8 -2.7 xex3_data xplt 3. Nonlinear Regression A. Explanation For a general nonlinear regressionNon-Commercial (i.e. not a polynomial) Use Only we must use a different fitting function. In Excel we minimized the sum of the squared error using solver, and in Python we used Scipy.optimize.curve_fit. In Mathcad we will use genfit. B. Recipe 1. Define the function you are trying to fit with the indepent variable first, and a vector of coefficients to fit next: f(x, b) := fit_function(<x dep variable>, <b array of coefficents>) 2. Make an initial guess for the coefficients. 3. Call genfit: <fit coefficients> = genfit(<x data>, <y data>, <coefficients guess>, <fit function>) 4. (if needed) Generate the x_fit, y_fit pairs using the function defined in (1) and the coefficients obtained in (3). C. Example - Fit the Antoine Equation to the given data. 3. Nonlinear Regression A. Explanation For a general nonlinear regression (i.e. not a polynomial) we must use a different fitting function. In Excel we minimized the sum of the squared error using solver, and in Python we used Scipy.optimize.curve_fit. In Mathcad we will use genfit. B. Recipe 1. Define the function you are trying to fit with the indepent variable first, and a vector of coefficients to fit next: f(x, b) := fit_function(<x dep variable>, <b array of coefficents>) 2. Make an initial guess for the coefficients. 3. Call genfit: <fit coefficients> = genfit(<x data>, <y data>, <coefficients guess>, <fit function>) 4. (if needed) Generate the x_fit, y_fit pairs using the function defined in (1) and the coefficients obtained in (3). C. Example - Fit the Antoine Equation to the given data. TPdata ≔ READPRN (“Lec_22-Ex_C.dat”) (Read in the data) ⟨0⟩ ⟨1⟩ ⎛ ⟨1⟩⎞ Tdata ≔ TPdata ⋅ K Pdata ≔ TPdata ⋅ Pa log_Pdata ≔ log ⎝TPdata ⎠ b 1 log_Psat (T , b) ≔ b - ――― (function to fit) 0 T ―+ b K 2 ⎡ 10 ⎤ b ≔ ⎢ 1000 ⎥ (guess parameters) guess ⎢ ⎥ ⎣ 100 ⎦ ⎡ 9.408 ⎤ b ≔ genfit ⎛T , log_P , b , log_P ⎞ = ⎢ 1.301 ⋅ 103 ⎥ (get the fit) fit ⎝ data data guess sat⎠ ⎢ ⎥ ⎣ -33.723 ⎦ log_Psat ⎝⎛350 K , bfit⎠⎞ = 5.293 (Test it) (Make a plot) Non-Commercial Use Only (Make a plot) i ≔ 0 ‥ 500 i Tplot ≔ min ⎝⎛Tdata⎠⎞ + ⎝⎛max ⎝⎛Tdata⎠⎞ - min ⎝⎛Tdata⎠⎞⎠⎞ ⋅ ―― i 500 log_Psat ⎛Tplot , bfit⎞ P ≔ 10 ⎝⎜ i ⎠⎟ ⋅ Pa sati 1⋅10⁴ 1⋅10³ 1⋅10² 1⋅10 Pdata (kPa) 1 Psat (kPa) 1⋅10⁻¹ 1⋅10⁻² 200 230 260 290 320 350 380 410 440 470 500 530 Tdata (K) Tplot (K) Non-Commercial Use Only.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us