Curve Fitting, Quality of fits and Fitting errors
Introduction
Knowledge in terms of data is Power. In engineering, plethora of data is generated. However, data doesnt make sense until it could be assessed and used for the predcition of the phenomena. Curve fitting is one of the most basic operation in this approach towards utilising and making sense out of the data. The data is fitted into a form of equation, so that equation could be used for further predictions of the phenomena.
However, it is a double edged sword, underfitting and overfitting is detrimental to the prediction and hence there are several parameters to assess the quality of the data fit. Among them, R2 and RMS error are the two widely used benchmarking parameter for the quality of the fit
R2 varies between 0 and 1. Closer to 0 indicates a poor fit and 1 indicates the best fit. this could be achieved by two means. Either by breaking the data into different sets and having the individual equations for each set or increasing the power of fit until the error comes within tolerance. Latter approach is taken in this code
Octave code
The code is written to perform the curve fitting process and analyse the quality of fit. The data in form of.csv is loaded. This data file consists of specific heat and temperature. This data is extracted and loaded into the array. RMS error and power of fit is initialised to 1. A while loop is utilised to increase the power of fit until it meets the RMS error threshold. Similarly R2 value is calculated for the final fit. Predicted data is plotted against the actual data for the comparison and results are reported
%This program is coded by Sweekar and is his intellectual property, contact in case of help
%[email protected]
%This program loads the data abd fits the data while varying the power of the equation
%of fit to reduce the RMS error
%Curve fitting is an important aspect of engineering as we generate tons of data
%These data only makes sense once they are able to predict future trend
%To do this, Curve fitting is used to generate the equation and variable is used to predict
%Two ways of achieving this feat. Either breaking curves into two datasets or increasing the exponent
%Drawback of this method is that it is susceptible to overfit
%Advantage: A simple code to quickly find the trend and probable power
close all
clear all
clc
cp_data = load('data1.csv'); % loading the cp vs temp data
temperature = cp_data(:,1); %Extracting temperature from data
cp = cp_data(:,2); %extracting specific heat from data
RMS_error = 1; %Initial assumption
n = 1; %Initial assumption of Power exponent
%While loop increases power of the fit while trying to reduce the RMS error and meeting
%error threshold
while RMS_error>0.6
coeffs = polyfit(temperature,cp,n);
predicted_cp = polyval(coeffs,temperature);
error = (predicted_cp-cp).^2;
average1 = sum(error)/length(cp);
RMS_error = average1^0.5
n = n+1;
end
%R2 calculation which is used to characterise the quality of fit
%R2 closer to 1 is a good fit and close to zero is a bad fit
Mean = sum(cp)/length(cp); %Calculating the mean of the data set
for i =1:length(predicted_cp)
Predvar(i) = (predicted_cp(i)-Mean)^2;
end
TotPredvar = sum(Predvar); %variation in predictec cp
ActPredvar = sum(error); %spread of actual data
Total = TotPredvar + ActPredvar;
R2 = TotPredvar/Total;
%Plotting of data
plot(temperature,cp,'color','b')
hold on
plot(temperature, predicted_cp,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat[kJ/kgK]')
legend('original data','Curve fit')
title('Cp vs Temperature')
a = RMS_error;
b = n-1;
printf('The power of the fit is %d with RMS error of %f\n',b,a)
printf('The quality of fit given by R2 which is %f for this data\n',R2)
Results and Plots
Discussion and Report
1) R2 value of the fit is 0.999971 which indicates a very good fit
2) The power of fit is 8, RMS error target is kept close to 0.6
3) As the error threshold is reduced, lets say to 0.09, power of the fit increases to 12, R2=0.99999, indicating possible overfitting
4) Similarly, if an unrealistic fit criteria is specified, code returns a NaN, for example if error threshold is set to 0.04 which is highly unrealistic, following output could be noticed