Curve Fitting for a data given between Temperature vs Specific Heat.
AIM:
To plot a curve fit for a raw data given between Temperature and Specific heat.
OBJECTIVE:
To generate a curve which fits best for the given raw data of temperature and specific heat.
THEORY:
Curve Fitting is a curve plotted to generate an approximation curve for a given set of data points for better understanding of the variation of the statistics. here an issue arises as approximation can vary immensely and from point to point,hence finding the Best Curve Fit is very important to give the end user to understand the variation clearly. the accuracy of the curve fit can be varied by using Polynomial equations. The higher the order of the polynomial equation, greater the accuracy of the curve fit. usually a quadratic or a cubic fit produces a good fit. another way of improving the fit can be done by splitting the data.
MATHEMATICAL FORMULAE:
we know that a curve fit is good when the R square values are usually above 0.95.
in our program we have acheived
R square for linear fit = 0.9999
R square for cubic fit = 0.9967
which is good enough and is highlighted below. the other data can also be seen below.
Program to generate the curve fit.
close all
clc
%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%linear curve fit
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
%cubic curve fit
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
%calculating R suare for linear fit
SSR1=0;
SSE1=0;
for i=1:length(cp)
SSR1= SSR1 + (predicted_cp1(i)-x).^2;
SSE1= SSE1 + (cp(i)- predicted_cp1(i)).^2;
end
Rsquare1 = SSR1/(SSR1+SSE1)
RMSE1 = (SSE1/length(cp)).^0.5
%calculating R suare for cubic fit
SSR2=0;
SSE2=0;
for i=1:length(cp)
SSR2= SSR2 + (predicted_cp2(i)-x).^2;
SSE2= SSE2 + (cp(i)- predicted_cp2(i)).^2;
end
Rsquare2 = SSR2/(SSR2+SSE2)
RMSE2 = (SSE2/length(cp)).^0.5
%comparing curvefit with original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
plot(temperature,predicted_cp2,'linewidth',3,'color','g')
xlabel('temperature[k]');
ylabel('specific_heat [KJ/Kmol-K]')
legend('original dataset','linear fit','cubic fit')
The Program to generate the best curve fit for the given data is displayed below.
close all
clc
%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
x=mean(cp)
%linear curve fit
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
predicted_cp_split = zeros(3200,1);
%split
a=1;
b=400;
for j= (1:8)
co_effs1_split = polyfit(temperature(a:b),cp(a:b),1);
predicted_cp_split(a:b) = polyval(co_effs1_split,temperature(a:b));
a=b+1;
b=b+400;
end
plot(temperature,cp,'linewidth',5)
hold on
plot(temperature,predicted_cp_split,'linewidth',3)
%calculating R square for linear fit for the split
SSR1=0;
SSE1=0;
for i=1:length(cp)/8
SSR1= SSR1 + (predicted_cp_split(i)-x).^2;
SSE1= SSE1 + (cp(i)- predicted_cp_split(i)).^2;
Rsquare1 = SSR1/(SSR1+SSE1)
RMSE1 = (SSE1/length(cp)).^0.5
end
xlabel('temperature[k]');
ylabel('specific_heat [KJ/Kmol-K]')
%cubic curve fit
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
plot(temperature,predicted_cp2,'linewidth',3)
legend('original dataset','split 1','cubic fit')
%calculating R square for cubic fit
SSR2=0;
SSE2=0;
for i=1:length(predicted_cp2)
SSR2= SSR2 + (predicted_cp2(i)-x).^2;
SSE2= SSE2 + (cp(i)- predicted_cp2(i)).^2;
Rsquare2 = SSR2/(SSR2+SSE2)
RMSE2 = (SSE2/length(cp)).^0.5
end
OUTPUTS:
The graph for the curve fit.
The graph for the best fit.