Curve fitting of Specific Heat data using MATLAB
AIM
Write a program to fit a linear and cubic polynomial for the Cp data and measure the fitness characteristics for both the curves.
OBJECTIVES
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves.
- Measure the fitness characteristics of the curves.
EQUATIONS
Curve Fitting
Polynomial curve fitting is when we fit our data to the graph of a polynomial function.
- Polynomial
p(x)of degreen
`p(x)=p_1*x^n+p_2*x^(n−1)+...+p_n*x+p(n+1)`
Measure the fitness characteristics of the curves.
`"Error = "|y(i)- f(x(i))|`
Here,
`y(i)= "Data at point " i`
`f(x(i))= "Data at polynomial function point " i`
- Goodness-of-Fit Statistics
-
The sum of squares due to error (SSE)
-
R-square
-
Adjusted R-square
-
Root mean squared error (RMSE)
-
- Residual Analysis
- Confidence and Prediction Bounds
The sum of squares due to error (SSE)
`SSE ("sum of square errors")=sum_(i=1)^n "Error"(i)^2`
R-square
`SSR = sum_(i=1)^n (f(x(i))-"Mean")^2`
`SSE = sum_(i=1)^n (y(i)-f(x(i)))^2`
`SST = SSR + SSE`
`R^2 = (SSR) / (SST)`
Adjusted R-square
`R^2_(adj)=1-[((1-R^2)*(n-1))/(n-k-1)]`
where,
`n = "Number of datapoints"`
`k = "Number of independent variables"`
Root mean squared error (RMSE)
`RMSE = sqrt((sum_(i=1)^n (y(i)-f(x(i)))^2)/n)=sqrt("SSE"/2)`
MATLAB PROGRAM
% Curve fitting by a linear and cubic polynomial for the Cp data.
clear all
close all
clc
% load data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
legendInfo{1} = 'Original dataset';
% Range of Polynomial Degree
maxPolyDegree = 5;
for polyDegree = 1:maxPolyDegree
co_effs = polyfit(temperature,cp,polyDegree); % polyfit to fit curve
predicted_cp(:,polyDegree) = polyval(co_effs, temperature); % polyval to get predicted data
SSE = sum((cp - predicted_cp(:,polyDegree)).^2); % Sum of squares due to error (SSE)
SSR = sum((predicted_cp(:,polyDegree) - mean(cp)).^2);
SST = SSR + SSE;
R_square = SSR/SST; % R-square
k=1;
n=length(cp);
R_squareAdj = ((1 - R_square)*((n-1)/(n-k-1))); % Adjusted R-square
RMS = sqrt(SSE/n); % Root mean squared error (RMSE)
fitError(:,polyDegree) = [SSE;R_square;R_squareAdj;RMS]; % fiError
legendInfo{polyDegree+1} = ['FitDegree ' num2str(polyDegree)]; % legendInfo Strings
end
% plot curve fit
figure(1)
set(gcf,'position',[10,10,900,600])
plot(temperature, cp, 'linewidth',1);
hold on
plot(temperature, predicted_cp, 'linewidth',1);
xlabel('Temperture in [K]');
ylabel('Specific Heat in [KJ/Kmol-K]');
title('Curve fitting of Cp data');
legend(legendInfo,'location','southeast');
% plot error of curve fit
for polyDegree = 1:maxPolyDegree
% 'Sum of squares due to error (SSE)'
figure(2)
set(gcf,'position',[10,10,900,600])
hold on
plot(polyDegree, fitError(1,polyDegree),'b*');
text(polyDegree, fitError(1,polyDegree),legendInfo{1,polyDegree+1},'VerticalAlignment','bottom','HorizontalAlignment','center');
xlabel('Degree of Polynomial');
ylabel('Sum of squares due to error (SSE)');
title('Sum of squares due to error (SSE)');
% 'R-square '
figure(3)
set(gcf,'position',[10,10,900,600])
hold on
plot(polyDegree, fitError(2,polyDegree),'c*');
text(polyDegree, fitError(2,polyDegree),legendInfo{1,polyDegree+1},'VerticalAlignment','top','HorizontalAlignment','center');
xlabel('Degree of Polynomial');
ylabel('R-square ');
title('R-square ');
% 'Adjusted R-square'
figure(4)
set(gcf,'position',[10,10,900,600])
hold on
plot(polyDegree, fitError(3,polyDegree),'b*');
text(polyDegree, fitError(3,polyDegree),legendInfo{1,polyDegree+1},'VerticalAlignment','bottom','HorizontalAlignment','center');
xlabel('Degree of Polynomial');
ylabel('Adjusted R-square');
title('Adjusted R-square');
% 'Root mean squared error (RMSE)'
figure(5)
set(gcf,'position',[10,10,900,600])
hold on
plot(polyDegree, fitError(4,polyDegree),'b*');
text(polyDegree, fitError(4,polyDegree),legendInfo{1,polyDegree+1},'VerticalAlignment','bottom','HorizontalAlignment','center');
xlabel('Degree of Polynomial');
ylabel('Root mean squared error (RMSE)');
title('Root mean squared error (RMSE)');
end
% Display plot by order and save as .jpj
figure(5)
saveas(gcf,strcat('Root mean squared error (RMSE)','.jpg'))
figure(4)
saveas(gcf,strcat('Adjusted R-square','.jpg'))
figure(3)
saveas(gcf,strcat('R-square','.jpg'))
figure(2)
saveas(gcf,strcat('Sum of squares due to error (SSE)','.jpg'))
figure(1)
saveas(gcf,strcat('Curve fitting of Cp data','.jpg'))
EXPLANATION
Step 1: Load Cp data from file 'data' into the MATLAB workspace.
Step 2: Solve the fit data into polynomials using `"MATLAB polyfit function"`.
Step 3: Get the predicted data ployfit coefficients using `" MATLAB polyval function"`.
Step 3: To evaluate the Goodness-of-Fit use Statistics and create fitness characteristics of the curve fits.
RESULTS
CONCLUSION
Cp data was fitted as a linear and cubic polynomial by using MATLAB code and the fitness characteristics of the curves measured using Goodness-of-Fit Statistics methods.
ERRORS
Correction:
plot(polyDegree, fitError(1,polyDegree),'b*');
Correction:
figure(1)
saveas(gcf,strcat('Curve fitting of Cp data','.jpg'))
REFERENCES