Project

Program In Matlab For Curve Fitting

  • AIM:

In this project, we are going to plot some data of Specific Heat[cp] of Oxygen as a function of Temperature and then try to fit a curve for this plot. From this Curve Fitting, we can do additional calculation and prediction for cp values. Curve Fitting is a mathematical function that is highly effective in the mathematical modeling of some natural processes.

  • PARAMETERS USED TO MEASURE THE FITNESS CHARACTERISTICS OF CURVES:

Some parameters help us to find out the best fit for curves. To fit a curve on the observed data set is not the ultimate thing. We have to measure some parameter that gives us a clear idea about the goodness of fit criteria or how well the equation is a representing the data points. These parameters are -

  1. The Some Of Squares Due To Error [SSE].
  2. R- Square.
  3. Adjusted R-Square.
  4. Root Mean Square Error [RMSE].

By using this parameter, we can determine the effectiveness of Curve Fitting. If R-Square is greater than 0.95 then the fit is assumed to be good.

  • GOVERNING EQUATIONS:

Taking some data points[(`x_1,y_1`),..,(`x_6,y_6`)] from input data sets[(`x_1,y_1`),..,(`x_n,y_n`)] and plotting them. Let's plot a Curve Fitting [of 2nd order] for these data points. So, the plots look like -

- Now, calculate Error for 'i' point [`1<= i <= n`], Error = |y(i) - value of f(x) for x=i| = |y(i) - f(x(i))|

- To remove the modulus sign, it is effective if we square the error term, when we square the error terms and add the errors for all the 'n' points we get,

SSE(The Some Of Squares Due To Error) = `sum_(i=1)^n`Error`(i)^2`

- The value of R-square can be calculated using the following formula, `R^2` = `(SSR)/(SST)`

Where, SSR = sum of squares of the regression, which is basically the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the data set.

If, y= f(x) is the curve then SSR is defined as - SSR = `sum_(i=1)^n``(f(x) - Mean)^2`, where, Mean =`sum_(i=1)^n`f(x) / n

Now the term SST is given by summation of SSR and SSE. So, SST = SSR + SSE

- Adjusted R-Square is a normalizing variation of R-Square which neglects the variation of R-Square due to insignificant independent variables. It is calculated by -

Adjusted R-Square =`1- [((1-R^2)*(n-1))/(n-k-1)]` , where, n= no of data points, k = no of the independent variable.

- RMSE is an estimate of the standard deviation of the random component in the data, can be calculated by - RMSE = `sqrt((SSE)/n)`

  • OBJECTIVE: Main objectives of this project are -

1. To write a program to fit curves for a linear and cubic polynomial for the Cp data.

2. To Plot the linear and cubic fit curves along with the raw data points.

3. To verify the fitness of the curve fitting.

Program In Matlab For Curve Fitting -

clc
close all
clear all

% Program for Curve fitting - 
 
 data = load('data'); % To load the data file that consist of values of cp & temperature.
 temp = data (:,1); % To extract temperature values from data
 cp   = data (:,2); % To extract cp values from data 
 

 
                    % Code for linear curve fitting -
   
                        % cp = (a*temp) + b
     
 Co_eff = polyfit(temp,cp,1); % To determine the Co-efficients for the linear Curve
 Predicted_cp = polyval(Co_eff,temp); % To determine the Predicted values for Specific Heat
  
% Plotting for figure(1)
 
 figure(1);
 plot(temp,cp,'linewidth',3);
 hold on 
 plot(temp,Predicted_cp,'linewidth',3);
 xlabel('Temperature [K]');
 ylabel('Specific heat [KJ/ mol-k]');
 legend('Originial cp','Linear Curve fitting ')
 title ('Specific Heat Vs Temperature [For Linear Curve Fitting]')
 
% Parameter[R_square] to validate best fit -
 
 Total_cp = 0;
for i= 1: length(data(:,1))
 Total_cp = Total_cp + cp(i); % To calculate summation of all cp values
     end
 mean = Total_cp / 3200 ; % To calculate mean of cp values
   
            % To calculate sum of squares of the regression[SSR]
 SSR =0;
 for j = 1: length(data(:,1))
 SSR = SSR + (Predicted_cp(j) - mean )^2 ;
     end
 
             % To calculate sum of squared errors[SSE]
 SSE = 0;
 for k = 1: length(data(:,2))
   SSE = SSE + (cp(k) - Predicted_cp(k))^2;
    end
    
 
  R_square_Linear =  (SSR /(SSR + SSE))
  
  if  R_square_Linear >= 0.95 
    RESULT_1 = sprintf ('LINEAR CURVE FITTING IS GOOD')
  else  RESULT_1 = sprintf('LINEAR CURVE FITTING IS NOT GOOD')
     endif

     
                     % Code for Cubic curve fitting - 
     
                  % cp = a*temp^3 + b*temp^2 + c*temp + d
                  
 Co_Eff = polyfit(temp,cp,3);  % To determine the Co-efficients for the cubic Curve
 Predicted_cp_3 = polyval(Co_Eff,temp);
 
    % Plotting for figure(2)
 
 figure(2);
 plot(temp,cp,'linewidth',3);
 hold on 
 plot(temp,Predicted_cp_3,'linewidth',3);
 xlabel('Temperature [K]');
 ylabel('Specific heat [KJ/ mol-k]');
 legend('Originial cp','Cubic Curve fitting ')
 title ('Specific Heat Vs Temperature [For Cubic Curve Fitting]')
 
