Project

Curve Fitting and Goodness of Fit

Aim: To perform Curve Fitting for Linear and Cubic Polynomial for the Cp data

Theory:

Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data.

Matlab has two functions, polyfit and polyval, which can quickly and easily fit a set of data points with a polynomial.

Polyfit is a Matlab function that computes a least squares polynomial for a given set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data.

Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the coefficients found using polyfit.

Program:

Code for the Curve Fitting

clear all
close all
clc

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

% curve fit

% Linear Fit: cp = a*T + b
co_effs_linear = polyfit(temperature,cp,1);
predicted_cp_linear = polyval(co_effs_linear, temperature);

% Quadratic Fit: cp = a*T^2 + b*T^1 + c
co_effs_quad = polyfit(temperature,cp,2);
predicted_cp_quad = polyval(co_effs_quad, temperature);

% Cubic Fit: cp = a*T^3 + b*T^2 + c*T + d
co_effs_cubic = polyfit(temperature,cp,3);
predicted_cp_cubic = polyval(co_effs_cubic, temperature);

% Compare my curve fit with the original data

figure(1)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,predicted_cp_linear,'linewidth',3,'color','r')
title('Comparing Original Dataset and Linear Fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset','Linear Fit')

figure(2)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,predicted_cp_quad,'linewidth',3,'color','r')
title('Comparing Original Dataset and Quadratic Fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset','Quadratic Fit')

figure(3)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,predicted_cp_cubic,'linewidth',3,'color','r')
title('Comparing Original Dataset and Cubic Fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset','Cubic Fit')

The Procedure followed while writing the above code is cited below:

  1. The code is written to load the file 'data' in MatLab which contains the raw data of multiple cp and temperatures.
  2. Two variables 'temperature' and 'cp' are created and the values are assigned to them.
  3. In this step, we create a variable to store the coefficients of the linear equation and this done by assigning 'polyfit(temperature,cp,1)' to 'co_effs_linear'.
  4. Here the value of the cp is to be calculated so the pre-defined function polyval is used and the required parameters are filled in as shown above and then assigned to a new variable 'predicted_cp_linear'.
  5. Similarly, the two steps mentioned above are performed for a quadratic and cubic polynomial with the creation of new variables, and the parameter, order of the polynomial is changed from 1 to 2,3 for these cases, the code can be referred to which is cited above.
  6. Then, code is written to generate the plots as mentioned with titles, axes with labels and legend which differentiates the curves from each other. The plots are shown at the end of the result.

Parameters used to measure the fitness of a curve:

  1. The sum of squares due to error (SSE)
  2. R-square
  3. Adjusted R-square
  4. Root mean squared error (RMSE)

Curve Fitting using ToolBox:

Goodness of fit:

SSE: 2.163e+06

R-square: 0.9249

Adjusted R-square: 0.9249

RMSE: 26.01

In this Curve fit Toolbox, the curve fit type used is polynomial with a degree of 1, because we need the result for a linear function. The result which we get from this fit which is verified through the value of 'R-square' is 0.9249 which is considerably good as it close to the '1' which is the best fit.

Goodness of fit:


SSE: 5.401e+05

R-square: 0.9812

Adjusted R-square: 0.9812

RMSE: 13

In this Curve fit Toolbox, the curve fit type used is polynomial with a degree of 2, because we need the result for a quadratic function. The result which we get from this fit which is verified through the value of 'R-square' is 0.9812 which is considerably good as it close to the '1' which is the best fit.

Goodness of fit:

SSE: 9.427e+04

R-square: 0.9967

Adjusted R-square: 0.9967

RMSE: 5.431

In this Curve fit Toolbox, the curve fit type used is polynomial with a degree of 3, because we need the result for a cubic function. The result which we get from this fit which is verified through the value of 'R-square' is 0.9967 which is almost equal to '1' which is the best fit.

Goodness of fit:

SSE: 0

R-square: 1

Adjusted R-square: NaN

RMSE: NaN

In this Curve fit Toolbox, the curve fit type used is Interpolant with method 'Linear', because we need the result for a linear function. We observe that after applying the parameters the result that we get is the best fit because the value of R-square is equal to 1. So to reduce or avoid any errors Interpolant function can be considered an option.

Goodness of fit:

SSE: 0

R-square: 1

Adjusted R-square: NaN

RMSE: NaN

In this Curve fit Toolbox, the curve fit type used is Interpolant with method 'Cubic', because we need the result for a Cubic function. We observe that after applying the parameters the result that we get is the best fit because the value of R-square is equal to 1. So to reduce or avoid any errors Interpolant function can be considered an option.

Error Reduction:

Root Mean Square Error (RMSE) is a standard way to measure the error of a model in predicting quantitative data.

Sum of Squares Due to Error: It is also called the summed square of residuals and is usually labeled as SSE. A value closer to 0 indicates a better fit. Note that the SSE was previously defined in The Least Squares Fitting Method.
Error in Coding:
Error 1:
This error is observed because the third parameter in the function polyfit i.e the order of the polynomial is not mentioned so the MatLab will shout an error.
Error 2:
This error is observed because the data file is not present in the same folder as the existing '.m' extension file that is created.
Error 3:
This is more a warning than an error that tells the user to reduce the degree of the polynomial.

Result:

The Plot for Original Dataset and Linear Fit is shown above.

The Plot for Original Dataset and Quadratic Fit is shown above.

The Plot for Orginal Dataset and Cubic Fit is shown above.

1. How to make a curve fit perfectly?

The curve fit can be made perfect by changing the fit type to Interpolant which reduces the errors to zero so that they turn out to be identical. Interpolation is the best way to make a curve fit to be perfect.

2. How to get the best fit?

The best fit can be achieved by splitting a polynomial into multiple polynomials and also by increasing the order of the polynomial can be a method to obtain the best fit. Whenever the value of R square is obtained to be '1' then we can conclude that we have achieved the best fit.

3. What could be done to improve the cubic fit?

The cubic fit can be improved by splitting it into multiple polynomials or it can be solved by using the interpolation method which will give it the best fit. The errors have to be reduced in order to obtained the desired output.

References:

https://en.wikipedia.org/wiki/Curve_fitting

https://engineering.vanderbilt.edu/ge/es140/Otherlinks/MatlabTutorial/CurveFitting.php

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.