Create a curve fitting performance with goodness of fit criteria in MATLAB
AIM
To create the predicted curve for fiitting into the original curve in perfectly by using polynomial equations in MATLAB.
INTRODUCTION
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. Curves such as parabola and hyperbola are used in architecture to design arches in buildings. The purpose of curve fitting is to specify an appropriate function and adjust its parameters in a way that it matches, as close as possible, an experimental or historical data set. Fitting functions can be derived by applying regression techniques, interpolation, and spline curves.
Curve fiitting is help to predicting future values at specific point. For example, predicting the population growth at next year, predicting share markets scenario and so on.
Robust curve fitting
This figure represents the curve fitting the black color curve is raw data curve, the red color curve is predicted curve by using polynomial curve. To increase the degree of polynomial, the red colour curve will exactly fit into the black colour curve. This is called curve fitting.
OBJECTIVES
1) To create a program for making a linear and cubic curve with corresponding polynomial equation and compare with original raw data.
2) Split the raw data and to create a program for comparing the predicted and original raw data in both linear and cubic polynomial equation.
3) Check it goodness of fit criteria for individual polynomial equation.
4) Plot results should be comparing to both predicted and original curve.
5) Measuring goodness of fit criteria.
WHAT IS POLYNOMIALS
A ploynomial is an expression of more than one algebraic terms, especially of several terms that contains different powers of the same variables.
Types of polynomials equation
Linear equation
`Y = a(T)+b`
It plotted on a graph, it's actually in exactly straight line. Typically, it is directly proportional to indenpendent variables.
Quadratic equation
`Y = a(T)^2 + b(T) + c`
It plotted on a graph, it's looks parabolic curve.
Cubic equation
`Y = a(T)^3 +b(T)^2 + c(T) + d`
It plotted on a graph, it's looks cubic curve.
n th order equation
`Y = P1(T)^(n) + P2(T)^(n-1) + ......+Pn-1(T)+Pn`
It plotted on a graph, it's looks nonlinear curve.
QUALITIES FOR FINDING THE GOODNESS OF FIT CRITERIA
1) Sum of squares due to error (SSE)
2) R - square
3) Adjustive R - square
4) Root mean squared error (RMSE)
Sum of squared error
SSE is the sum of squared deviations of actual value from the predicted values. The SSE value is 0, the predicted curve fit is exactly fitted overlapped the original raw data curve. The SSE value is too high, that means the predicted value does not correct fit. So, we increase the ploynomial when the curve is fitted over the raw data curve.
Formula to finding the sum of squared error.
`SSE = sum_(i=1)^n (Y(i) - f(x(i)))^2`
Sum of squared regression
SSR is the sum of squared of predicted values from the mean value. The SSR value are 0 or high, same previous scenario is held on in sum of squared regression.
Formula to finding the sum of squared regression.
`SSR = sum_(i=1)^n (f(x(i))-Mean)^2`
Total sum of square
The total sum of squares is the addition of sum of sqaured error and sum of squared regression.
`SST = SSE +SSR`
R - Square
R - square value is a sum of squared regression upon total sum of square
`R ^2 = (SSR)/(SST)`
The value of R square is 0, it is very bad fit. The value of R square is 1, it is very good fit.
Acceptable R square values
0.3 < r < 0.5 this value is generally considered a weak or low effect size.
0.5 < r< 0.7 this value is generally considered a moderate effect size.
r > 0.7 this value is generally considered a strong effect size and it is recommended for best R square values.
Adjusted R - square
The adjusted R-squared is a modified version of R-squared that has been adjusted for the number of predictors in the model. The adjusted R-squared increases only if the new term improves the model more than would be expected by chance.
It depends on independent variables, it is negligible infinitesimal variation from R square.
`1 - [((1-R^2)(n-1))/(k-1)]`
Where,
n = Number of data points
k = Number of independent variables
Root mean square error
Root mean square error (RMSE) is the standard deviation of the residuals (predicted errors). Residuals are a measure of how far from the regression line data points are; RMSE is a measure of how spread out these residuals are. In other words, it tells you how concentrated the data is around the line of best fit.
`sqrt(((sum_(i=1)^2)(Y(i) - f(x(i)))^2)/n)`
`sqrt((SSE)/n)`
QUESTIONS AND ANSWERS
How to make a curve perfectly ?
Split the original data points to our conveniency, to apply the appropriate polynomial equation in individual subdivided data point ranges. When we use this method, the error will be reduced and curve fit make it in perfectly without going to increasing the degree of ploynomial.
In order to say curve is fit perfectly, that means a error is 0 in between gap of original raw data curve and predicted curve. The predicted curve exactly overlapped to the original raw data curve.
How to get the best fit ?
Initially, we set the some minimum tolerance from the predicted curve to original curve. The error will comes under the tolarances range that is called best fit.
We can fix the tolerannce range in any one of the goodness of fit criteria, it should satisfies the condition, assume that we get best fit.
The perfect fit is always a best fit, but the best fit is not a perfect fit.
What could be done to improve the cubic fit ?
Try splitting the raw data to apply the mathematical polynomial equation within the less degree of polynomials and calculate it.
PROGRAM TO FITTING THE CURVE WITHOUT SPLITTING THE ORIGINAL RAW DATA
clear all % Remove the all variables and corresponding values from work space tab
close all % Closing the all figures
clc % Clean the command window
% Preparing the raw data and import to matlab
Cp_data = load('data'); % Cp_data is actually name of original curve and file name is data
temperature = Cp_data(:,1); % All row and first column is temperature vector
Specific_heat = Cp_data(:,2); % All row and second column is Specific heat vector
% Create a predicted data points by using linear eqaution
% Linear_Cp = a*T+b % Linear equation polynomial
Linear_Coefficient = polyfit(temperature,Specific_heat,1) % By using the polyfit command it helps to predict the coefficient values for linear equation
Linear_Specific_heat = polyval(Linear_Coefficient,temperature) % Polyval command actually a calculate data point values in given polynomial
% Create a predicted data points by using cubic eqaution
% Cubic_Cp = a*T^3+b*T^2+c*T+d % Cubic equation polynomial
Cubic_Coefficient = polyfit(temperature,Specific_heat,3) % By using the ployfit command it helps to predict the coefficient values for cubic eqaution
Cubic_Specific_heat = polyval(Cubic_Coefficient,temperature) % Polyval command actually a calculate data point values in given polynomial
% Plotting the original raw data curve
figure(1)
plot(temperature,Specific_heat,'linewidth',3) % To plot the graph temperature in x axis and Specific heat in y axis
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
title('Original raw data curve')
% Compare my linear specific heat curve with the original data curve
figure(2)
plot(temperature,Specific_heat,'linewidth',3) % To plot the graph temperature in x axis and original specific heat in y axis in figure 2
hold on % Hold on command is helps to overlapping the upcomig the curve in same plot
plot(temperature,Linear_Specific_heat,'linewidth',3) % Overlapping in the graph in same plot temperature in x axis and linear specific heat in
y axis in same
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original curve','Linear curve')
title('Linear curve with original curve')
% Compare my cubic specific heat curve with the original data curve % In this figure same manner have do instead of Cubic specific heat at
linear specific heat place
figure(3)
plot(temperature,Specific_heat,'linewidth',3)
hold on
plot(temperature,Cubic_Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original curve','Cubic curve')
title('Cubic curve with original curve')
% Compare both Linear and cubic curve with the original value
figure(4)
plot(temperature,Specific_heat,'linewidth',3)
hold on
plot(temperature,Linear_Specific_heat,'linewidth',3)
plot(temperature,Cubic_Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Curve','Linear curve','Cubic curve')
title('Both linear and cubic curve with original curve')
% Finding the goodness of fit criteria characteristics for best curve fit
for i = 1:length(Specific_heat) % Iterating the original specific heat from initial value to final value in orderly
SSE_1(i) = (Specific_heat(i) - Linear_Specific_heat(i))^2
SSE_2(i) = (Specific_heat(i) - Cubic_Specific_heat(i))^2
SSR_1(i) = (Linear_Specific_heat(i) - mean(Specific_heat))^2
SSR_2(i) = (Cubic_Specific_heat(i) - mean(Specific_heat))^2
end
SSE_Linear = sum(SSE_1) % Sum the all first sum of squared error is belongs to sum of squared error in linear equation
SSE_Cubic = sum(SSE_2) % Sum the all second sum of squared error is belongs to sum of squared error in cubic equation
SSR_Linear = sum(SSR_1) % Sum the all first sum of squared regression is belongs to sum of squared regression in linear equation
SSR_Cubic = sum(SSR_2) % Sum the all second sum of squared regression is belongs to sum of squared regression in cubic equation
SST_Linear = SSE_Linear + SSR_Linear % Addition of both sum of square error and regression in linear is called total sum of error
SST_Cubic = SSE_Cubic + SSR_Cubic
R_Square_Linear = (SSR_Linear)/(SST_Linear) % The SSR linear upon SST linear is a R sqaure for linear polynomial
R_Square_Cubic = (SSR_Cubic)/(SST_Cubic) % The SSR cubic upon SST cubic is a R sqaure for cubic polynomial
n = 3200 % Number of data points
k = 3200 % Number of independent variables
R_Square_Adjustable_Linear = 1 - (((1-R_Square_Linear)*(n-1))/(k-1))% Formula for calculating the R square adjustable in linear curve
R_Square_Adjustable_Cubic = 1 - (((1-R_Square_Cubic)*(n-1))/(k-1)) % Formula for calculating the R square adjustable in cubic curve
RMSE_Linear = ((SSE_Linear)/n)^0.5 % Root mean sqaure error for linear curve with original curve
RMSE_Cubic = ((SSE_Cubic)/n)^0.5 % Root mean sqaure error for linear curve with original curve
EXPLANATION
1) Load the raw data file into the matlab and mention the temperature and specific data, by using this data to plot the curve in graph.
2) Finding the coefficients for both linear and cubic equation by the polyfit command.
3) By using the polyval command for finding the polynomial values. In order to finding the dependent variable we can use independent variable. The finded specific heat values are only predicted values. If we make curve perfectly to increase the degree of polynomial or split the raw data and substitute the same polynomial equation.
4) Same manner for create the cubic curve. But one thing is increase the degree of polynomial before calculate the coefficients.
5) To plot the individual curve and combined curve.
6) For loop command is iterating the( i ) value in orderly, after that ( i ) will passing into functions.
7) In order finding goodness of fit criteria, we use four quanlities such as sum of squared error, sum of squared regression, R squared error and R squared adjustable error.
8) We have individual formula for calculating the goodness of fit criteria.
9) Another way to finding the goodness of fit criteria by use inbuild command is cftool.
10) Noted the error and it satisfies the goodness of fit criteria, the curve is good.
11) We check and compare error both linear and cubic curve and finalize the good one.
REASONS FOR USING A CERTAIN ORDER OF POLYNOMIAL
1) Predicted curve should be an perfectly fit over the original raw data curve.
2) When the curve is perfectly, easy to predict the curve will goes in function of independent variable.
3) Results are accurate with some infinitesimal error.
RESULTS FOR WITHOUT SPLITTING THE POLYNOMIAL
Plot a curve by using original raw data
It represents specific heat is proportion to function of temperature , some certain cases pressure also involves to the specific heat variations.
Plot a linear curve with original curve
Easy to says, it error deviation is high so should not be a correct curve fit.
Plot a cubic curve with original curve
The cubic curve is best fit , but not a perfect fit.
Plot a both linear and cubic curve with original curve
Workspace
Showing the goodness of fit for predicted linear curve and original curve
Showing the goodness of fit for predicted cubic curve and original curve
Calculating goodness of fit by using individual formula
PROGRAM TO FITTING THE CURVE WITH SPLITTING ORIGINAL RAW DATA
clear all
close all
clc
% Preparing the data
Cp_data = load('data');
temperature = Cp_data(:,1);
Specific_heat = Cp_data(:,2);
% Splitting the raw data to create a curve by using linear equation
% Linear_Cp = a*T+b % To splitting the raw data and finding the coefficient individual range by using linear equation
Linear_Coefficient_1 = polyfit(temperature(1:800),Specific_heat(1:800),1)
Linear_Coefficient_2 = polyfit(temperature(801:1600),Specific_heat(801:1600),1)
Linear_Coefficient_3 = polyfit(temperature(1601:2400),Specific_heat(1601:2400),1)
Linear_Coefficient_4 = polyfit(temperature(2401:3200),Specific_heat(2401:3200),1)
% To finding the polynomial values in individual splitting range
Linear_Specific_heat_1 = polyval(Linear_Coefficient_1,temperature(1:800))
Linear_Specific_heat_2 = polyval(Linear_Coefficient_2,temperature(801:1600))
Linear_Specific_heat_3 = polyval(Linear_Coefficient_3,temperature(1601:2400))
Linear_Specific_heat_4 = polyval(Linear_Coefficient_4,temperature(2401:3200))
Linear_Specific_heat = [Linear_Specific_heat_1; Linear_Specific_heat_2; Linear_Specific_heat_3; Linear_Specific_heat_4] % Combine the calcualted polynomial values and to make an single column vector
% Splitting the raw data to create a curve by using cubic equation
% Cubic_Cp = a*T^3+b*T^2+c*T+d % To splitting the raw data and finding the coefficient individual range by using cubic curve
Cubic_Coefficient_1 = polyfit(temperature(1:800),Specific_heat(1:800),3)
Cubic_Coefficient_2 = polyfit(temperature(801:1600),Specific_heat(801:1600),3)
Cubic_Coefficient_3 = polyfit(temperature(1601:2400),Specific_heat(1601:2400),3)
Cubic_Coefficient_4 = polyfit(temperature(2401:3200),Specific_heat(2401:3200),3)
Cubic_Specific_heat_1 = polyval(Cubic_Coefficient_1,temperature(1:800))
Cubic_Specific_heat_2 = polyval(Cubic_Coefficient_2,temperature(801:1600))
Cubic_Specific_heat_3 = polyval(Cubic_Coefficient_3,temperature(1601:2400))
Cubic_Specific_heat_4 = polyval(Cubic_Coefficient_4,temperature(2401:3200))
Cubic_Specific_heat = [Cubic_Specific_heat_1;Cubic_Specific_heat_2;Cubic_Specific_heat_3;Cubic_Specific_heat_4]
% Plot the original raw data curve
figure(1)
plot(temperature,Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
title('Original raw data curve')
% Compare the linear specific heat curve with the original data curve
figure(2)
plot(temperature,Specific_heat,'linewidth',3)
hold on
plot(temperature,Linear_Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original curve','Linear curve')
title('Linear curve with original curve')
% Compare the cubic specific heat curve with the original data curve
figure(3)
plot(temperature,Specific_heat,'linewidth',3)
hold on
plot(temperature,Cubic_Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original curve','Cubic curve')
title('Cubic curve with original curve')
% Compare both Linear and cubic curve with the original raw data curve
figure(4)
plot(temperature,Specific_heat,'linewidth',3)
hold on
plot(temperature,Linear_Specific_heat,'linewidth',3)
plot(temperature,Cubic_Specific_heat,'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Curve','Linear curve','Cubic curve')
title('Both linear and cubic curve with original curve')
% Finding the goodness of fit criteria characteristics of curve fitting
for j = 1:length(Specific_heat)
SSE_1(j) = (Specific_heat(j) - Linear_Specific_heat(i))^2
SSE_2(j) = (Specific_heat(j) - Cubic_Specific_heat(i))^2
SSR_1(j) = (Linear_Specific_heat(j) - mean(Specific_heat))^2
SSR_2(j) = (Cubic_Specific_heat(j) - mean(Specific_heat))^2
end
SSE_Linear = sum(SSE_1)
SSE_Cubic = sum(SSE_2)
SSR_Linear = sum(SSR_1)
SSR_Cubic = sum(SSR_2)
SST_Linear = SSE_Linear + SSR_Linear
SST_Cubic = SSE_Cubic + SSR_Cubic
R_Square_Linear = (SSR_Linear)/(SST_Linear)
R_Square_Cubic = (SSR_Cubic)/(SST_Cubic)
n = 3200
k = 3200
R_Square_Adjustable_Linear = 1 - (((1-R_Square_Linear)*(n-1))/(k-1))
R_Square_Adjustable_Cubic = 1 - (((1-R_Square_Cubic)*(n-1))/(k-1))
RMSE_Linear = ((SSE_Linear)/n)^0.5
RMSE_Cubic = ((SSE_Cubic)/n)^0.5
EXPLANATION
1) Load the raw data file into the matlab and import which is temperature and specific heat.
2) We can split the raw data in equal distances range individually.
3) By using polyfit command for calculating the coefficient of all splitting raw data by using the linear and cubic polynomial equation in individual sections.
4) By using polyval command to calculating the predicted value for finding the predicted data points.
5) Combine the subdivided range and to make a array in set of order.
6) Same process to calculating the goodness of fit in curve fitting both linear and cubic.
7) To compare with split wise polynomial and without split wise polynomial and finalize which one is better.
8) For loop command is iterating the( i ) value in orderly, after that ( i ) will passing into functions.
9) In order finding goodness of fit criteria, we use four quanlities such as sum of squared error, sum of squared regression, R squared error and R squared adjustable error.
10) For finding errors we have four qualities previously mentioned.
RESULTS FOR WITH SPLITTING THE POLYNOMIAL
Plot a splitwise linear curve with original curve
In this curve is better than the previously plotted without splitting the linear curve. The error has to minimized. This is best fit compare to without splitting. linear curve
Plot a cubic curve with original curve
This curve has to be consider perfect fit, the goodness of fit criteria condition were satisfies the error.
Plot a both linear and cubic curve with original curve
Workspace for splitwise polynomial
Calculating goodness of fit by using individual formula
ERRORS FACED WHILE DOING IN PROGRAMMING
The command window showing the error, i use colon instead of semi colon in line 39. After that correct that mistake.
The command window showing the error, i forget the parenthesis at line 21. After that, i check the code and recorrect it.
CONCLUSION
Finally, to make plot different varieties of curves and also calculated the goodness of fit individually by using some quanlity measures parameters. The splitwise cubic fit is better than without splitwise curve, it is a best fit with minimum errors.
In split wise cubic curve R square error is infinitesimal deviation from 1 . So it consider perfect fit in this case.
REFERANCE
1) https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
2) https://in.mathworks.com/help/curvefit/curve-fitting.html