Curve Fitting - (Temperature vs Cp)
Objective:
- To perform Curve Fitting for a given data set(specific heat of oxygen wrt to temperature)
- To fit the polynomial of order 1 and order 3.
- To increase the goodness of fit for the polynomial of order 3
Code: In MATLAB
-
%load the file a = load('curve_fit_cp_data'); %extracting the columns from the file and storing them in the form of %vectors temp = a(:,1); cp = a(:,2); [sfit,gfit] = fit(temp,cp,'poly1'); %linear fit of order1 [sfit1,gfit1] = fit(temp,cp,'poly3'); %linear fit of order 3 plot(sfit,temp,cp); plot(sfit1,temp,cp); a1 = unique(temp); if length(a1) == length(temp) disp("No repeated values in vector temp") end b1 = unique(cp); if length(b1) == length(cp) disp("No repeated values in vector cp") end % centering and scaling of data using polyfit function [p,~,mu] = polyfit(temp,cp,5); f = polyval(p,temp,[],mu); plot((temp),(cp),'o','MarkerSize',1) hold on plot((temp),f) hold off fit_val_1 = gfit fit_val_3 = gfit1 % residual plots of the fits plot(sfit,temp,cp,'residual') plot(sfit1,temp,cp,'residual') %fitting linear model for intercept [lin] = fitlm((temp),(cp)) plotResiduals(lin) plotResiduals(lin,'probability') %using the log(y) for tranforamtion and to make the distribution of %random error approximately normal. [lin_log] = fitlm(temp_log,cp_log); plotResiduals(lin_log); temp_log = log(temp); cp_log = log(cp); [sfit_log,gfit_log] = fit(temp_log,cp_log,'poly3'); gfit_log plot(sfit_log,temp_log,cp_log,'residual')
Process:
Fitting the linear model of order 1 and 3
Order 1: output
Order 3 : output.
The fit was complete but MATLAB gives a Warning: Equation is badly conditioned. Remove repeated data points or try centering and scaling.
Moving further we will first check if there are repeated values.
unique(temp) returns the same data as in a1, but with no repetitions, so if there will be any repeated values the length of ‘a1’ will be small than the vector ‘temp’
So, we check if the length of the new vector is equal to temp. If there have been any repetitions the
‘if ‘condition will become false and we will not get an output.
Output: of the above code.
Now we will try the centering and scaling of data.
Use polyfit with three outputs to fit a 3th-degree polynomial using centering and scaling, which improves the numerical properties of the problem. polyfit centers the data in year at 0 and scales it to have a standard deviation of 1, which avoids an ill-conditioned Vandermonde matrix in the fit calculation.
After centering and scaling ,now we check the values of the fit.
fit_val_1 provides the value of order1 and fit_val_3 of order3. We come to conclusion that both the fit gives satisfactory value but the fit 3 gives a much good value of rsquare. The value of SSE of the both the fit is very large.
In order to increase the goodness of fit we first need to understand what the problem. We will start by checking the residual plot of the fit.
Residual = Observed – Predicted
Residual plot of order 1: It clearly shows an inverted U shape. It’s a non-random plot. The distance from the line at 0 is how bad the prediction was for that value.
By looking up the residual plot for order1 it is very much clear that order1 polynomial does not fit the data up to the mark
Here is the graph for order 3 .It’s better than the order1 but it also follows a pattern.
Also, it does not give the information about the large value of SSE.
Now, we plan to fit a linear model and get the intercept and will look to the histogram of residual using probability density function scaling.
Output for above code:
It is clearly seen that the above histogram is not normally distributed.
We will use transformation for non -normal distributed data type. The log(y) will be used in this project for the transformation. One can also test the square root or the reciprocal (1/y) .
The residual histogram plot after the transformation of value shows an approximation for normal distribution.
Output of the above code:
SSE value shows a large decrement in the value and is very close to 0. The value of rsquare also increases. It gives us the perfect fit for the values of temperature and specific heat.
We can also go for the split wise operations of breaking down the large data set into smaller number of pieces and fitting it but for a very large data set it could be a cumbersome process.
CONCLUSION:
log transformation is a good method to fit a non -normal distributed data type