A program for linear fit and cubical fit for given dataset using matlab and what criteria needs to be used for best fit and perfect fit
Aim :- Write a program for linear fit and cubical fit for given dataset using matlab and what criteria needs to be used for best fit and perfect fit.
Objective :-
- 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.
- Write a code to show splitwise method.
- Explain the parameters used to measure the fitness characteristics for both the curves.
Curve Fitting:-
- Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data
- Curve fitting basically examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.
- It is highly effective in the mathematical modelling of some natural processes.
- Curve fitting is one of the most powerful and most widely used analysis tools in Origin.
- Most commonly used fitting techniques are interpolant and polynomial for unknown nature of experimental data.
- Interpolation is a technique in which each point is joined one by one to get a meaning full curve thereby evaluating the point. However, the main drawbacks of this technique can be only used when the data is reliable.
- Linear fitting technique is nothing but the use of first order fit directly for the selected datasets whereas polynomial fit helps you to generate a polynomial of any particular order to fit the dataset.
Assume data points (x1,y1) , (x2,y2) , (x3,y3) , ... (xn,yn) and assume the best fit for these set of points will be y = f(x), the criterion under which the equation of the curve will be the best fit possible is as follows,

Suppose each of these n points has an individual error (difference in y co-ordinate values)
The error for a general "i" point can be given by the function,
Error = |y(i) − value of f(x) at x(i)| = |y(i) − f(x(i))|
To remove the modulus sign, it is effective if we square the error term, when we square the error terms and add the errors for all the 'n' points we get,
n
Σ Error(i)^2
i=1
Let us assume the proposed equation is a nth degree polynomial of the form,
f(x) = A1 + A2 ⋅ x + A3 ⋅ x2 + ... + An ⋅ xn−1
The first-degree polynomial equation

It is a line with slope a. A line will connect any two points, so a first-degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second-degree polynomial, then equation changes as under:

This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third-degree polynomial, the following is obtained:

This will exactly fit four points.
Parameters / Criteria used to measure the fitness characteristics of the curve as under:
1. The sum of squares due to error (SSE)
2. R-square
3. Adjusted R-square
4. Root mean squared error (RMSE)
1) 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 denoted as SSE. If the value of SSE comes closer to 0 then it indicates the better fit.
Formula of SSE is as under;

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.
According to layman's terms, higher the value of R-square the better the fit, the lower it is the lousier the fit.
Rsquare ranges from a value of 0 to 1
i) 0 for the worst possible fit for the data set
ii) 1 for the best possible fit for the data set

R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).

SSR = sum of squares of the regression, which is basically the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the data set.

if y=f(x) is the fit curve then the SSR is defined as follows,
n
SSR = Σ (f(x(i)) − Mean)^2
i=1
Similarly, SSE is formulated as under;
n
SSE = Σ (Y (i) − f(x(i)))^2
i=1
3) Adjusted R-square
i) Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant
independent variables.
for y = f(x1, x2, x3, ..., xn), here x1, x2, ..., xn are the independent variables and y is the dependent variable.
If value of x(i)contributes to the output y, then x(i) is a significant dependent variable.
If value of does not x(i) contribute to the output y, then x(i) is an insignificant dependent variable.
ii) Adjusted R-square only considers the variables which actually contribute to the output of the system in a drastic way.
R-square is formulated as shown below,

where,
n - the number of datapoints
k - the number of independent variables
Case (1) :-

Case (2) :-

4) RMSE (Root Mean Squared Error)
This statistic is also known as the fit standard error and the standard error of the regression. An RMSE value closer to 0 indicates a better fit. It is an estimate of the standard deviation of the random component in the data, and is defined as,

Matlab Program :-
1) Linear Fit :-
% Write a program for linear fit for given dataset using matlab
clc;
clear all;
close all;
% Inputs
cp_data = load('data');
temperature =cp_data(:,1);
cp =cp_data(:,2);
% Curve Fitting
% cp = a*T + C
coefficients = polyfit(temperature,cp,1);
predicted_cp = polyval(coefficients,temperature);
% Comparing original dataset with curve fit
plot(temperature,cp,'linewidth',3,'color','r');
hold on;
plot(temperature,predicted_cp,'linewidth',3,'color','m');
xlabel('Temperature [k]');
ylabel('Specific Heat [KJ/Kmol-K]');
grid on;
legend('Original Dataset','Linear Curve Fit');
2) Cubical Fit :-
% Write a program for Cubical fit for given dataset using matlab
clc;
clear all;
close all;
% Inputs
cp_data = load('data');
temperature =cp_data(:,1);
cp =cp_data(:,2);
% Curve Fitting
% cp = a*T^3 + b*T^2 + c*T + D
coefficients = polyfit(temperature,cp,3);
predicted_cp = polyval(coefficients,temperature);
% Comparing original dataset with curve fit
figure(2);
plot(temperature,cp,'linewidth',3,'color','r');
hold on;
plot(temperature,predicted_cp,'linewidth',3,'color','k');
xlabel('Temperature [k]');
ylabel('Specific Heat [KJ/Kmol-K]');
grid on;
legend('Original Dataset','Cubical Curve Fit');
Steps :-
1) Start the program with 'clear all','close all', and 'clc' commands
2) The data of specific heat is loaded with ‘load’ command and 'Temperature', as well as 'Cp' array, is defined.
3) Curve fitting is performed using 2 functions:
Polyfit is a Matlab function that computes a least-squares polynomial for a given set of data. Polyfit generates the
coefficients of the polynomial, which can be used to model a curve to fit the data.
Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the
coefficients found using polyfit.
4) The curve fit data is compared with original data by plotting the curves to get linear and cubic curve fitting.
5) The axis is shifted by using the 'axis' command and the legend is provided for the plot.
Results :-
1) Linear Fit :-

