MATLAB Program to perform and compare the linear and cubic fit for given data.
AIM:
To perform curve fitting based on the given set of datasets.
OBJECTIVE:
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points.
- Explain the parameters used to measure the fitness characteristics for both the curves.
THEORY:
Curve fitting is the process of plotting a curve such that it passes through most of the dataset point for a data having numerous values or points (Big data). Thereby, bringing a proper shaped graph of the data which can be interpreted for further references.
There are a number of ways in curve fitting, the goodness of fit of the curve and the best fit can be determined as mentioned below.
MATLAB PROGRAM FOR CURVE FITTING:
clear
clc
%Conditioning of Data
load ('data')
cp_data = data;
temperature = cp_data(:,1);
cp = cp_data(:,2);
[predicted_cp,gof] = fit(temperature,cp,'poly1','Normalize','on');
%Comparing Curve fit
plot(temperature,cp,'m');
hold on
plot(predicted_cp,temperature,cp);
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/Kmol^-K');
legend("Fitted curve","original Curve");
title("Curve fitting of linear polynomial for the Cp data ")
hold off
%Determining the Best Fit
gof
predicted_cp
cftool
(OR)
clear
clc
%Conditioning of Data
load ('data')
cp_data = data;
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fit
%cp = a*T^2+b*T+c
co_effs = polyfit(temperature,cp,1);
predicted_cp = polyval(co_effs, temperature);
%Comparing curve fit with original data
plot(temperature,cp,'-g','LineWidth',3);
hold on
plot(temperature, predicted_cp,'-r','LineWidth',3)
title("Curve fitting of linear polynomial for the Cp data ")
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/Kmol^-K');
legend('Orginal Curve','Curve fit')
hold off
EXPLANATION OF THE ABOVE PROGRAMS:
Initially load the load into the workspace.
- Then segregate the data by assigning suitable data to the appropriate variables as done above.
PROGRAM 1
- 'fit' function is used to fit a polynomial to data.
- The curve of original data and the fit is plotted using 'plot' funciton.
- To fit polynomials of different degrees, change the fittype string.
- Due to large number of input values, to obtain better results by centering and scaling the data 'normalize' function is used.
- The struct
gofshows the goodness-of-fit statistics. - 'predicted_cp' is used to display the models, the fitted coefficients, and the confidence bounds for the required fits.
- The command 'cftool' can be used to open the curve fitting tool box to visualize the graphical data, table of datas, and numerical results inorder to find the best fit in a easy way.
PROGRAM 2
- The polyfit function is used to plot the curve through multiple point of the data in the best possible way.
- 'polyval' function evaluates coe_effs at each point of temperature.
- Then the curve of original data and the fit is plotted using 'plot' funciton.
- The goodness of fit statistics and determination of best fit can be evaluated using the 'Curve fitting tool'.
ANSWER TO THE QUESTIONS:
How to make a curve fit perfectly?
A curve can be made to fit perfectly by comparing the criteria 'sum of squares due to error (SSE) and 'R square' values obtained for rious fits. A fit is perfect if the SSE value is lesser(nearer to 0) and if the R square value is greater (nearer to 1).
How to get the best fit?
To determine the best fit, we should examine both the graphical and numerical fit results of the various degrees of fits (linear,quadratic etc.,) obtained for the data, the fit which has minimum SSE value and the Maximum R square value when compared with other fits is termed as the best fit.
What could be done to improve the cubic fit?
Cubic fit can be improved by normalizing the data by selecting the Center and scale checkbox.
ERRORS FACED:
- The below error occurred because of not loading the data file in the initial step. The error was later rectified by loading the data file in the MATLAB workspace.
RESULTS:
- The curve fit for linear and cubic polynomial were obtained.
- The parameters used to measure the fitness characteristics of the both the curves are: The sum of square due to errors (SSE), Adujsted R-square, R-square, Root Mean Square Error(RMSE) and by comparing the coefficients and bounds (
p1,p2, and so on) in the Results pane for both fits .
DETERMINATION OF BEST FIT:
By comparing the graphical and numerical results the best fit among the linear and cubic fit is the cubic fit since it has the minimum SSE value and maximum R square value than the linear fit.
So the R square value of the best fit = 0.9967.
THANK YOU:)