Curve fitting using MATLAB
Objective: -
- Develop 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, legends could be shown if necessary.
- Develop code to show splitwise method and explain the parameters used to measure the fitness characteristics for both the curves.
Principle: -
Curve fitting refers to finding an appropriate mathematical model that expresses the relationship between a
dependent variable Y and a single independent variable X. It 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, in which a "smooth" function is constructed that approximately fits the data.
Solution Procedure: -
- Linear: `y=a+bx`
This common model is usually fit using standard linear regression techniques. We include it here to allow for
various special forms made by transforming X and Y
- Quadratic: `y=a+bx+cx^2`
The quadratic or second-order polynomial model results in the familiar parabola.
- Cubic: `y=a+bx+cx^2+dx^3`
This is the cubic or third-order polynomial
Curve fit algorithms determine the best-fit parameters by minimizing a chosen merit function. In order to optimize the merit function, it is necessary to select a set of initial parameter estimates and then iteratively refine the merit parameters until the merit function does not change significantly between iterations.
Error(i) =|y(i) - value of function y = f(x) at x=x(i)| = |y(i) - value of f(x) at x(i)|
Fitting Techniques: -
- Fourier to fit data similar to the waveform
- Linear fitting doing the first-order fit directly for the selected data set
- The polynomial fit - general polynomial of any particular order to fit the dataset
- Interpolant method - create a curve/surface passing all the data point
The parameter used to measure the fitness of the curve: -
- SSE(Sum of the square term) - 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
`SSE=∑_(i=1)^n Err(i)^2`
A value closer to 0 indicates that the model has a smaller random error component and that the fit will be more useful for prediction.
-
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=SSR/SST
-
Degrees of Freedom 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.
v = n – m
v 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 are increased by the number of such parameters.
The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested — that is, a series of models each of which adds additional coefficients to the previous model.
adjusted R-square=1−SSE(n−1)SST(v)
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. Negative values can occur when the model contains terms that do not help to predict the response.
Root Mean Squared Error
This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data and is defined as
RMSE=s= `root 2MSE`
Based on the research I have done:-
How to make a curve fit perfectly?
The curve fit is known to be a perfect fit when the predicted curve points and the actual curve points meet at all the points and the value for the R square is 1.Increasing the number of polynomials thus, increase the probability of a more fitter curve. Perfect can always be the best fit but the best fit can not be a perfect fit, we can get a perfect fit with a 3-degree polynomial but it is not the best fit as for the best fit we can increase the degree of the polynomial
How to get the best fit?
We can get the best fit by increasing the degree of the polynomial and can also use RMSE and also we can use splitwise method to split the data into folds and perform a curve fit to get the best fit
What could be done to improve the cubic fit?
We can improve the cubic fit by increasing the degree of a polynomial or by using the splitwise method to get an improved cubic fit
CODE for the curve for the original dataset: -
clear all
close all
clc
%to laod a file we use load command in Matlab
cp_data = load('data');
% to Access the first column which contain temperature
temp = cp_data(:,1);
%to access the cp in second column of the fiel
cp =cp_data(:,2);
%Potting of the curve to be fitted
plot(temp,cp,'linewidth',3)
xlabel('Temperature')
ylabel('Specific Heat[KJ/Kmol-K]')
legend('Original dataset','location','southeast')
title('Original Dataset')
Explanation of the above code: -
- clear all -to clear all the workspace variables
- close all - to close all the output window
- clc - to clear the command window
- %to laod a file we use load command in Matlab - to clear the command window
- cp_data = load('data');- load command is use to load the file data
- % to Access the first column which contains temperature -comments
- temp = cp_data(:,1); - access the first column of the loaded data which contain the temperature
- %to access the cp in the second column of the file - comments
- cp =cp_data(:,2); - access the second column of the loaded data which contain the cp
- %Potting of the curve to be fitted - comments
- plot(temp,cp,'linewidth',3) - plotting of the temperature and cp dataset
- xlabel('Temperature[Kelvin] - labelling x-axis as temperature
- ylabel('Specific Heat[KJ/Kmol-K]') - labelling y-axis as specific heat
- legend('Original dataset','location','southeast') - giving the legend as original dataset and locating it
- title('Original Dataset') - giving title to the plot
Output:-

CODE for the curvefit for the Linear Polynomial: -
clear all
close all
clc
%to laod a file we use load command in Matlab
cp_data = load('data');
% to Access the first column which contain temperature
temp = cp_data(:,1);
%to access the cp in second column of the fiel
cp =cp_data(:,2);
%polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc
%as n polynomials
co_eff = polyfit(temp,cp,1)
predicted_cp =polyval(co_eff,temp)
%Potting of the curve to be fitted
plot(temp,cp,'linewidth',3,'color','b')
%compare my curve with riginal curve
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
legend('Original dataset','Curvefit','location','southeast')
title('Linear Polynomial plot')
xlabel('Temperature[kelvin]')
ylabel('Specific Heat[KJ/Kmol-K]')
Explanation of the above code: -
- clear all -to clear all the workspace variables
- close all- to close all the output window
- clc - to clear the command window
- %to laod a file we use load command in Matlab - to clear the command window
- cp_data = load('data'); - load command is use to load the file data
- % to Access the first column which contain temperature - comments
- temp = cp_data(:,1); - access the first column of the loaded data which contain the temperature
- %to access the cp in second column of the file - comments
- cp =cp_data(:,2); - access the second column of the loaded data which contain the cp
- %polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc - comments
- %as n polynomials - comments
- co_eff = polyfit(temp,cp,1) - polyfit command gives the set of coefficients
- predicted_cp =polyval(co_eff,temp) - predicted cp
- %Potting of the curve to be fitted - comments
- plot(temp,cp,'linewidth',3,'color','b') -plotting of the temperature and cp dataset
- %compare my curve with riginal curve - comments
- hold on - holding the plot
- plot(temp,predicted_cp,'linewidth',3,'color','r') - plotting of the temperature and predicted cp dataset
- legend('Original dataset','Curvefit','location','southeast') - giving the legend as original dataset and locating it
- title('Linear Polynomial plot') - giving title to the plot
- xlabel('Temperature[kelvin]') - labelling x axis as temperature
- ylabel('Specific Heat[KJ/Kmol-K]') - labelling x axis as specific heat
Output: -

CODE for the curvefit for the Quadratic Polynomial: -
clear all
close all
clc
%to laod a file we use load command in Matlab
cp_data = load('data');
% to Access the first column which contain temperature
temp = cp_data(:,1);
%to access the cp in second column of the fiel
cp =cp_data(:,2);
%polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc
%as n polynomials
co_eff = polyfit(temp,cp,2)
predicted_cp =polyval(co_eff,temp)
%Potting of the curve to be fitted
plot(temp,cp,'linewidth',3,'color','b')
%compare my curve with riginal curve
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
legend('Original dataset','Curvefit','location','southeast')
title('Quadratic Polynomial plot')
xlabel('Temperature[kelvin]')
ylabel('Specific Heat[KJ/Kmol-K]')
Explanation of the above code: -
- clear all -to clear all the workspace variables
- close all- to close all the output window
- clc - to clear the command window
- %to laod a file we use load command in Matlab - to clear the command window
- cp_data = load('data'); - load command is use to load the file data
- % to Access the first column which contain temperature - comments
- temp = cp_data(:,1); - access the first column of the loaded data which contain the temperature
- %to access the cp in second column of the file - comments
- cp =cp_data(:,2); - access the second column of the loaded data which contain the cp
- %polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc - comments
- %as n polynomials - comments
- co_eff = polyfit(temp,cp,2) - polyfit command gives the set of coefficients
- predicted_cp =polyval(co_eff,temp) - predicted cp
- %Potting of the curve to be fitted - comments
- plot(temp,cp,'linewidth',3,'color','b') -plotting of the temperature and cp dataset
- %compare my curve with riginal curve - comments
- hold on - holding the plot
- plot(temp,predicted_cp,'linewidth',3,'color','r') - plotting of the temperature and predicted cp dataset
- legend('Original dataset','Curvefit','location','southeast') - giving the legend as original dataset and locating it
- title('Quadratic Polynomial plot') - giving title to the plot
- xlabel('Temperature[kelvin]') - labelling x axis as temperature
- ylabel('Specific Heat[KJ/Kmol-K]') - labelling x axis as specific heat
Output: -

CODE for the curvefit for the Cubic Polynomial: -
clear all
close all
clc
%to laod a file we use load command in Matlab
cp_data = load('data');
% to Access the first column which contain temperature
temp = cp_data(:,1);
%to access the cp in second column of the fiel
cp =cp_data(:,2);
%polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc
%as n polynomials
co_eff = polyfit(temp,cp,3)
predicted_cp =polyval(co_eff,temp)
%Potting of the curve to be fitted
plot(temp,cp,'linewidth',3,'color','b')
%compare my curve with riginal curve
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
legend('Original dataset','Curvefit','location','southeast')
title('Cubic Polynomial plot')
xlabel('Temperature[kelvin]')
ylabel('Specific Heat[KJ/Kmol-K]')
Explanation of the above code: -
- clear all -to clear all the workspace variables
- close all- to close all the output window
- clc - to clear the command window
- %to laod a file we use load command in Matlab - to clear the command window
- cp_data = load('data'); - load command is use to load the file data
- % to Access the first column which contain temperature - comments
- temp = cp_data(:,1); - access the first column of the loaded data which contain the temperature
- %to access the cp in second column of the file - comments
- cp =cp_data(:,2); - access the second column of the loaded data which contain the cp
- %polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc - comments
- %as n polynomials - comments
- co_eff = polyfit(temp,cp,3) - polyfit command gives the set of coefficients
- predicted_cp =polyval(co_eff,temp) - predicted cp
- %Potting of the curve to be fitted - comments
- plot(temp,cp,'linewidth',3,'color','b') -plotting of the temperature and cp dataset
- %compare my curve with riginal curve - comments
- hold on - holding the plot
- plot(temp,predicted_cp,'linewidth',3,'color','r') - plotting of the temperature and predicted cp dataset
- legend('Original dataset','Curvefit','location','southeast') - giving the legend as original dataset and locating it
- title('Cubic Polynomial plot') - giving title to the plot
- xlabel('Temperature[kelvin]') - labelling x axis as temperature
- ylabel('Specific Heat[KJ/Kmol-K]') - labelling x axis as specific heat
Output: -

CODE for the curvefit using Splitwise method splitter data into three folds: -
clear all
close all
clc
%to laod a file we use load command in Matlab
cp_data = load('data');
% to Access the first column which contain temperature
temp = cp_data(:,1);
%to access the cp in second column of the fiel
cp =cp_data(:,2);
%polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc
%as n polynomials
co_eff1 = polyfit(temp(1:1000),cp(1:1000),2)
co_eff2 = polyfit(temp(1000:2000),cp(1000:2000),3)
co_eff3 = polyfit(temp(2000:3200),cp(2000:3200),4)
predicted_cp1 =polyval(co_eff1,temp(1:1000))
predicted_cp2 =polyval(co_eff2,temp(1000:2000))
predicted_cp3 =polyval(co_eff3,temp(2000:3200))
%Potting of the curve to be fitted
plot(temp,cp,'linewidth',3,'color','b')
%compare my curve with riginal curve
hold on
plot(temp(1:1000),predicted_cp1,'linewidth',3,'color','r')
plot(temp(1000:2000),predicted_cp2,'linewidth',3,'color','black')
plot(temp(2000:3200),predicted_cp3,'linewidth',3,'color','g')
legend('Original dataset','Curvefit','Splitwise','location','southeast')
title('Splitwise curvefit')
xlabel('Temperature[kelvin]')
ylabel('Specific Heat[KJ/Kmol-K]')
Explanation of the above code: -
- clear all -to clear all the workspace variables
- close all - to close all the output window
- clc - to clear the command window
%to laod a file we use load command in Matlab
- cp_data = load('data'); - load command is use to load the file data
% to Access the first column which contain temperature
- temp = cp_data(:,1); - access the first column of the loaded data which contain the temperature
%to access the cp in second column of the fiel
- cp =cp_data(:,2); - access the second column of the loaded data which contain the cp
%polyfit command gives the set of coefficient temp as x cp as y and 2,3,etc
%as n polynomials
- co_eff1 = polyfit(temp(1:1000),cp(1:1000),2) - polyfit command gives the set of coefficients 1 for the range for the quadratic polynomial
- co_eff2 = polyfit(temp(1000:2000),cp(1000:2000),3) - - polyfit command gives the set of coefficients2 for the range for the cubic polynomial
- co_eff3 = polyfit(temp(2000:3200),cp(2000:3200),4) - polyfit command gives the set of coefficients2 for the range for the four degree polynomial
- predicted_cp1 =polyval(co_eff1,temp(1:1000)) - predicted cp1
- predicted_cp2 =polyval(co_eff2,temp(1000:2000)) - predicted cp2
- predicted_cp3 =polyval(co_eff3,temp(2000:3200)) - predicted cp3
%Potting of the curve to be fitted
- plot(temp,cp,'linewidth',3,'color','b') - plotting of the temperature and cp dataset
%compare my curve with riginal curve
- hold on - to hold the screen
- plot(temp(1:1000),predicted_cp1,'linewidth',3,'color','r') - plotting of the temperature and predicted_cp1 dataset
- plot(temp(1000:2000),predicted_cp2,'linewidth',3,'color','black') - plotting of the temperature and predicted_cp2 dataset
- plot(temp(2000:3200),predicted_cp3,'linewidth',3,'color','g') - plotting of the temperature and predicted_cp3 dataset
- legend('Original dataset','Curvefit','Splitwise','location','southeast') - giving the legend as original dataset and locating it
- title('Splitwise curvefit') - giving title to the plot
- xlabel('Temperature[kelvin]') - labelling x axis as temperature
- ylabel('Specific Heat[KJ/Kmol-K]') - labelling x axis as specific heat
Output: -

Errors face while programming: -
- Error raised was Character vector is not terminated properly
On debugging it was found that (') was missing in a statement

- The error arises of an Invalid expression
On resolving it was due to missing of an expression

- An error was found because of an Unrecognized function
It was due to the invalid typo error of the function name

- an error was due to an unrecognized variable

- This error was due to the first two inputs must have the same number of elements

- The error was due to an Unrecognized function

- The error arises due to the typo error

References: -
https://www.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html