Project

CURVE FITTING USING MATLAB

I. OBJECTIVES

  1. To build a program in MATLAB that could perform curve fitting of the Linear and Cubic polynomials for the given Temperature & Specific Heat, Cp data and plot them respectively.
  2. To calculate the Goodness of Fit of the curves.
  3. To write another program in MATLAB that can showcase the Splitwise method in curve fitting.

II. INTRODUCTION

  1. Curve Fitting - In regression analysis, curve fitting is the process of specifying the model that provides the best fit to the specific curves in your dataset. Curved relationships between variables are not as straightforward to fit and interpret as linear relationships. For linear relationships, as you increase the independent variable by one unit, the mean of the dependent variable always changes by a specific amount. This relationship holds true regardless of where you are in the observation space.

Common polynomials for fitting any curve: -

  1. 1­st degree polynomial equation (Linear) `f ( x ) = a x + b`
  2. 2nddegree polynomial equation (Quadratic) `f(x)=ax^2+bx+c`
  3. 3rd degree polynomial equation (Cubic) `f ( x ) = ax^ 3 + bx^ 2 + c x + d`

This continues further with increase in the degree of polynomial.

  • In MATLAB, for performing the curve fitting, the values of the coefficients in the equations are to be calculated which can be done using the ‘polyval’ command.
  1. The Goodness of Fit – The Goodness of Fit of any statistic data determines how well it is capable to fit a set of observations with regards to the original data set. For the measure of Goodness of Fit, there are four methods to quantify the same –
  1. Sum of Squares due to Error (SSE): This measures the total deviation of the response values from the fit. It is also referred to as the Square of Residuals. A value closer to 0 means a better fit. `SSE = sum_(i=1)^(n) "Error"(i)^2`
  2. R-Square: This statistic measures how successful the fit is in providing a variety of data. Put in another way, R-Square is the square of the correlation between the actual values and the predicted response values. Mathematically, it is the ratio of the Sum of Squares of Regression (SSR) and The Sum of Squares in Total (SST). `R^2 = "SSR"/"SST"` where, `SST = SSR + SSE` and, `SSR = sum_(i=1)^n ( f ( x ( i ) ) − M e a n )^2`

    R-Square can take any value between 0 and 1 where closeness to 1 denotes a better fit.

  3. Adjusted R-Square: This entity uses the R-Square mentioned above and adjusts it according to the residual degrees of freedom. Adjusted R-Square is a normalizing variation of R-Square which neglects its variation due to insignificant independent variables. Mathematically, `R^2adj = 1 - [((1 − R^2)(n − 1))/(n − k − 1)]`

    where n = Number of datapoints

    k = Number of independent variables

    Here, the Adjusted R-Square can take any values between 0 and 1 where closer to 1 denotes a better fit.

  4. Root Mean Squared Error (RMSE): This statistic is also known as the fit standard error and the standard error of the regression.

`RMSE = sqrt((SSE)/n)`

An RMSE value closer to 0 is considered as a good fit.

III. MATLAB PROGRAMS

A. Program for performing Curve Fitting of Linear and Cubic Polynomials

% A program to perform Curve Fitting for required cases

clear all
close all
clc

% Calling the required thermodynamic data
Cp_data = load('data');    % Provided data is loaded 
Temp = Cp_data(:,1);       % 1st column data is Temperature
Cp = Cp_data(:,2);         % 2nd column data is Specific Heat

% Curve Fit for Linear Polynomial 
% Cp_1 = a*x + b 
coeff_1 = polyfit(Temp,Cp,1);         % Coefficients are calculated for 1st degree
predict_cp_1 = polyval(coeff_1,Temp); % Predicted values are calculated

% Curve Fit for Cubic Polynomial 
% Cp_3 = a*x^3 + b*x^2 + c*x+ d 
coeff_3 = polyfit(Temp,Cp,3);         % Coefficients are calculated for 2nd degree
predict_cp_3 = polyval(coeff_3,Temp); % Predicted values are calculated

