Project

Curve fitting for different kinds of polynomials

Objective: To use the concept of curve fitting for various kinds of polynomials (in this case, linear and cubic polynomials) and explain the parameters used to measure the fitness characteristics of the curves.

Procedure

Step 1: The sample data is given as the input to MATLAB, in this case, the dependence of the specific heat of a gas (here, di-atomic oxygen), on temperature.

Step 2: The coefficients of the linear and cubic polynomial fits are determined the polyfit() function, and these values are stored in another variable for each fit using the polyval() function, as a function of temperature.

Step 3: The fitness parameters for confirming which polynomial fit is better are discussed.

Fitness Parameters

1. SSE (Sum of Squares due to Error)

This parameter measures the total deviation of the response values from the fit to the response values. It can be mathematically represented as:

`SSE = sum_(i = 1) ^ n w_i(y_i - hat y_i) ^2`

2. R-Square

This parameter is used to predict how useful a model is, in terms of the square of the distance from the points to the best fitting line. It is useful to note that as the number of regression coefficients increases, `R^2` never decreases, even if the additional variable is not useful, but increases a lot if the additional variable affects the variation directly.

Mathematically, it can be defined as the ratio of the sum of the squares of regression (SSR) to the sum of the squares of the total sum of SSE and SSR (called SST)

`R^2 = 1 - ((SSE)/(SST)) = (SSR)/(SST)`

Hence, `R^2` lies between 0 and 1, and for a model that is not useful, `R^2 = 0`. Whereas for `R^2 = 1`, the model fits all the points perfectly. Therefore, all models have `R^2` values between these extremes.

For values of the dependent variable that are hard to predict, a smaller value of `R^2` is more useful, but in general, for a model to be deemed as a "good fit" from the `R^2` value is difficult to predict as it entirely depends on the context.

3. Degrees of Freedom Adjusted R-Square (`R_adj`)

This parameter is a modified form of the R-Square parameter as mentioned above, wherein the degree of freedom `v` is calculated as the number of response values `n` minus the number of fitted coefficients `m` estimated from the response values.

So, `v = n - m`

So, the adjusted R-square value can be given as:

`adjusted R-Square = 1 - (SSE(n-1))/(SST(v))`

This parameter is a better indicator of a better fit as compared to the `R^2` value when two models are nested, that is, when they add additional coefficients to the previous model.

4. Root Mean Square Error (RMSE)

Also known as the fit standard error or the standard error of the regression, this parameter is essentially an estimate of the standard deviation of the random component of data, and can be mathematically represented as:

`RMSE = s = sqrt (MSE)`

Where MSE is defined mathematically as:

`MSE = (SSE)/v`

Validation of fitness characteristics using the curve fitting toolbox in MATLAB

The figures below present a linear and cubic fit for the raw data obtained from a table of `C_p` values and temperature values.

From the above figures, it can be observed that the `R^2`, as well as the `R_(adj)` values for the cubic polynomial, are higher than that for the linear polynomial, and the SSE and the RMSE values are also considerably lower for the cubic fit. Thus for this case, it can be concluded that the cubic fit is the better fit for the input data as all the fitness parameters favours the same.

Answers to the questions in the challenge

Ans 1: In general, there is no foolproof perfect curve fit method, as mentioned in reference 3, as it purely depends on the requirement of the fit for later use or approximations. In the above case, it can be noted, however, that the better fit is the cubic polynomial on account of all the fitness parameters being lesser than their counterparts for the linear polynomial.

Ans 2: The best fit is more or less an optimization technique, which highly depends on the need of the fit for later purposes. The best fit for a given data set is that straight line that best represents data on a scatter plot. This point may pass through some of the points, none of the points or all of the points. Whereas the perfect fit for a given data set is that model in which exactly all the points in the given model fit, and according to theory, for a perfect model, `R^2 = 1`.

Ans 3: The cubic fit can be improved by either:

1. Adding weights to each and every term in the polynomial coefficients of the cubic fit. This would ensure that the effect of a particular term allows improving the accuracy of the fit. This can be done by a trial and error method.

2. Increasing the number of data points that would affect the value of`R^2`. This would increase the value of `R_adj`, which allows for a better fit for the data set.

Errors obtained and steps to deal with them

1. Incorrectly using function names for defining the coefficients. This can be easily corrected by providing the correct names for the same.

2. Not using the hold on command. This would provide only the last figure that is asked by MATLAB to be displayed and not the ones asked before it.

Conclusion

It can be observed from the figure below that the cubic fit is undoubtedly the better fit for the given data set as it almost coincides with the values of the given data set.

clear all
close all
clc

%Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%Curve fit with both linear and cubic polynomial coefficients used
% cp = a*T^2 + b*T + c
co_effs_1 = polyfit(temperature,cp,1);
predicted_cp_1 = polyval(co_effs_1,temperature);
co_effs_2 = polyfit(temperature,cp,3);
predicted_cp_2 = polyval(co_effs_2,temperature);

%Compare my curve fit with the original data
plot(temperature,cp,'linewidth',3);
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kmol-K]');
hold on
plot(temperature, predicted_cp_1,'linewidth', 3, 'color', 'r');
plot(temperature, predicted_cp_2,'linewidth', 3, 'color', 'g');
legend('Original dataset','Linear Curve fit','Cubic Curve fit');

%Curve fitting using in-built MATLAB toolbox
cp_data = load('data');
cftool;

References

1. https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html

2. http://www.medicine.mcgill.ca/epidemiology/joseph/courses/EPIB-621/fit.pdf

3. https://en.wikipedia.org/wiki/All_models_are_wrong

4. https://stats.stackexchange.com/questions/60655/what-are-the-implications-of-a-perfect-fit-model

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.