CURVE FITTING USING REGRESSION
AIM:
To find the best relationship for the curve between Cp and temperature according to their given unknown data.
OBJECTIVES:
- To understand the meaning of regression and find the best fit for a certain given curve.
- To distinguish between perfect fit and best fit.
- To gain knowledge about different methods to find the best fit for a certain given curve.
INTRODUCTION:
In statistical modeling, regression analysis is a set of statistical processes for estimating the relationships between a dependent variable (often called the 'outcome variable') and one or more independent variables (often called 'predictors', 'covariates', or 'features'). The most common form of regression analysis is linear regression, in which a researcher finds the line (or a more complex linear combination) that most closely fits the data according to a specific mathematical criterion.
Best fit is considered as the fit that we can fit into the curve by normal regression method. But, perfect fit is considered when we can calculate all the error points in our fit and stack it into the given relationship for getting a probable exact relationship.
Criteria to get a best fit:
The following four ways give us the measure of how good our fit is:
- SSE(Sum of squares of errors) – If y = f(x) is the probable fit, then at a general point 'i',
SSE = `sum_(i=0)^N[y(i)-f(x(i))]^2`
- R – square – This method is used for mean centering the data.
SSR = `sum_(i=0)^N[f(x(i))-Mean]^2`
SST = SSR + SSE
`R^2` = `(SSR)/(SST)`
- R – square(adjusted) – This method is used when more different independent variables are used and to know if/ whether these may affect the actual relationship or not.
`(R^2)_(adj)`= `1-[(1-R^2)(n-1)/(n-k-1)]`
where n = no. of data points, k = no. of independent variables.
- RMS -
RMS = `sqrt((SSE)/n)`
PROCEDURE OF COMPLETION:
- At first, the data between Cp and temperature was loaded onto the MATLAB workspace.
- Then, they were separated out into two columns and plotted.
- At first, a linear fit was adjusted and then a cubic fit was adjusted according to the 4 parameters mentioned above.
The following code can be referred to complete the assignment:
%% CURVE FITTING
clear all;
close all;
clc;
%% loading the data onto the workspace
data = load('data');
% initialization of variables
temp = data(:,1); % temperature (in K)
Cp = data(:,2); % Cp
n = length(Cp); % no. of datapoints
%% Predicted Curve fitting
% Linear polynomial (Cp = a*T + b)
coeff1 = polyfit(temp,Cp,1); % coefficients of polynomial
Cp1 = polyval(coeff1,temp); % new Cp
% Cubic polynomial (Cp = a*T^3 + b*T^2 + c*T + d)
coeff3 = polyfit(temp,Cp,3); % coefficients of polynomial
Cp3 = polyval(coeff3,temp); % new Cp
%% Plotting the original data with the new ones
% Linear curve fitting
figure(1),clf
plot(temp,Cp,temp,Cp1,'linew',2)
axis square, grid on
xlabel('Temp','fontsize',12)
ylabel('Cp','fontsize',12)
title('CURVE FITTING USING LINEAR POLYNOMIAL','fontsize',15)
legend('Original','Linear fitting')
% Cubic curve fitting
figure(2),clf
plot(temp,Cp,temp,Cp3,'linew',2)
axis square, grid on
xlabel('Temp','fontsize',12)
ylabel('Cp','fontsize',12)
title('CURVE FITTING USING CUBIC POLYNOMIAL','fontsize',15)
legend('Original','Cubic fitting')
%% Evaluation of goodness of fit
% As per our observation degree 3 polynomial almost fits the curve.
% let's check how good it has fitted by the following methods:
% SSE(Sum of squares of errors)
for i=1:length(Cp3)
sse = sum((Cp(i) - Cp3(i)).^2);
end
% R-square
for j=1:length(Cp3)
ssr = sum((Cp3(j)-mean(Cp)).^2);
end
sst = ssr + sse;
R_square = ssr/sst;
% R-square adjusted
k = 1; % no . of independent variables
R_adj = 1 - (((1-(R_square)^2)*(n-1))/(n-k-1));
% RMS
RMS = sqrt(sse/n);
%% plotting
figure(3),clf
plot(temp,Cp,temp,Cp3,'linew',2)
grid on
xlabel('Temp','fontsize',12)
ylabel('Cp','fontsize',12)
title('CURVE FITTING USING CUBIC POLYNOMIAL','fontsize',15)
legend('Original','Cubic fitting')
text(520,1290,['SSE = ' num2str(sse)])
text(520,1280,['R-square = ' num2str(R_square)])
text(520,1270,['R-adjusted = ' num2str(R_adj)])
text(520,1260,['RMS = ' num2str(RMS)])
OBSERVATIONS:
At first, using the 'Curve Fitting Tool', we get the following result:
Now, using the MATLAB programming, the linear fit and cubic fit are as follows:
CONCLUSION:
Regression is a powerful tool to gather knowledge about the relationship between any two(or more) given data set. This is an integral part of Machine Learning. To get the perfect fit, the error parameters must always be considered as they will give us a perfect measure for our need.
REFERENCES:
- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
- https://www.udemy.com/course/master-matlab-through-guided-problem-solving/learn/lecture/11135272?start=1#questions
(Check the non-linear fitting in the above link)