% Parameter[R_square] to validate best fitting -
 
             % To calculate sum of squares of the regression[SSR]
 SSR_3 =0;
 for m = 1: length(data(:,1))
 SSR_3 = SSR_3 + (Predicted_cp_3(m) - mean )^2 ;
     end

             % To calculate sum of squared errors[SSE]
 SSE_3 = 0;
 for n = 1: length(data(:,1))
   SSE_3 = SSE_3 + (cp(n) - Predicted_cp_3(n))^2;
    end
    
 
  R_square_Cubic =  (SSR_3 /(SSR_3 + SSE_3))
  
  if  R_square_Cubic >= 0.95 
    RESULT_2 = sprintf ('CUBIC CURVE FITTING IS GOOD')
  else  RESULT_2 = sprintf('CUBIC CURVE FITTING IS NOT GOOD')
     endif    

From this program, we get two graph-plots, these are-

By observing these two plots, we can deduce that Cubic curve fitting is better to fit the plot of the original data set. To conclude the deduction, we can see the R-Square values for this curve fitting. The values of R_Square are displayed in the command window, values are -

In the case of Linear curve fitting, the value of R-Square is less than 0.95 & for Cubic curve fitting, the value of R-Square is more than 0.95 & also close to the value of 1.

Now, we can able to answer some questions related to curve fitting after successfully run the program. These are-

1. How to make a curve fit perfectly?

In Perfect fit, without showing the centering error and scaling error, the curve fit should fit the original curve in that particular order of the polynomial. A perfect fit can always be the best fit but the best fit can not be a perfect fit always. We can improvise perfect fit by minimizing error and making it to the best fit.

2. How to get the best fit?

First of all the Best fit and a Perfect fit are two completely different phenomena. To get the best fit, we can check the parameters for the characteristic curve fitting. We can increase the order of Polynomial to get the best fit. In some cases, we can use the Split-wise method to get the best fit.

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

By using the Split-Wise Method, we can improve a curve fit for that particular order of Polynomial. We must split the original data set into some small ranges, such that we should compute different co-efficient for the curve fit functions for the small ranges. At last by combining these values, we can plot over the entire data set.

Here is the program to verify the improvement of curve fit -

 clc
 clear all
 close all
 
 
 % Program for Curve fitting - 
 
 data = load('data'); % To load the data file that consist of values of cp & temperature.
 temp = data (:,1); % To extract temperature[temp] values from data
 cp   = data (:,2); % To extract Specific Heat[cp] values from data 
 
 
 
 
 % Code for Cubic curve fitting with split - 
     
                  % cp = a*temp^3 + b*temp^2 + c*temp + d
                  
 temp_1 = data(1:800,1);     % Temperature values for 1st split
 temp_2 = data(801:1600,1);  % Temperature values for 2nd split
 temp_3 = data(1601:2400,1); % Temperature values for 3rd split
 temp_4 = data(2401:3200,1); % Temperature values for 4th split
 
 cp_1 = data(1:800,2);       % Specific Heat values for 1st split
 cp_2 = data(801:1600,2);    % Specific Heat values for 2nd split
 cp_3 = data(1601:2400,2);   % Specific Heat values for 3rd split
 cp_4 = data(2401:3200,2);   % Specific Heat values for 4th split
 
 Co_Eff = polyfit(temp,cp,3);  % To determine the Co-efficients for the Curve
 Predicted_cp = polyval(Co_Eff,temp); % To determine the Predicted values for Specific Heat
 
 Co_Eff_1 = polyfit(temp_1,cp_1,3);
 Predicted_cp_1 = polyval(Co_Eff_1,temp_1);
 
 Co_Eff_2 = polyfit(temp_2,cp_2,3);
 Predicted_cp_2 = polyval(Co_Eff_2,temp_2);
 
 Co_Eff_3 = polyfit(temp_3,cp_3,3);
 Predicted_cp_3 = polyval(Co_Eff_3,temp_3);
 
 Co_Eff_4 = polyfit(temp_4,cp_4,3);
 Predicted_cp_4 = polyval(Co_Eff_4,temp_4);
 
    % Plotting for figure(1)
 
 figure(1);
 plot(temp,cp,'linewidth',3);
 hold on 
 plot(temp,Predicted_cp,'linewidth',3);
 xlabel('Temperature [K]');
 ylabel('Specific heat [KJ/ mol-k]');
 legend('Originial cp','Cubic Curve fitting ')
 title ('Specific Heat Vs Temperature [For Cubic Curve Fitting Without Splitting]')
 hold off 
     
     % Plotting for figure(2)  
     
 figure(2);
 plot(temp,cp,'linewidth',3,'b');
 hold on 
 plot(temp_1,Predicted_cp_1,'linewidth',3,'r');
 plot(temp_2,Predicted_cp_2,'linewidth',3,'r');
 plot(temp_3,Predicted_cp_3,'linewidth',3,'r');
 plot(temp_4,Predicted_cp_4,'linewidth',3,'r');
 xlabel('Temperature [K]');
 ylabel('Specific heat [KJ/ mol-k]');
 legend('Originial cp','Cubic Curve fitting ')
 title ('Specific Heat Vs Temperature [For Cubic Curve Fitting With Splitting]')
 
