Curve fitting for given data using MATLAB
Aim:
To write code for a linear and cubic polynomial curve fitting for the Cp data w.r.t. temperature for given Raw data and Plot the linear and cubic curves fit along with the raw data points using MATLAB.
Objective:
To write program in Matlab to fit a linear and cubic polynomial for the given data of Cp and do comparision among linear fitting, cubic fitting and raw data by ploting them.
Theory:
Curve fitting: Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. A related topic is regression analysis, which focuses more on questions of statical inferance such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainity. since it may reflect the method used to construct the curve as much as it reflects the observed data.
Fig. : Graphical represantation of curve fitting
Answer Of Question Asked:
1. How to make a curve fit perfectly?
Ans: We can make a curve fit as a perfect fit by increasing the degree of a polynomial (cubic polynomial is perfect fit when compared to the linear polynomial in the given problem) or by split-wise polynomial i.e splitting the enter data into certain intervals and calculating the coefficients, they are plotted together to get curve fit perfectly.
2. How to get the best fit?
Ans: The best fit is achieved when the error becomes minimum i.e the geometric distance between the original value and the predicted value is minimum (in perfect case it is 'zero').
3. What could be done to improve the cubic fit?
Ans: To improve the cubical fit, we need to use split wise polynomial which splits the given data into the required range (in this case we have split into the two different range) and plot them together.
Step wise explanation of Program:
1. In first step load the given Raw data and then load data for temperature and cp seperately from coloumn 1 and 2.
2. Next by using polyfit and polyval command feed curve fit for linear polynomial and cubic polynomial.
3. Then plot linear and cubic polynomial curve fit with raw data for comparission using plot command.
4. Next level the graph in x-direction and y-direction and put legend.
Program:
1. Program for linear and cubic polynomial curve fitting:
clear all
close all
clc
% Preparing the data
cp_data = load('data');
% load data for temperature from coloumn 1
temperature = cp_data(:,1);
% load data for cp from coloumn 2
cp = cp_data(:,2);
%curve fit for linear polynomial
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
%curve fit for cubic polynomial
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
% polting linear and cubical polynomial curve fit with original data and
plot(temperature,cp ,'linewidth',3,'color','black')
hold on
plot(temperature,predicted_cp1 ,'linewidth',3,'color','b')
hold on
plot(temperature,predicted_cp2 ,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-k]')
legend('Original data','Linear curve fitting','cubical curve fitting')
Output:
2. Program for Split wise curve fitting.
% Program for split wise curve fitting
clear all
close all
clc
% Preparing the data
cp_data = load('data');
% load data for temperature from coloumn 1
temperature = cp_data(:,1);
% load data for cp from coloumn 2
cp = cp_data(:,2);
%split wise curve fit for linear polynomial
co_effs1 = polyfit(temperature(1:1600),cp(1:1600),1);
predicted_cp1 = polyval(co_effs1,temperature(1:1600));
%split wise curve fit for cubic polynomial
co_effs2 = polyfit(temperature(1601:3200),cp(1601:3200),3);
predicted_cp2 = polyval(co_effs2,temperature(1601:3200));
% polting linear and cubical polynomial curve fit with original data and
plot(temperature,cp ,'linewidth',3,'color','black')
hold on
plot(temperature(1:1600),predicted_cp1 ,'linewidth',3,'color','b')
hold on
plot(temperature(1601:3200),predicted_cp2 ,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-k]')
legend('Original data','Linear splitwise curve fitting','cubical splitwise curve fitting')
Output:
The parameters used to measure the fitness characteristics for the curves:
The following are the parameters used to measure fitness characteristic for the curve:
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
Sum of Squares Due to Error
This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE. A value closer to 0 indicates a better fit. Note that the SSE was previously defined in the 'least square fitting method'.
Suppose we have the best fit function as y=f(x) for the given points ((x1,y1),........,(xn,yn)) then an error of the fit is given by
Error=| y(i)-f(x(i))|;
where
y(i) is the predicted value or the point of the best fit function at "i" point.
f(x(i) is the original value at the "i" point.
SSE is calculated by the,
SSR=∑i=1n▒(f(x(i))-Mean)2">SSR=∑ni=1▒(f(x(i))−Mean)2SSR=∑i=1n▒(f(x(i))-Mean)2
R-Square
This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination. R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares about the mean (SST).
where SST = SSR + SSE. Given these definitions, R-square is expressed as
R^2 = SSR/SST
R-square can take on any value between 0 and 1, with a value closer to 1 indicating a better fit. For example, an R2 value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average. If you increase the number of fitted coefficients in your model, R-square might increase although the fit may not improve. To avoid this situation, you should use the degrees of freedom adjusted R-square statistic described below. Note that it is possible to get a negative R-square for equations that do not contain a constant term. If R-square is defined as the proportion of variance explained by the fit, and if the fit is actually worse than just fitting a horizontal line, then R-square is negative. In this case, R-square cannot be interpreted as the square of a correlation.
Adjusted R-Square
This statistic uses the R-square statistic defined above, and adjusts it based on the residual degrees of freedom. The residual degrees of freedom is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values. K indicates the number of independent pieces of information involving the n data points that are required to calculate the sum of squares. Note that if parameters are bounded and one or more of the estimates are at their bounds, then those estimates are regarded as fixed. The degrees of freedom is increased by the number of such parameters.The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model. The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit.
The adjusted R-square is calculated by:
R_adj^2=1-[((1-R^2 )*(n-1))/(n-k-1)] |
Root Mean Squared Error
This statistic is also known as the fit standard error and the standard error of the regression. where MSE is the mean square error or the residual mean square. A RMSE value closer to 0 indicates a better fit.
RMSE=√(SSE/n) |
here
SSE- the sum of square due to error.
n- number of data points.
By using Curve fitting Tool, The fitness of curve is measured:
1. Fitness of Linear Curve fitting:
2. Fitness of Cubic polynomial Curve fitting:
Error faced while programming:
1. Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Step taken to overcome:
Replaced semicolon (;) to colon (:).
2.Error using plot, Vectors must be the same length.
Step taken to overcome:
Changes made in both plot command.
plot(temperature,predicted_cp1 ,'linewidth',3,'color','b') to plot(temperature(1:1600),predicted_cp1 ,'linewidth',3,'color','b')
plot(temperature,predicted_cp2 ,'linewidth',3,'color','r') to plot(temperature(1601:3200),predicted_cp2 ,'linewidth',3,'color','r')
Conclusion:
Thus, we have executed the linear polynimial fitting, cubical polynomial fitting and the split wise polynomial fitting for a set of data given, that containing specific heat o at the respective temperature using mathematical functions in Matlab.
Referances: