Curve fitting using MATLAB .
AIM - To perform the Curve fitting of the given data in MATLAB.
Governing Equations -
For R-square method, `R^2 = (SSR)/(SST)`
where `SSR = sum_(i=0)^n(f(x(i))-Mean)^2` (Sum of Squares of the Regression)
`SSE = sum_(i=0)^n(Y(i)-f(x(i)))^2` (Sum of Squares of the errors)
Sum of Squares of the total = `SST=SSR+SSE`
Root Mean Squared Error = RMSE = `((SSE)/n)^(1/2)`
Reference for Formulas and Concepts =
https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
Objectives -
1. To plot the given data and curve fit.
2. To determine the error using R-square method
Procedure
Inputs-
MATLAB code file-
Code to curve fit the linear and Cubic Polynomial-
clear all
close all
clc
%Preparing the data
input_data = load('data');
%divide the data in two parts
temp1 = input_data(1:1700,1);
cp1 = input_data(1:1700,2);
temp2 = input_data(1701:3200,1);
cp2 = input_data(1701:3200,2);
%using the whole data
cp = input_data(:,2);
temp = input_data(:,1);
%Curve fitting
%Fit the divided data
co_effs1 = polyfit(temp1,cp1,1); %polyfit of linear polynomial
predicted_cp1 = polyval(co_effs1,temp1);
co_effs2 = polyfit(temp2,cp2,1);
predicted_cp2 = polyval(co_effs2,temp2);
%Fitting the whole data set (cubic)
co_effs3 = polyfit(temp,cp,3); % n order = 3
predicted_cp3 = polyval(co_effs3,temp);
%compare original curve to the fitted curve
%comparing the divide data
figure(1)
hold all
plot(temp1,cp1,'linewidth',3,'color','g')
plot(temp2,cp2,'linewidth',3,'color','y')
plot(temp1,predicted_cp1,'linewidth',3,'color','r')
plot(temp2,predicted_cp2,'linewidth',3,'color','b')
xlabel('Temperature')
ylabel('Specific heat(cp)')
title('compare the divided data in linear polynomial')
legend('first dataset','second dataset','first predicted set','second predicted set')
hold off
%comparing the whole data
figure(2)
hold on
plot(temp,cp,'linewidth',3)
plot(temp,predicted_cp3,'linewidth',3)
title('comparing the whole data using cubic polynomial')
xlabel('Temperature')
ylabel('Specific heat(cp)')
legend('Original','Fitted')
%Goodness of fit for cubic polynomial
M = mean(cp);
%R^2 of cubic fit
for i=1:length(cp)
%sum of squared errors
SSE_cubic=sum((cp(i)-predicted_cp3(i))^2); %order of equation 3
%sum of squares of regression
SSR_cubic = sum((predicted_cp3(i)-M)^2); %order of equation 3
end
%Total sum of squares
SST_cubic=SSE_cubic+SSR_cubic;
%R square calculator
R_sq_cubic = SSR_cubic/SST_cubic
%Goodness of fit for square polynomial
M1=mean(cp1);
M2=mean(cp2);
%R^2 of square fit of first set
for i=1:length(cp1)
%sum of squared errors
SSE_1=sum((cp1(i)-predicted_cp1(i))^2);
%sum of squares of regression
SSR_1 = sum((predicted_cp1(i)-M1)^2);
end
%R^2 of square fit of second set
for i=1:length(cp2)
%sum of squared errors
SSE_2=sum((cp2(i)-predicted_cp2(i))^2);
%sum of squares of regression
SSR_2 = sum((predicted_cp2(i)-M2)^2);
end
%Total sum of squares
SST_1=SSE_1+SSR_1;
%R square calculator
R_square_linear_1 = SSR_1/SST_1
%Total sum of squares
SST_2=SSE_2+SSR_2;
%R square calculator
R_square__linear_2 = SSR_2/SST_2
% Root mean squared error (RMSE)
RMSE_linear_1 = (SSE_1/(length(cp1)))^0.5
RMSE_linear_2 = (SSE_2/(length(cp2)))^0.5
RMSE_cubic = (SSE_cubic/(length(cp)))^0.5
Code explanation -
1.First we have loaded the data given in the challenge itlself, by using load() function.
2.Then we have assigned the Cp and temperature values to variables using (:,n) function because the data set is a CSV i.e Comma Seperated Values file having 2 columns, i.e. seperated by a comma.
3.Then use polyfit function whose inputs will be temperature and CP and also specify what degree polynomial you will be using. here I have used 2 as the degree.
4.Then using polyval function, obtain the coefficients a, b and c.
5.Using plot function, plot the graph of Cp of original dataset and Predicted Cp got from puting the values obtained from the polyval into the polynomial. Compare the fit of the curve visually.
6.The later part of the code is used for checking the maximum error of the fitting of the curve. 2 methods are used here, namely 1. R-square method and 2. Root mean square method.
7.The value of R in R-square method should lie between 0 and 1 where 0 means poor fit and 1 means Typically a value greater than 0.95 means a good fit.
8.The RMSE value in the next method should be as less as possible to be called as a good fit.
Results -
Output -
Result through MATLAB Code file-
Results in the Command window
Results through Curve Fitting toolbox
Linear polynomial with Cp length 1-1700
Linear polynomial with Cp length 1701-3200
Cubic polynomial with whole Data set
Conclusion -
Therefore the Curve fitting has been performed through the given data set.
The curve fitting can be smooth by increasing the order of the polunomial.