Curve fitting
Code:
%Curve fitting
%Data
Cp_Data=load(\'data\');
temp=Cp_Data(:,1); %Temperature column of the data set
Cp=Cp_Data(:,2); %Cp column of the data set
%Fitting
%Any nth order polynomial can be used to fit the values in a curve
%Higher the order greater is the accuracy
%If n co-efficients are there then n equations are supposed to be used
%Linear function
Co_Eff1=polyfit(temp,Cp,1); %Determining the co-efficients of the curve using linear equation
Predicted_Cp1=polyval(Co_Eff1,temp); %Predicting using the x values and the obtained co-efficients
%Cubic Function
Co_Eff3=polyfit(temp,Cp,3); %Determining the co-efficients of the curve using 3rd order polynomial equation
Predicted_Cp3=polyval(Co_Eff3,temp); %Predicting using the x values and the obtained co-efficients
%Plot
plot(temp,Cp,\'linewidth\',4,\'color\',\'b\');
hold on;
grid on;
plot(temp,Predicted_Cp3,\'linewidth\',4,\'color\',\'r\');
plot(temp,Predicted_Cp1,\'linewidth\',4,\'color\',\'g\');
axis([0 3700 900 1450]);
ylabel(\'Temperature[K]\');
xlabel(\'Specific heat [KJ/Kmol-K]\');
legend(\'Original values\',\'Predicted values (Cubic Function)\',\'Predicted values(Linear Function)\');
Output graph:
We can see from the above outputs that the cubic function is a more accurate fit than compared to the linear function.
The cubic function is better fit because there are 3 co-efficients involved between the two points which can create a smooth curve than the linear function as the linear function has only 1 co-efficient and creates only a striaght line.