Curve Fitting Using MATLAB
Abstract:
Curve fitting is the way we model or represent a data spread by assigning a best function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future. In this project our aim is to plot a linear and cubic fit curves along the raw data points given. The data provided contains the value of specific heat(Cp) at different temperatures. The values are in form in 3200x1 matrix. We will be using 'fit' in-built function in MATLAB to fit the curve. We will aim to find the "best fit curve" by calculating the 'Goodness of Fit' and also try to fit a curve perfectly.
Keywords: Goodness of fit, fit function, best fit curve, pefect fit
What is polynomial Curve Fitting?
Polynomial curve fitting is when we fit our data to the graph of a polynomial function. The least squares method can be used to find the polynomial, of a given degree, that has a minimum total error. In MATLAB we can use upto 9th degree polynomial to fit our curve.
A polynomial of degree n is a function of the form-
`f(x) = a_nx^n + a_(n-1)x^(n-1) + .........+ a_2x^2 + a_1x + a_0`
What is Least Squares Fitting?
Least squares fitting (also called least squares estimation) is a way to find the best fit curve or line for a set of points. In this technique, the sum of squares of the offsets (residuals) are used to estimate the best fit curve or line instead of the absolute values of the offsets.
Defining Some Terms used in codes:
1.Goodness of Fit:
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are,
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
2. Centering and Scaling Error
When we increase degree of the polynomial function the error generates in the commond window that "The function is badly conditioned". To avoid this error we use 'Normalise' as one of the curve fitting methods to center and scale our data.
MATLAB PROGRAM
1. Code for linear curve fit-
%Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp_value = cp_data(:,2);
%Program for curve fitting
[curvefit,gof] = fit(temperature,cp_value,'poly1','normalize','on')
figure(1)
plot(curvefit,'b');
hold on
plot(temperature,cp_value,'g');
legend('predicted curve','original data','Location',"northwest")
RESULT :
*Fig.1 shows the graph of givent data points and fit curve
*Fig.2 shows the values of coefficients of assumed linear polynomial
*Fig.3 shows the Values of different Goodness of Fit parameters.
NOTE.1 We are mainly focused on rsquare value(0.9249). The values generally above 0.95 are considered to be a good fit. Since value is below 0.95 we will increase our degree of polynomial.
2. Code for Cubic Fit
%Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp_value = cp_data(:,2);
%Program for curve fitting
[curvefit,gof] = fit(temperature,cp_value,'poly3','normalize','on')
%Program for Ploting Curves
figure(1)
plot(curvefit,'b');
hold on
plot(temperature,cp_value,'g');
legend('predicted curve','original data','Location',"northwest")
xlabel('Temperature[K]')
ylabel('Specific Heat Cp[KJ/Kmol-K]')
RESULTS :
*Fig.4 shows the graph of givent data points and fit curve.
*Fig.5 shows the values of coefficients of assumed linear polynomial
*Fig.6 shows the Values of different Goodness of Fit parameters(gof).
Observation :
We can observe that value of rsquare has increased which means our cubic curve fit is better that linear fit. BUT there is still some errors and the curve is not able to predict the actual curve.
What is perfect fit?
When our fitting curve completely overlaps our actual curve we can say that the assumed fit is a perfect fit. When rsquare value becomes 1 then also we can also say that our assumed curve is perfect fit.
NOTE.2 To find a perfect fit we can increase the degree of our polynomial function but there are some limitations to use a polynomial function of higher degree. Higher order polynomials can oscillatory between data points, leading to a poorer fit to the data. Polynomials are unbounded, oscillatory functions by nature. Therefore, they are not well-suited to extrapolating bounded data or monotonic (increasing or decreasing) data.
Example -
6th degree polynomial -
9th degree polynomial -
Observation Table :
| Polynomial Order | SSE | R-square | Adjusted R-square | RMSE |
| Linear | `2.1630times10^6` | `0.9249` | `0.9249` | `26.0072` |
| Cubic | `9.4272times10^4` | `0.9967` | `0.9967` | `5.4311` |
| 6 degree | `8.3322times10^3` | `0.9997` | `0.9997` | `1.6154` |
| 9 degree | `95.8326` | `1.0000` | `1.0000` | `0.1733` |
It is clearly visible through table that GOF parameters keeps on improving as we increase the order of the polynomial. R-square increases from 0.9249,for linear fit, to 1, for 9 degree polynomial fit.
NOTE.3 We can observe that rsqaure is 1 and 9th order polynomial is a perfect fit. BUT as we mentioned earlier, blindly increasing the degreee of polynomial will not be a good method.
NOTE.4 The best way of improving our cubic curve fit is to split our datapoints and plot a seperate cubic curve for each set.
Splitwise Curve Fit Data
Code for fitting be split wise method -
clear all
close all
%Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp_value = cp_data(:,2);
%Splitting our data point in 4 different sets
temp1 = temperature(1:588,1);
cp_value1 = cp_value(1:588,1);
temp2 = temperature(589:1283,1);
cp_value2 = cp_value(589:1283,1);
temp3 = temperature(1284:2500,1);
cp_value3 = cp_value(1284:2500,1);
temp4 = temperature(2501:3200,1);
cp_value4 = cp_value(2501:3200,1);
%PROGRAM FOR FITTING CURVE
%Value of coefficients of our predicted polynomial function
[coeff_values1,S1,mu1] = polyfit(temp1,cp_value1,3);
[coeff_values2,S2,mu2] = polyfit(temp2,cp_value2,3);
[coeff_values3,S3,mu3] = polyfit(temp3,cp_value3,3);
[coeff_values4,S4,mu4] = polyfit(temp4,cp_value4,3);
%Evaluate the cp at the specified values of temperature ranges
predicted_cp1 = polyval(coeff_values1,temp1,[],mu1);
predicted_cp2 = polyval(coeff_values2,temp2,[],mu2);
predicted_cp3 = polyval(coeff_values3,temp3,[],mu3);
predicted_cp4 = polyval(coeff_values4,temp4,[],mu4);
%Comparing my original curve to predicted curve
figure(2)
plot(temperature,cp_value,'LineWidth',4,"Color",'b');
hold on
plot(temp1,cp_value1,'LineWidth',2,"Color",'r');
plot(temp2,cp_value2,'LineWidth',2,"Color",'r');
plot(temp3,cp_value3,'LineWidth',2,"Color",'r');
plot(temp4,cp_value4,'LineWidth',2,"Color",'r');
legend('predicted curve','original data','Location',"northwest")
xlabel('Temperature[K]')
ylabel('Specific Heat Cp[KJ/Kmol-K]')
Explaination of above code:
1. Under comment %Preparing the data -
We load the given data in form of matrix using "load" command. Then we assaign temperature and Cp seperately in different arrays.
2. Under comment %Splitting our data point in 4 different sets -
We divide our Cp value and temperature in 4 different arrays.
3. Under coment %PROGRAM FOR FITTING CURVE -
In first part we calculate coefficients using "polyfit" in built function for each set of temp. and Cp.
In second part we calculate the predicted Cp using our coefficient that we determined in first part.
4. Under comment %Comparing my original curve to predicted curve -
I plot my original curve and my predictd curve
RESULTS :
We can observe that our predicted curve fit perfectly on our original curve.
CONCLUSION :
- As we keep on increasing the degree of the polynomial, the curve fit the original curve in more accurate way with minimum error.
- The limitation of increasing the polynomial is that polynomial functions are oscillatory in nature. The higher order polynomials can fit the current curve with minimum errors but they might not be able to predict the future values accurately.
- The splitwise curve fitting is much better way predict the given curve.
REFERENCES :
https://in.mathworks.com/help/curvefit/evaluate-a-curve-fit.html#EvaluateCurveFitExample-8
https://in.mathworks.com/help/curvefit/fit.html
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860