It can be observed that there are large variations between the curves and hence curves don't fit perfectly.
2) Cubical Fit :-

It can be observed that there are fewer variations between the curves and hence curves fit perfectly.
Curve Fitting Toolbox:
1. Curve Fitting Toolbox is another alternative way to validate the fitness characteristics of the curve.
2.We just have to prepare the data first and then open the ‘Curve Fitting Toolbox’ and then select the ‘x’ and ‘y’ values.
3.The toolbox will automatically plot the original curve. Then we have to select the degree of polynomial to fit the original
curve. This will plot a new curve according to the degree of the polynomial as well as it will display the values of R-square,
SSE, Adjusted R-square, and RMSE.
Linear Fit :-

If we consider Adjusted r-square as a tool of comparison for the goodness of fit, then its value for linear regression is 0.9249 or 92.49%. Hence it is not a good fit.
Cubical Fit :-

The value of Adjusted R square for a cubic polynomial is 0.9967 or 99.67% which is a better fit.
Best Fit:-
It means that the differences between the actual measured values and the values predicted by the model equation are
minimized.
Perfect Fit:-
When the curve goes through every point. The sum-of-squares is 0.0, and the R2 is 1.00.
How to get the best fit?
1. The degree of the polynomial affects the ‘detail’ to which it approximates your data.
2. A first-degree polynomial is a simple linear fit: y=b(1)*x+b(2), the degree being one less than the number of parameters the polynomial estimates. The higher the polynomial degree, the more closely the polynomial fits the data. Lower degrees have the effect of ‘smoothing’ the fit, acting as a sort of lowpass filter. The highest possible degree is one less than the number of data points to be fit, although a polynomial degree higher than about 7 does not add any significant information. Choosing the polynomial degree (as with most other aspects of signal processing) is a matter of experimenting to determine what works best.
3. To see the effect of polynomial degree, the easiest way is to experiment with the core MATLAB functions polyfit and polyval
with random data (the third argument to polyfit is the polynomial degree).
4. The 3rd-degree polynomial approximates the sine curve. Note that the 9th-degree polynomial fits the data more closely,
but does not at all approximate the sine curve (and also throws an error).
How to make a curve fit perfectly?
To get a perfect fit, we need to get the value of R-square exactly equal to 1. Using the Curve Fit Toolbox, increasing the degree of Polynomial to 8 we get a perfect fit,

What could be done to improve the cubic fit?
Initially, when ‘polyfit’ and ‘polyval’ command for cubic fit is used, we get a warning as “Equation is badly conditioned”. This
problem can be solved by checking the 'center and scale' the points of the curve fitting equation to get better results

Results:-

To improve the cubic fit, we can also split the data into many small data. We have split data into 4 subparts and wrote the
program.
% Write a program for curve fit using splitwise method
clc;
clear all;
close all;
% loading data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Spliting data
temperature1 = cp_data(1:800,1);
cp1 = cp_data(1:800,2);
temperature2 = cp_data(801:1600,1);
cp2 = cp_data(801:1600,2);
temperature3 = cp_data(1601:2400,1);
cp3 = cp_data(1601:2400,2);
temperature4 = cp_data(2401:3200,1);
cp4 = cp_data(2401:3200,2);
% considering cubic polynomial; Y = a*X^3 +b*X^2 + c*X +D
co_efficent1 = polyfit(temperature1,cp1,3);
predicted_cp1 = polyval(co_efficent1,temperature1);
co_efficent2 = polyfit(temperature2,cp2,3);
predicted_cp2 = polyval(co_efficent2,temperature2);
co_efficent3 = polyfit(temperature3,cp3,3);
predicted_cp3 = polyval(co_efficent3,temperature3);
co_efficent4 = polyfit(temperature4,cp4,3);
predicted_cp4 = polyval(co_efficent4,temperature4);
% plotting results
plot(temperature,cp,'linewidth',2,'color','b')
hold on
plot(temperature1,predicted_cp1,'linewidth',2,'color','r')
hold on
plot(temperature2,predicted_cp2,'linewidth',2,'color','r')
hold on
plot(temperature3,predicted_cp3,'linewidth',2,'color','r')
hold on
plot(temperature4,predicted_cp4,'linewidth',2,'color','r')
xlabel('Temparature [K]');
ylabel('Cp [KJ/K-mol K]');
axis([0 4000 900 1400]);
legend('Original dataset','Curve Fit data','Location','northwest')
Output :-

Here, we can observe that by using splitwise, we got a better cubical fit as compared to earlier cubical fit results.
Errors:-
No errors were observed while running the MATLAB program.
Conclusion:-
1. There are many methods that can be used to make the cure fit properly,
2. We need to have high order polynomial equations to achieve the best fit.
3. By splitting the data we get a better fit, more the number of splits, the better the output results.
References :-
1. https://in.mathworks.com/matlabcentral/answers/316510-how-plot-the-best-fit-curve
2. https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
3. https://en.wikipedia.org/wiki/Curve_fitting