CURVE FIT OF LINEARLY AND CURVATURE DRAWING GRAPH USING MATLAB
AIM:
1:Code to fit a linear and cubic polynomial for the Cp data
2:Plot the linear and cubic fit curves along with the data points
3: The parameters used to measure the fitness characteristics for both the curves
THEORY:
It is a method in which designing the curve that has a mathematical function having n no. of polynomials and constriants, more the degree of function leads to more steaper curve, here we have some terminology which help to gives the final curvature stated below:
Now we have the programme which gives us a curve fit for that we have POLYFIT which is used for fitting the curve and we have the polyval which gives a predicted curve to be fitted ,suitable programme stated below:
clear all
close all
clc
%preparing the data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%plot
%curve fit
%cp=a*T^2+b*T+c
co_effs=polyfit(temperature,cp,2)
predicted_cp=polyval(co_effs,temperature);
%compare my curve fit with the original data
plot(temperature,cp,'linewidth',2)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[k]')
ylabel('Specific Heat[kj/kmol-k]')
legend('original dataset','curve fit')
hold off
now we have the procedure of drawing 2 curves one is straight and another is 2nd order curve for that we have used LEGEND which distinguish both the curves and the programmes are stated below:
clear all
close all
clc
p_cp=load('data')
temperature=p_cp(:,1)
cp=p_cp(:,2)
subplot(2,1,1)
co_eff1=polyfit(temperature,cp,1);
predicted_cp1=polyval(co_eff1,temperature);
plot(temperature,cp,'linewidth',3,'color','b')
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
legend('Actual','predicted')
%y=ax^2+bx+c
subplot(2,1,2)
co_eff2=polyfit(temperature,cp,3);
predicted_cp2=polyval(co_eff2,temperature);
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp2,'linewidth',3)
xlabel('temperature')
ylabel('specific heat')
legend('Actual','predicted')
s=sum(cp)
mean=s/length(cp)
for j=(1:length(cp))
ssr1=(predicted_cp1(j)-mean).^2
sse1=(cp(j)-predicted_cp1(j)).^2
end
sst1=ssr1+sse1;
r1=ssr1/sst1
for k=(1:length(cp))
ssr2=(predicted_cp2(k)-mean)^2
sse2=(cp(k)-predicted_cp2(k))^2
end
sst2=ssr2+sse2;
r2=ssr2/sst2
and finally we have to split the curve to get a straight line when clubbing all those straight line we will get a curvature for that we have tp split the whole predicted values and polyfits into 8 parts and the programme stated below for it as well:
clear all
close all
clc
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2)
plot(temperature,cp)
hold on
temp_1=temperature(1:400)
cp_1=cp(1:400)
co_eff1=polyfit(temp_1,cp_1,1);
predicted_cp1=polyval(co_eff1,temp_1);
temp_2=temperature(401:800)
cp_2=cp(401:800)
co_eff2=polyfit(temp_2,cp_2,1);
predicted_cp2=polyval(co_eff2,temp_2);
temp_3=temperature(801:1200)
cp_3=cp(801:1200)
co_eff3=polyfit(temp_3,cp_3,1);
predicted_cp3=polyval(co_eff3,temp_3)
temp_4=temperature(1201:1600)
cp_4=cp(1201:1600)
co_eff4=polyfit(temp_4,cp_4,1);
predicted_cp4=polyval(co_eff4,temp_4)
temp_5=temperature(1601:2000)
cp_5=cp(1601:2000)
co_eff5=polyfit(temp_5,cp_5,1);
predicted_cp5=polyval(co_eff5,temp_5)
temp_6=temperature(2001:2400)
cp_6=cp(2001:2400)
co_eff6=polyfit(temp_6,cp_6,1);
predicted_cp6=polyval(co_eff6,temp_6)
temp_7=temperature(2401:2800)
cp_7=cp(2401:2800)
co_eff7=polyfit(temp_7,cp_7,1);
predicted_cp7=polyval(co_eff7,temp_7)
temp_8=temperature(2801:3200)
cp_8=cp(2801:3200)
co_eff8=polyfit(temp_8,cp_8,1);
predicted_cp8=polyval(co_eff8,temp_8)
plot(temp_1,predicted_cp1,'linewidth',3)
plot(temp_2,predicted_cp2,'linewidth',3)
plot(temp_3,predicted_cp3,'linewidth',3)
plot(temp_4,predicted_cp4,'linewidth',3)
plot(temp_5,predicted_cp5,'linewidth',3)
plot(temp_6,predicted_cp6,'linewidth',3)
plot(temp_7,predicted_cp7,'linewidth',3)
plot(temp_7,predicted_cp7,'linewidth',3)
plot(temp_8,predicted_cp8,'linewidth',3)
and the graph for it is shown below:
here we introduced 2 new concept
1:legend :which gives the representation.
2:subplot:which splits the graphical display