Curve fitting for the temperature and specific heat values.
1. AIM:
Curve fitting for the temperature and specific heat values.
2. OBJECTIVES:
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points. Title and axes labels are a must, legends could be shown if necessary.
- Explain the parameters used to measure the fitness characteristics for both the curves.
3. TASKS:
- How to make a curve fit perfectly?
- How to get the best fit? Note: Best fit and perfect fit are two completely different phenomena.
- What could be done to improve the cubic fit?
4. THEORY:
Polynomial models for curves are given by;
Where n + 1 is the order of the polynomial, n is the degree of the polynomial, and 1 ≤ n ≤ 9. The order gives the number of coefficients to be fit, and the degree gives the highest power of the predictor variable.
In this guide, polynomials are described in terms of their degree. For example, a third-degree (cubic) polynomial is given by;
The main advantages of polynomial fits include reasonable flexibility for data that is not too complicated, and they are linear, which means the fitting process is simple. The main disadvantage is that high-degree fits can become unstable. Additionally, polynomials of any degree can provide a good fit within the data range, but can diverge wildly outside that range. Therefore, exercise caution when extrapolating with polynomials.
5. GIVEN:
Data file with 2 column vector, one for temperature and other for specific heat.
6. POLYNOMIAL EQUATION:
7. BODY OF THE CONTENT:
Start the program writing by creating new script;
Always use the comments in the program for the validation of specific inputs and thus the commands have been use through the program;
- Code to fit a linear and cubic polynomial for the Cp data.;
Load the data file, and create two new variables: is a column vector containing the Temperature. cp is a column vector with specific heat values correspond to the temperature.
- Now write the lines for linear polynomial and cubic polynomial.
Now there are more than couple of ways to create such polynomials vectors.
- Use the Polyfit and Polyval function of curve fitting for recording the linear and cubic polynomial as shown in the code.
As you can see in the code, by this way we have to create the variables first and then we need to record the values.
OR
- Use the fit function to fit a polynomial to data. You specify degree polynomial, with the string 'poly’n’'. The first output from fit is the polynomial, and the second output, good 1/2, contains the goodness of fit statistics you will examine in a later step.
Both the function are perform similar action but the record method is different.
- Plotting of the curves
To plot the fit, use the plot function. Add a legend in the top left corner.
To fit polynomials of different degrees, change the fit type string, e.g., for a cubic or third-degree polynomial use 'poly3'. The scale of the input, T, is quite large, so you can obtain better results by centering and scaling the data. To do this, use the 'Normalize' option.
Experimenting with an exponential equation should fit this census data well. To fit a single term exponential model, use 'exp1' as the fit type.
Plot all the fits at once, and add a meaningful legend in the top left corner of the plot.
The fit and residuals for the single-term exponential equation indicate it is a poor fit overall. Therefore, it is a poor choice and you can remove the exponential fit from the candidates for best fit.
Fig: with polyfit and polyval
Fig: with fit
Fig: with fit and the exponential fit
- Plot the Residuals to Evaluate the Fit;
To plot residuals, specify 'residuals' as the plot type in the plot function.
The fits and residuals for the polynomial equations are all similar, making it difficult to choose the best one.
If the residuals display a systematic pattern, it is a clear sign that the model fits the data poorly.
- Examine Goodness-of-Fit Statistics;
The struck good1/good2 shows the goodness-of-fit statistics for the 'poly2' fit. When you created the 'poly2' fit with the fit function in an earlier step, you specified the good1/good2 output argument.
Examine the sum of squares due to error (SSE) and the adjusted R-square statistics to help determine the best fit. The SSE statistic is the least-squares error of the fit, with a value closer to zero indicating a better fit. The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.
The large SSE for 'cp_Exp' indicates it is a poor fit, which you already determined by examining the fit and residuals. The lowest SSE value is associated with 'poly3'. However, the behavior of this fit beyond the data range makes it a poor choice for extrapolation, so you already rejected this fit by examining the plots with new axis limits.
Fig: poly1
Fig: poly3
- Compare the Coefficients and Confidence Bounds to Determine the Best Fit
Resolve the best fit issue by examining the coefficients and confidence bounds for the remaining fits: the fifth-degree polynomial and the quadratic.
Examine cp2 and cp3 by displaying the models, the fitted coefficients, and the confidence bounds for the fitted coefficients.
Fig: poly3 Fig: poly1
Fig: poly1
Fig: poly3
- Use curve fitting tool box for the best fit.
Use cftool command and select the parameter for x and y and also select the polynomial and the degree of polynomial.
Fig: poly1
Fig: poly3
9. EXPLAINATION:
- How to make a curve fit perfectly?
Curve fit can perfectly done with help of curve fitting tool as if we use the automatic command,
If we are manually plotting the curve then ore the degree of polynomial better the curve fitting.
I have found this by doing some trials on the different degrees of polynomials but again this can be dependent on the data type.
- How to get the best fit? Note: Best fit and perfect fit are two completely different phenomena.
As I explained in above question, perfect fit can be achieved with the higher degree of polynomials. Whereas, the best fit is something for which we need to have the values for R-square. R_square should be as near or equal to 1 for best fit. As we can see poly3 is best fit curve than poly1 for the above example as the poly is very near to zero.
Or else use the curve fitting tool for fitting automatically as it can provide you fit with R_square =1.SSE is also very important for best fit, its values should as near to zero for best fit.
- What could be done to improve the cubic fit?
To improve the cubic fit, we can normalize the values or else we can use the higher degree polynomial.
10. ERRORS OR ISSUES
Above are the screenshots of the error or issues I faced,
For first error, I use the normalized function to get the error resolved as the scale of the input, T, is quite large, so you we can obtain better results by centering and scaling the data. To do this, I used the 'Normalize' option. For the second, my file directory got changed in the middle of scripting (unknown reason), thus I had to select the path again and issue got resolved.
11. REFERENCE
https://en.wikipedia.org/wiki/Polynomial
https://in.mathworks.com/help/curvefit/index.html?s_tid=CRUX_lftnav