Curve fitting using MATLAB
1. Write the code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
First we are writing codes to fit a linear curve of degree 3 is as follows.
clear all
close all
clc
%Preparing the data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%curve fit
%cp=a*T^2+b*T+c
coefficients=polyfit(temperature,cp,2);
predicted_cp=polyval(coefficients,temperature);
%compare my curve fit with the original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('temperature[K]')
ylabel('specific Heat [KJ/Kmol-K]')
legend('original data set','curve fit')
%calculating the error
error=cp-predicted_cp
fprintf('Maximum error=');
disp(max(error));
The maximum error in this case will be 20.9318.
The plot is as follows.
Now writing the codes for the Cubic polynomial
clear all
close all
clc
%Preparing the data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%curve fit
%cp=a*T^2+b*T+c
coefficients=polyfit(temperature,cp,3);
predicted_cp=polyval(coefficients,temperature);
%compare my curve fit with the original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('temperature[K]')
ylabel('specific Heat [KJ/Kmol-K]')
legend('original data set','curve fit')
%calculating the error
error=cp-predicted_cp
fprintf('Maximum error=');
disp(max(error));
The maximum error in this case will be 9.6525.
The plot for the second case is as follows.
So as we can see that the error in the Cubic polynomial is less than the Linear polynomial, we can conclude that the Cubic polynomial curve is the best suited for curve fit. But this also has some warning.
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try
centering and scaling as described in HELP POLYFIT.
> In polyfit at 75
In curvefit2 at 13
So Cubic polynomial is also not the efficient one for the curve fit.
2. What needs to be done in order to make the curve fit perfect?
So in order to make the curve fit perfect we need to devide the Original data set into 5 separate data and obtain the error.
clear all
close all
clc
%Preparing the data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%deviding the data set into 5 sets
temperature1=cp_data(1:640,1);
cp1=cp_data(1:640,2);
temperature2=cp_data(641:1280,1);
cp2=cp_data(641:1280,2);
temperature3=cp_data(1281:1920,1);
cp3=cp_data(1281:1920,2);
temperature4=cp_data(1921:2560,1);
cp4=cp_data(1921:2560,2);
temperature5=cp_data(2561:3200,1);
cp5=cp_data(2561:3200,2);
%curve fit
%cp=a*T^2+b*T+c
coefficients1=polyfit(temperature1,cp1,2);
coefficients2=polyfit(temperature2,cp2,2);
coefficients3=polyfit(temperature3,cp3,2);
coefficients4=polyfit(temperature4,cp4,2);
coefficients5=polyfit(temperature5,cp5,2);
predicted_cp1=polyval(coefficients1,temperature1);
predicted_cp2=polyval(coefficients2,temperature2);
predicted_cp3=polyval(coefficients3,temperature3);
predicted_cp4=polyval(coefficients4,temperature4);
predicted_cp5=polyval(coefficients5,temperature5);
%compare my curve fit with the original data
plot(temperature,cp,'linewidth',3)
hold on
xlabel('temperature[K]')
ylabel('specific Heat [KJ/Kmol-K]')
%plotting predicted cp
plot (temperature1 , predicted_cp1, 'linewidth', 3, 'color','r');
plot (temperature2 , predicted_cp2, 'linewidth', 3, 'color','r');
plot (temperature3 , predicted_cp3, 'linewidth', 3, 'color','r');
plot (temperature4 , predicted_cp4, 'linewidth', 3, 'color','r');
plot (temperature5 , predicted_cp5, 'linewidth', 3, 'color','r');
legend('original data set','curve fit')
%calculating the error
predicted_cp=[predicted_cp1;predicted_cp2;predicted_cp3;predicted_cp4;predicted_cp5]
error=cp-predicted_cp
fprintf('Maximum error=');
disp(max(error));
The error in this case will be 7.7132. Which is the least of all.
The plot for this case is shown below.
This will be the best suited curve fit.