% Plot Commands
% First figure for Original vs Linear curve fit
figure(1)
plot(Temp,Cp,'linewidth',3,'color','b')
hold on
plot(Temp,predict_cp_1,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/K-mol K]')
axis([0 3750 900 1350])
legend('Original Data','Predicted Curve Fit','location','southeast')
title('Actual Curve vs Linear Curve Fit')
grid on

% Second figure for Original vs Cubic curve fit
figure(2)
plot(Temp,Cp,'linewidth',3,'color','b')
hold on
plot(Temp,predict_cp_3,'linewidth',3,'color','g')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/K-mol K]')
axis([0 3750 900 1350])
legend('Original Data','Predicted Curve Fit','location','southeast')
title('Actual Curve vs Cubic Curve Fit')
grid on

A.1. Steps for the above program: -

  1. The program is commenced using the basic 'clear all',' close all',' clc' commands.
  2. The thermodynamic data which has been provided shall be loaded in the script that can be done by using 'load('file_name')' command. Also, the two-column data have to be assigned to their respective categories i.e. Temperature and Specific heat.
  3. The coefficients for the polynomial have to be calculated by setting the degree for the same. This is done by using the 'polyfit' command as seen in the code. For the first one, the degree is set to 1 since it's linear.
  4. After obtaining the necessary data, the curve fit has to be performed by calculating the predicted data. This can be done by using the 'polyval' command.
  5. Steps 3 and 4 shall be repeated for this one but with a different coefficient variable and degree of the polynomial. Since we use a cubic polynomial, the degree should be 3.
  6. Plot commands are further given for creating both figure 1 and figure 2 separately. Here, for both, we plot the original data, hold the plot, and again plot the predicted curve fit data. All necessary labels, titles, and legends are fed in the code as seen above. The axes are fixed to limits and also the grid is turned on for reference.

A.2. Results from this program: -

  1. Original Data Curve vs. Predicted Linear Curve

Fig. 1

  • It is very evident from this plot resulted from the linear curve fit that the variation in the linear plot is not up to par with the original curve. Since a linear polynomial has just two coefficients, the plot is with respect to these two points only. This provides an inefficient curve fit.

2. Original Data Curve vs. Predicted Cubic Curve

Fig. 2

  • For the case of, the cubic polynomial does a better job of replicating the path of the original data curve. Since a cubic polynomial has four coefficients, it becomes easier to plot the data with the required variation. Though it is better than that of the linear plot, the cubic plot still does not thoroughly fit the curve.

A.3. Drawback/Error of this method: -

The method as seen in the aforementioned code is pretty straightforward to use but after running it one major warning is displayed on the command window as follows:

Fig. 3

The message prompts that the polynomial is 'badly conditioned' which means that the solution of the system of linear equations critically depends on rounding errors due to the limited precision. Fortunately, this issue can be worked around by the method of Centering and Scaling as suggested by MATLAB itself.

Now, this raises a few questions about the curve fit. These go about as follows:

Questions raised regarding the curve fit:-

Q.1. How to make a curve fit perfectly?

  • The solution for making a curve fit "perfectly" lies in calculating the error values encountered at the time of the process and then making the necessary tweaks. A Perfect curve fit means that the curve should fit the original data using the set polynomial degree without any errors. Mathematically, the value of R-Square (a parameter for the Goodness of Fit) should be equal to 1.
  • For our convenience, the parameters of the goodness of fit can be calculated simply by the use of a tool called Curve Fitting Tool which is an inbuilt feature of MATLAB.
  • Curve Fitting Tool
  • The Curve Fitting Tool is an alternative way in order to validate the fitness behavior of the curve. For using this the data has to be loaded in MATLAB first and then we can appoint the X and Y coordinate values for the data. Then, the degree of the polynomial has to be set from values 1 to 9 from the option bar. The tool will automatically create a plot of both the original as well as the predicted data set.
  • We use the keyword 'cftool' in the command window which opens up the Curve Fitting Tool. We feed in all the necessary data and tweak the degree of the polynomial until we get R-Square value as 1 in the result window.
  • For our case, the degree considered for getting a perfect square was 8. The result that was obtained from the tool can be observed in the picture below.

Fig. 4

It is very evident from the above result that the value of R-Square is equal to 1, which makes it a perfect fit for the curve.

Q.2. How to get the best fit?

  • A "Best Fitting Curve" means that it minimizes the sum of squares of differences between the actual measured values and the predicted values. The Best Fit can be obtained by calculating the Fitness characteristics of the curve. As done previously, we can employ the Curve Fitting tool for this job. We can take the same data and consider the linear as well as the cubic polynomial for comparing which one is the better fit.
  • The procedure for this is similar to that of discussed earlier. First, the degree of the polynomial is set as 1 and then the values of the parameters for Goodness of fit are noted. After this, the degree of the polynomial is increased to 3 and again the values are noted. The results for both circumstances can be seen below.
  • 1.) Linear Fit Characteristics

Fig. 5

The Goodness of Fit parameters can be seen in the red box above. The RMSE (Root Mean Square of Error) here is 26.01.

  • 2.) Cubic Fit Characteristics

Fig. 6

Here, the Goodness of Fit parameters is much different than that of the linear case. The RMSE (Root Mean Squared Error) here is 5.431 that is much lower than the linear case.

From the above results, it can be fairly concluded that the total deviation of the curve fit from the original data particularly for the case of the cubic polynomial is much less than the linear polynomial since the numerical value of RMSE is lower for the cubic polynomial.

Hence, it can be said that the Cubic fit is the Best Fit out of the two cases discussed above. Though it is not the only fit that can efficiently predict the path of the original data curve.

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

  • In order to improve the Cubic Curve Fit for the given data, we can choose to use the Split-wise method where we split the provided data set into a number of subdomains so that the equation gets conditioned. This procedure will successfully abolish the warning ("equation badly conditioned") and effectively fit the curve without using any higher degree polynomial. The program for performing this has been elaborated below:

B. Program for performing Curve Fitting for a Cubic Polynomial using Split-wise Method

% A program to perform a curve fit using the Split-wise method

clear all
close all
clc

% Calling the required thermodynamic data
Cp_data = load('data');    % Provided data is loaded 
Temp = Cp_data(:,1);       % Column 1 is Temperature
Cp = Cp_data(:,2);         % Column 2 is Specific heat

% Data Split into five equal parts
% Split 1
Temp_1 = Cp_data(1:640,1);       
Cp_1 = Cp_data(1:640,2); 

% Split 2
Temp_2 = Cp_data(641:1280,1);       
Cp_2 = Cp_data(641:1280,2);

% Split 3
Temp_3 = Cp_data(1281:1920,1);       
Cp_3 = Cp_data(1281:1920,2);

% Split 4
Temp_4 = Cp_data(1921:2560,1);       
Cp_4 = Cp_data(1921:2560,2);

% Split 5
Temp_5 = Cp_data(2561:3200,1);       
Cp_5 = Cp_data(2561:3200,2);

% Curve Fitting all split data
% Cubic polynomial for all sets
[coeff_1,s1,mu1] = polyfit(Temp_1,Cp_1,3);         
predict_cp_1 = polyval(coeff_1,Temp_1,s1,mu1);

[coeff_2,s2,mu2] = polyfit(Temp_2,Cp_2,3);         
predict_cp_2 = polyval(coeff_2,Temp_2,s2,mu2);

[coeff_3,s3,mu3] = polyfit(Temp_3,Cp_3,3);         
predict_cp_3 = polyval(coeff_3,Temp_3,s3,mu3);

[coeff_4,s4,mu4] = polyfit(Temp_4,Cp_4,3);         
predict_cp_4 = polyval(coeff_4,Temp_4,s4,mu4);

[coeff_5,s5,mu5] = polyfit(Temp_5,Cp_5,3);         
predict_cp_5 = polyval(coeff_5,Temp_5,s5,mu5);

% Plot Commands
plot(Temp,Cp,'linewidth',5,'color','b')
hold on
plot(Temp_1,Cp_1,'linewidth',2,'color','r')
plot(Temp_2,Cp_2,'linewidth',2,'color','r')
plot(Temp_3,Cp_3,'linewidth',2,'color','r')
plot(Temp_4,Cp_4,'linewidth',2,'color','r')
plot(Temp_5,Cp_5,'linewidth',2,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/K-mol K]')
axis([0 3750 900 1350])
legend('Original Data Curve','Split Data Curve Fit','location','southeast')
title('Actual Curve vs Modified Cubic Curve Fit')
grid on

B.1. Steps for the above program: -

  1. The program is commenced using the basic 'clear all',' close all',' clc' commands.
  2. The same thermodynamic data which has been provided shall be loaded in the script that can be done by using 'load('file_name')' command. Also, the two-column data have to be assigned to their respective categories i.e. Temperature and Specific heat.
  3. Now, this step is an alteration of the previous program. We divide all the 3200 elements of the given data into arrays of five sets by splitting them into equal intervals as seen in the code.
  4. This step employs the Centering and Scaling transformation. The 'polyfit' command is given which is defined by an output argument array of three elements [p, s, mu]; p - the least square polynomial coefficients, s - error estimation structure, and mu - the centering and scaling values. The degree is set as 3 since we are using the cubic polynomial. This has to be done for all five split sets of data.
  5. Finally, the plot commands are given for plotting the original data as well as the split curve fit data. Here the linewidth for the original data curve has been kept larger than the curve fit in order to distinguish both the curves. All the necessary labels, legends, and titles are provided. The axes are fixed to limits and the grid has been turned on for reference.

B.2. The result from the above program: -

Original Data Curve vs Split-wise Cubic Curve Fit

Fig. 7

The Split-wise method, from the above result, stands strong in terms of fitting the cubic polynomial over the original data. It can be clearly observed that the Curve fit traces a path similar to that of the original data plot.

B.3. Errors from the above program: -

No intricate errors were encountered while coding this program.

IV. CONCLUSION

In this report, the theory and application of Curve Fitting were discussed using MATLAB programs. Therefore, after learning about the topic and doing quite a lot of research for the same, a few conclusions can be made which are mentioned as follows: -

  1. Essentially, to get a 'Perfect fit', we are ought to increase the degree of governing polynomial since a higher degree equation has more coefficients that help in fitting the curve without any errors.
  2. Just increasing the degree of polynomial does not help. With the increasing degree of the polynomial, the equations also have to be conditioned for an efficient fit.
  3. There are a handful of methods that can be used for fitting curves in MATLAB with better efficacy. One of them is the Split-wise method which has been showcased in this report. Here, larger the number of splits, better the fineness of a curve fit.

V. REFERENCES

  1. Curve Fitting and the criteria to choose the best fit -
    https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
  2. Introduction to Curve Fitting Toolbox in MATLAB - https://skill-lync.freshdesk.com/support/solutions/articles/43000584172
  3. 'Polyfit' command in MATLAB -
    https://www.mathworks.com/help/matlab/ref/polyfit.html#bufcrts
  4. 'Polyval' command in MATLAB - https://in.mathworks.com/help/matlab/ref/polyval.html
  5. Curve Fitting with Linear and Non-Linear Regression - https://blog.minitab.com/blog/adventures-in-statistics-2/curve-fitting-with-linear-and-nonlinear-regression https://statisticsbyjim.com/regression/curve-fitting-linear-nonlinear-regression/
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.