% Parameter[R_square] to validate best fitting -
 
 Total_cp = 0;
for i= 1:800
 Total_cp_1 = Total_cp + cp_1(i); % To calculate summation of cp values for 1st split
 Total_cp_2 = Total_cp + cp_2(i); % To calculate summation of cp values for 2nd split
 Total_cp_3 = Total_cp + cp_3(i); % To calculate summation of cp values for 3rd split
 Total_cp_4 = Total_cp + cp_4(i); % To calculate summation of cp values for 4th split
 
     end
 mean_1 = Total_cp_1 / 800 ; % To calculate mean of cp values for 1st split
 mean_2 = Total_cp_2 / 800 ; % To calculate mean of cp values for 2nd split
 mean_3 = Total_cp_3 / 800 ; % To calculate mean of cp values for 3rd split
 mean_4 = Total_cp_4 / 800 ; % To calculate mean of cp values for 4th split
 
             % To calculate sum of squares of the regression[SSR]
 SSR =0;
 for m = 1: 800
 SSR = SSR + (Predicted_cp_1(m) - mean_1 )^2 ;
 SSR = SSR + (Predicted_cp_2(m) - mean_2 )^2 ;
 SSR = SSR + (Predicted_cp_3(m) - mean_3 )^2 ;
 SSR = SSR + (Predicted_cp_4(m) - mean_4 )^2 ;
     end
 
               % To calculate sum of squared errors[SSE]
 SSE = 0;
 for n = 1: 800
   SSE = SSE + (cp_1(n) - Predicted_cp_1(n))^2; 
   SSE = SSE + (cp_2(n) - Predicted_cp_2(n))^2;
   SSE = SSE + (cp_3(n) - Predicted_cp_3(n))^2;
   SSE = SSE + (cp_4(n) - Predicted_cp_4(n))^2;
    end
    
 
  R_square =  (SSR /(SSR + SSE))
  
  if  R_square >= 0.95 
    RESULT_FOR_SPLITTING = sprintf ('CUBIC CURVE FITTING IS GOOD')
  else  RESULT_FOR_SPLITTING = sprintf('CUBIC CURVE FITTING IS NOT GOOD')
     endif

We get two different plots, one without the Split-wise method and another one with this method. These are-

R-Square value for this method is displayed on the command window as-

During this project, some Error has been faced. After spotting the errors, these are eliminated, at the last program runs smoothly. Some of the error has been faced during writing the code for R-Square, & we got the wrong result. During the split wise method, we should concern about the ranges.

Here, the `1^(st)` image of the error was due to the wrong input of the range. As, after splitting the entire range into small ranges, we should use small range value to determine coefficients of the curve fit as well as for other variables. In the `2^(nd)` image of the error was due to not considering the mean values for small ranges. We must determine the separate value of means for separate ranges for calculation.

  • CONCLUSION:

In this project, we get three different values of R-Square for three different cases. In the ` 1^(st)` case, the R-Square value for the Linear Curve Fitting is 0.92488, which is less than 0.95. So, we can conclude that this linear curve fit is not good. So further, the order of the polynomial function is increased to cubic. In that case ( `2^(nd)` ) value of the R-Square is 0.99673, which is more than 0.95 & also close to 1. So, we can conclude that this cubic curve is good. Then, it further improvises for that cubic order by using the Split-wise method. In that `3^(rd)` case value of R-Square is 1.000. So, we can conclude that, this curve is the best fit for the given original data set.

  • REFERENCES:
  1. https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
  2. https://www.mathworks.com/help/matlab/ref/linespec.html
  3. http://asciimath.org/#syntax
  4. https://skill-lync.com/projects/Week-4-Curve-fitting-31550
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.