All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: Code to fit Linear and Cubic Polynomial for the given Cp vs Temperature Data Using MATLAB. INTRODUCTION: CURVE FITTING: The process of finding out a possible function or creating a curve that has a best fit characteristics to a series of data set points is referred to as CURVE FITTING. This leads to creating an equation…
Vyshakh Raju
updated on 20 Feb 2020
AIM:
Code to fit Linear and Cubic Polynomial for the given Cp vs Temperature Data Using MATLAB.
INTRODUCTION:
CURVE FITTING:
The process of finding out a possible function or creating a curve that has a best fit characteristics to a series of data set points is referred to as CURVE FITTING. This leads to creating an equation that might be used to find out any data point along the curve. Hence Curve Fitting helps in predicting a future result with help of currently available data set. A best fit is obtained which is having a negligible value of error, where error is the difference in actual data and data that is obtained from curve fitting at a desired point.
CODE:
%Curve Fitting A Given Data Set
clear all
close all
clc
%Loading the data set.
DATA = load('data');
%Extracting values from data
Temp = DATA(:,1);
cp_Actual = DATA(:,2);
a = 1;
figure (a)
%plot original Curve
plot(Temp,cp_Actual,'r','linewidth',3')
grid on
title(' Cp Actual vs Temperature ')
xlabel('Temperature [k]','color','b')
ylabel('Specific Heat [kJ/kmol*K]','color','b')
legend('ACTUAL DATA SET')
%Linear Polynomial (Ax + b = 0)
Coeff_Linear = polyfit(Temp,cp_Actual,1);
cp_Lfit = polyval(Coeff_Linear,Temp);
a=a+1;
figure(a)
%plot linear fit with actual data set
plot(Temp,cp_Actual,'r','linewidth',3')
hold on
grid on
plot(Temp,cp_Lfit,'color','b','linewidth',3)
title(' Cp (Linear Fit) vs Temperature ')
xlabel('Temperature [k]','color','b')
ylabel('Specific Heat [kJ/kmol*K]','color','b')
legend('ACTUAL CURVE','LINEAR FIT CURVE')
%Parameters for fitness of curve
%error = y(i)-f(x(i))
%f(x) = cp_Lfit
%y = cp_Actual
error_linear = cp_Actual - cp_Lfit;
SSE_Lfit = sum(error_linear.^2);
SSR_Lfit = sum((cp_Lfit - mean(cp_Lfit)).^2);
SST_Lfit = SSR_Lfit+SSE_Lfit;
RSq_Lfit = (SSR_Lfit/SST_Lfit);
RMSE_Lfit = (SSE_Lfit/3200).^0.5 ;
if RSq_Lfit >= 0.95
x = {'Good Fit Since R^2 Value is greater than 0.95.'};
else
x = {'Less Effective fit.'};
end
%Cubic Polynomial (Ax + b = 0)
%Normalising temperature value( X value)
NTemp = (Temp-mean(Temp))./std(Temp);
Coeff_Cubic = polyfit(NTemp,cp_Actual,3);
cp_Cfit = polyval(Coeff_Cubic,NTemp);
a = a+1;
figure(a)
%plot Cubic fit with actual data set
plot(Temp,cp_Actual,'r','linewidth',3')
hold on
grid on
plot(Temp,cp_Cfit,'--','color','k','linewidth',3)
title(' Cp (Cubic Fit) vs Temperature ')
xlabel('Temperature [k]','color','b')
ylabel('Specific Heat [kJ/kmol*K]','color','b')
legend('ACTUAL CURVE','CUBIC FIT CURVE')
%Parameters for fitness of curve
%error = y(i)-f(x(i))
%f(x) = cp_Cfit
%y = cp_Actual
error_Cubic = cp_Actual - cp_Cfit;
SSE_CFit = sum(error_Cubic.^2);
SSR_CFit = sum((cp_Cfit - mean(cp_Cfit)).^2);
SST_CFit = SSR_CFit+SSE_CFit;
RSq_CFit = (SSR_CFit/SST_CFit);
RMSE_CFit = (SSE_CFit/3200).^0.5;
if RSq_CFit >= 0.95
y = {'Good Fit Since R^2 Value is greater than 0.95.'};
else
y = {'Less Effective fit.'};
end
disp(' SOME POINTS TO REMEMBER WHILE CURVE FITTING')
disp('R^2 = 0 , Worst Possible Fit')
disp('R^2 = 1 , Best Possible Fit')
disp('Fit with R^2 > 0.95 assumed to be a good Fit')
type = {'Linear';'cubic'};
rsquare = [RSq_Lfit;RSq_CFit];
fitness = [x;y];
OUPUT = table(type,rsquare,fitness,'VariableNames',{'TYPE','R_Square','Fitness'})
a = a+1;
figure (a)
plot(Temp,cp_Actual,'color','r','linewidth',3')
hold on
grid on
plot(Temp,cp_Cfit,'--','color','k','linewidth',3)
hold on
plot(Temp,cp_Lfit,'color','b','linewidth',3)
title(' COMPARISON ')
xlabel('Temperature [k]','color','b')
ylabel('Specific Heat [kJ/kmol*K]','color','b')
legend('ACTUAL CURVE','CUBIC FIT CURVE','LINEAR FIT CURVE')
Code Explanation:
Line 7 : The file containing data is loaded to the program using load command
Line 10 - 11 : The set of value required are extracted from the file
Line 14 -22 : The given data set is plotted for a reference.
Line 24 - 26 : For obtaining a linear fit curve, the coefficients and values associated is being calculated using polyfit and polyval commands.
Line 28- 38 : From the data set obtained using the poynomial functions of order 1(Linear function Ax+B = 0 ), the linear fit curve is plotted along with the actual data curve.
Line 44 - 49 : The parameters required for checking the fitness of the obtained curve is calculated.
Line 50 - 54 : Since for a curve with value of R^2 above 0.95 is accounted to be as a best fit curve, using an if else loop , an ouput is generated whether the fit is good or not.
Line 58 : Inorder to prevent a higher order polynomial from being badly conditioned, the value of temperature is normalised before we obtain the higher order ploynomial coefficients and associated data points .
Line 58 - 59 : For obtaining a Cubic fit curve, the coefficients and values associated is being calculated using polyfit and polyval commands.
Line 61 -71 : From the data set obtained using the poynomial functions of order 3(Cubic function Ax^3+Bx^2+Cx+D = 0 ), the linear fit curve is plotted along with the actual data curve.
Line 77 - 82: The parameters required for checking the fitness of the obtained curve is calculated.
Line 83 - 87: Since for a curve with value of R^2 above 0.95 is accounted to be as a best fit curve, using an if else loop , an ouput is generated whether the fit is good or not.
Line 89 - 91: displaying some points to remember while curve fitting
Line 93 - 96: Defining variables inorder to display the required results as a table with help of table command.
Line 98 - 109 : Creating a plot to compare the actual, Linear fit and Cubic Fit curves.
PARAMETERS FOR FITNESS CHECK:
Error
Error = y(i) - f(x(i))
y(i) = value of actual data set
f(x(i)) = value obtained from the function of fit
R^2
This is the square of relation between the predicted value and original data set. Value of R^2 determines how well the curve fits the original data set.
The higher the value of R^2, the better is the fit.
R-squared is always between 0% and 100% ( 0 and 1)
R^2 = 0 , defines a worst fit
R^2 = 1, defines a perfect fit.
R^2 > 0.95 > 1 , defines a best fit.
PERFECT FIT CURVE is a curve with R^2 = 1 , each point in the original data set and the predicted values have the same value (or the curve is same for both original and predicted data sets).
RESULTS:
OUTPUT WINDOW
PLOTS OBTAINED
1) Actual DATA SET
2) Linear Fit Curve
3) Cubic Fit Curve
4)Comparison Plot
From the linear and cubic fit curves, it is clear that the cubic fit curve matches more with the actual data set curve. Hence we could say that Cubic fit curve is a best fit when compared with a Linear fit curve.
The R^2 value of Cubic fit curve approximates to 0.99 which is close to a perfect fit curve.
The R^2 value of Linear fit curve approximates to 0.92 which describes that the fit is not enough to be a good fit to the original data set.
IMPROVING CUBIC FIT:
A curve fit can be improved either by changing order of the function or by splitting the data range into sections or by using different polynomials to define different data range.
Inorder to improve our curve fit, let us split our temperture range into different stages which inturn divides our whole data set.
CODE:
%To Obtain a Smooth cubic fit
%Splitting the data range
clear all
close all
clc
%Loading the data set.
DATA = load('data');
%Extracting values from data
Temp = DATA(:,1);
cp_Actual = DATA(:,2);
a = 1;
%Cubic Polynomial (Ax^3 + Bx^2 + Cx + D = 0)
%Normalising temperature value( X value)
NTemp = (Temp-mean(Temp))./std(Temp);
Coeff_Cubic = polyfit(NTemp,cp_Actual,3);
cp_Cfit = polyval(Coeff_Cubic,NTemp);
%splitting
T1 = NTemp((1:500),1);
T2 = NTemp((500:1600),1);
T3 = NTemp((1600:2400),1);
T4 = NTemp((2400:3200),1);
cp_Actual1 = cp_Actual((1:500),1);
cp_Actual2 = cp_Actual((500:1600),1);
cp_Actual3 = cp_Actual((1600:2400),1);
cp_Actual4 = cp_Actual((2400:3200),1);
%plot Cubic fit with actual data set
for i = 1:length(T1)
Coeff_Cubic1 = polyfit(T1,cp_Actual1,3);
cp_Cfit1 = polyval(Coeff_Cubic1,T1);
end
for i = 1:length(T2)
Coeff_Cubic2 = polyfit(T2,cp_Actual2,3);
cp_Cfit2 = polyval(Coeff_Cubic2,T2);
end
for i = 1:length(T3)
Coeff_Cubic3 = polyfit(T3,cp_Actual3,3);
cp_Cfit3 = polyval(Coeff_Cubic3,T3);
end
for i = 1:length(T4)
Coeff_Cubic4 = polyfit(T4,cp_Actual4,3);
cp_Cfit4 = polyval(Coeff_Cubic4,T4);
end
a = 1;
figure(a)
grid on
plot(NTemp,cp_Actual,'color','k','linewidth',6)
hold on
plot(T1,cp_Cfit1,'--','color','g','linewidth',2)
hold on
plot(T2,cp_Cfit2,'--','color','g','linewidth',2)
hold on
plot(T3,cp_Cfit3,'--','color','g','linewidth',2)
hold on
plot(T4,cp_Cfit4,'--','color','g','linewidth',2)
xlabel('Temperature [k]','color','b')
ylabel('Specific Heat [kJ/kmol*K]','color','b')
legend('ACTUAL CURVE','SPLIT CUBIC FIT CURVE')
title('SPLITTING CUBIC FIT CURVE')
error_Cubic1 = cp_Actual1 - cp_Cfit1;
SSE_CFit1 = sum(error_Cubic1.^2);
SSR_CFit1 = sum((cp_Cfit1 - mean(cp_Cfit1)).^2);
SST_CFit1 = SSR_CFit1+SSE_CFit1;
RSq_CFit1 = (SSR_CFit1/SST_CFit1);
RMSE_CFit1 = (SSE_CFit1/800).^0.5;
error_Cubic2 = cp_Actual2 - cp_Cfit2;
SSE_CFit2 = sum(error_Cubic2.^2);
SSR_CFit2 = sum((cp_Cfit2 - mean(cp_Cfit2)).^2);
SST_CFit2 = SSR_CFit2+SSE_CFit2;
RSq_CFit2 = (SSR_CFit2/SST_CFit2);
RMSE_CFit2 = (SSE_CFit2/800).^0.5;
error_Cubic3 = cp_Actual3 - cp_Cfit3;
SSE_CFit3 = sum(error_Cubic3.^2);
SSR_CFit3 = sum((cp_Cfit3 - mean(cp_Cfit3)).^2);
SST_CFit3 = SSR_CFit3+SSE_CFit3;
RSq_CFit3 = (SSR_CFit3/SST_CFit3);
RMSE_CFit3 = (SSE_CFit3/800).^0.5;
error_Cubic4 = cp_Actual4 - cp_Cfit4;
SSE_CFit4 = sum(error_Cubic4.^2);
SSR_CFit4 = sum((cp_Cfit4 - mean(cp_Cfit4)).^2);
SST_CFit4 = SSR_CFit4+SSE_CFit4;
RSq_CFit4 = (SSR_CFit4/SST_CFit4);
RMSE_CFit4 = (SSE_CFit4/800).^0.5;
R_Square_split = (RSq_CFit1+RSq_CFit2+RSq_CFit3+RSq_CFit4)/4;
disp('R^2 of Split Cubic Fit')
disp (R_Square_split)
PLOT:
COMPARING: The Split Cubic curve with Normal Cubic Curve
From the plot it is clear that the Split cubic curve almost matches the Actual data curve. This gives a better fit than that obtained from a normal Cubic fit curve. Hence splitting the data range also helps in smoothening and obtaining a more best fit curve for a given data set.
OUTPUT:
The R^2 value for the split curve obtained is 1 which implies that the SPLIT CUBIC CURVE is a Perfect fit for the given data set.
HOW TO MAKE A CURVE FIT PERFECTLY?
For curve to be perfect, the error between predicted value and actual value should be 0 or negligible. The value R^2 = 1 describes a perfect fit, where the real points and predicted points are same.
HOW TO GET A BEST FIT?
A best fit can be obtained either by changing order of the function or by splitting the data range into sections or by using different functions to define different data range. Value of R^2 >0.95 >1 describes a best fit.
What could be done to improve the cubic fit?
Inorder to improve the cubic fit, the range of data sets can be divided into different sections, ie, splitting of data range . Each data range can then be solved and a more perfect it can be obtained than that obtaine from a normal cubic curve.
CONCLUSION:
Curve fitting is the construction of a curve or a function which is having the best fit to a random series of data points provided. The cubic curve constructed turns out to be a best fit for the given data set when compared to the Linear fit curve. Further, on splitting the data set for a cubic curve, we could obtain a curve that fits the data set perfectly. Hence for obtaining a best fit for a data set, changing the order and splitting data range might help in the Curve Fitting process. Even using different polynomial for different range of data may also be performed to produce a best fit.
REFERENCE:
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
http://www.synergy.com/Tools/curvefitting.pdf
http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/math_anal/datafu14.html
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Radar Mast & Final Assembly of Yacht
Modelling a Yacht with Solidworks OBJECTIVE: To model different parts of a Yacht To assemble each parts of yacht to create the full model INTRODUCTION: This project is focused in partwise modelling of the Yacht and then assembly of the parts together inorder to obtain the full Yacht model DESIGN METHODOLOGY:…
14 Jun 2021 10:52 AM IST
Photo Realistic Rendering
MODELLING OF AN RT66 AMERICAN CHOPPER OBJECTIVE: To model each parts of an RT66 Chopper Assemble the each parts of Chopper to obtain full Scale model of RT66 Chopper Render the Chopper model to a realistic View INTRODUCTION: This projuct is focused on modelling of an American Chopper model part wise,…
07 Jun 2021 04:07 PM IST
Advanced Sheet Metal Design Using NX Cad Challenge_7_ Metal bracket-II
DESIGNING A METAL BRACKET WITH NX SHEET METAL APPLICATION OBJECTIVE: To create a Metal bracket with respect to the given 2-D Drawing with NX sheet Metal Application. INTRODUCTION: This work focuses on designing a Metal Bracket with NX Sheet metal Application with respect to a given 2-D Drawing for dimensions.…
30 May 2021 01:35 PM IST
Advanced Sheet Metal Design Using NX Cad Challenge_6_Bracket
BRACKET DESIGN USING NX SHEET METAL APPLICATION OBJECTIVE: To design a Bracket with the specified dimensions and contour using NX CAD Sheet Metal Application. INTRODUCTION: This work is focused to design and create a Bracket as per specified dimensions and contour in the 2-D drawing. DESIGN METHODOLOGY: PROCEDURE:…
30 May 2021 06:34 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.