curve fitting
AIM:To fit a linear and cubic polynomial for the given cp data.
THEORY:
curve fittng: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.
4 quantities that help us measure the goodness of a fit.
1.the sum of sqaures due to error(sse)
2.R sqaure
3.adjuisted R sqaure
4.root mean sqaured error
sse=`sum_(i=1)^n`error(i)^2
R^2=`(ssr)/(sst)`
sse=`sum_(i=1)^n(f(x(i))-mean)^2`
rmse=`sqrt((sse)/n`
1.HOW TO MAKE PERFECT FIT?
To make the curve perfect,we should use the formula of errror, or we should use the splitting method ,or increase in the order.
2.HOW TO GET THE BEST FIT?
By finding the r sqaure value we can findout the best fit for curve fitting ,if r sqaure value is greater than 0.95 then it is considered as best fit.
3.WHAT COULD BE DONE TO IMPROVE THE CUBIC FIT?
To improve cubic fit the data is to be spitted and then curve fitting is done.
CODING FOR LINEAR AND CUBIC CURVE FITTING:
clear all
close all
clc
% preparing the data
cp_data=load('data');
temperature =cp_data(:,1);
cp =cp_data(:,2);
% inputs for linear fit
co_effs1=polyfit(temperature,cp,1);
pred_cp1= polyval(co_effs1,temperature);
%inputs for cubic fit
co_effs2=polyfit(temperature,cp,3);
pred_cp2= polyval(co_effs2,temperature);
x=mean(cp);
%calculate r^2 values for linear fit
ssr1=0;
sse1=0;
for i=1:length(cp);
ssr1=sum((pred_cp1-x).^2);
sse1=sum((cp-pred_cp1).^2);
end
r_squared1=ssr1/(sse1+ssr1);
rmse=(sse1/length(cp))^0.5;
%calculate r^2 values for cubic fit
ssr2=0;
sse2=0;
for j=1:length(cp)
ssr2=sum((pred_cp2-x).^2);
sse2=sum((cp-pred_cp2).^2);
end
r_squared2=ssr2/(sse2+ssr2);
rmse=(sse2/length(cp))^0.5;
% compare my curve fit with original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,pred_cp1,'linewidth',1,'color','g')
hold on
plot(temperature,pred_cp2,'linewidth',1,'color','r')
xlabel('temperature [k]')
ylabel('specific heat [kj/kmol-k]')
legend('original dataset','linear','cubic')
CURVE FITTNG WITHOUT SPLITTING:
CODING AFTER SPLITTING OF CURVES:
clear all
close all
clc
cp_data=load ('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
a=1;
b=400;
% plotting
hold on
plot(temperature,cp)
for i=1:8
co_effs(i,:)=polyfit(temperature(a:b),cp(a:b),2);
pred_cp(i,:)=polyval(co_effs(i,:),temperature(a:b));
pred_cp1(a:b)=pred_cp(i,:);
plot(temperature(a:b),pred_cp(i,:),'color','r')
hold on
a=b+1;
b=b+400;
end
ssr=0;
sse=0;
sum=0;
for j=1:length(cp)
sse=sse+(cp(j)-pred_cp1(j))^2;
sum=sum+cp(j);
end
mean=sum/length(cp);
for j=1:length(cp);
ssr=ssr+(pred_cp1(j)-mean)^2;
end
sst=ssr+sse;
r_square=ssr/sst;
fprintf('splitting of quadratic curve:%f',r_square)
xlabel('temperature')
ylabel('specipic heat')
legend('original','curvefit')
CONCLUSION: