CURVE FITTING USING MATLAB

AIM:

      To write a code in matlab to fit a linear and cubic polynomial for the Cp data provided and to plot the linear and cubic fit curves along with the raw data point.

CURVE FITTING:

      Curve fitting is the process of constructing a curve or a mathematical function that has best fit to a series of data points.

GOODNESS OF FIT:

      Goodness-of-fit tests are statistical tests aiming to determine whether a set of observed values match those expected under the applicable model.

TYPES OF GOODNESS OF FIT TESTS:

          1.The sum of squares due to error.

          2.R-square.

          3.Adjusted R-square.

          4.Root mean squared error.

  • SSE:

      This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.

                                SSE

  • R-SQUARE:

      This statistic measures how successful the fit is in explaining the variation of the data.

                                          

          SSR-  It is the sum of the differences between the predicted value and the mean of the dependent variable.

                        

          SST- It is the squared differences between the observed dependent variable and its mean.

                        

  • ADJUSTED R-SQUARE:

       This statistic uses the R-square statistic defined above, and adjusts it based on the residual degrees of freedom. The residual degrees of freedom is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.

     k=n-m,v indicates the number of independent pieces of information

                      

  • RMSE:

       This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as

             

      

PROGRAM:

clear all
close all
clc

%loading data
cp_data=load('data');   
temperature=cp_data(:,1);  
cp=cp_data(:,2);
mean_cp=mean(cp);
n=3200;

%curve fitting

%cubic polynomial
%cp=ax^3+bx^2+cx+d
co_eff_1=polyfit(temperature,cp,3)
predicted_cp_1=polyval(co_eff_1,temperature)

%plotting curve fit with original data set
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp_1)
title('Cubic Polynomial Curve Fit')
xlabel('Temperature[K]')
ylabel('Specific heat [KJ/Kmol-K]')
grid on
axis([0 4000 900 1500])
legend('original data','curve fit','location','northeast')

%calculation of goodness of fit of cubic polynomial
error_1=cp-predicted_cp_1
sse_cubic=sum(error_1.^2)
ssr_cubic=sum((predicted_cp_1-mean_cp).^2)
sst_cubic=sse_cubic+ssr_cubic
rsquare_cubic=ssr_cubic/sst_cubic
rmse_cubic=sqrt(sse_cubic/n)

%linear polynomial
%cp=ax+b
co_eff_2=polyfit(temperature,cp,1);
predicted_cp_2=polyval(co_eff_2,temperature);

%plotting curve fit with original data set
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp_2)
title('linear Polynomial Curve Fit')
xlabel('Temperature[K]')
ylabel('Specific heat [KJ/Kmol-K]')
grid on
axis([0 4000 900 1500])
legend('original data','curve fit','location','northeast')

%calculation of goodness of fit of linear polynomial
error_2=cp-predicted_cp_2
sse_linear=sum(error_2.^2)
ssr_linear=sum((predicted_cp_2-mean_cp).^2)
sst_linear=sse_linear+ssr_linear
rsquare_linear=ssr_linear/sst_linear
rmse_linear=sqrt(sse_linear/n)

 

INPUTS:

         The inputs for our code is a cp data file which is a [3200 x 2] matrix. In this matrix the 1st column represents the 'Temperature' whose unit is [K] and the second column represents the 'Specific Heat' whose unit is [KJ/KMOL-K].

EXPLANATION  OF CODE:

STEP 1:

          The 1st step is to clear and close the previous program files using clear all, close all, clc commands and then to load the cp data file using load command under the variable name cp_data. Then to provide the values for variables temperature,cp,mean_cp and then n.

STEP 2:

          The 2nd step is curve fitting for a cubic polynomial 'cp=ax^3+bx^2+cx+d' and for a linear polynomial 'cp=ax+b' by using the polyfit command to find the coefficients for a order of 3 and 1 for a cubic and linear polynomial then followed by using the polyval command to find the predicted cp values by comparing the temperature and the found coefficient values of respective order.

STEP 3:

          The 3rd step is to plot the cubic polynomial and linear polynomial data set with the original data set by using the plot command.

      linearplot            cubicplot

STEP 4:

          The 4th step is to calculate the goodness of fit for both a cubic polynomial and linear polynomial using the above explained types of goodness of fits.

Calculated GOF values using Formulas.

1.Cubic Polynomial

                           CUBIC1

2.Linear Polynomial

                           LINEAR1

          Here in our program we have found the goodness of fits using both formulas and also by using the curve fit app.

Calculated GOF values using Curve Fit App.

1.Cubic polynomial

cubicgof

2.Linear polynomial

linear gof

 

ERRORS FACED DURING PROGRAMMING:

1.The most comman errors faced during programming are typo errors

    

STEPS TAKEN TO OVERCOME THE ERRORS:

1.By viewing at the red coloured hints popped out in the command window

2.By programing with concentration.

3.By recalling the instructions provided by the instructor during the course.

CONCLUSION:

           By comparing the above curve fitting results of the linear and cubic polynomial with raw data points we conclude that the curve fits perfectly as the order of polynomial increases.

QUESTIONS:

1.How to make a curve fit perfectly?

         From the above results we can increase the order of the polynomials to make the curve fit perfectly. More the order of the polynomial perfect the curve fit is or else we can split the provided data and plot them to make the curve fit properly.

2.How to get the best fit?

         By calculating different types of goodness of fits we can determine whether the fit is best fit or not. From our above results for a cubic polynomial, since R-SQUARE value is greater than 0.95 i.e., 0.9967 the fit is considered as a best fit. The best fit has an minimal error from the original value.

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

         To improve the cubic fit we can split the provided data and plot it in a sequence or by increasing their order of polynomial.

PROGRAM:

clear all
close all
clc

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

%cubic polynomial
%cp=ax^3+bx^2+cx+d
%splitting the data
temperature_1=cp_data(1:400,1);
cp_1=cp_data(1:400,2);
co_eff_1=polyfit(temperature_1,cp_1,3);
predicted_cp_1=polyval(co_eff_1,temperature_1);

temperature_2=cp_data(401:800,1);
cp_2=cp_data(401:800,2);
co_eff_2=polyfit(temperature_2,cp_2,3);
predicted_cp_2=polyval(co_eff_2,temperature_2);

temperature_3=cp_data(801:1200,1);
cp_3=cp_data(801:1200,2);
co_eff_3=polyfit(temperature_3,cp_3,3);
predicted_cp_3=polyval(co_eff_3,temperature_3);

temperature_4=cp_data(1201:1600,1);
cp_4=cp_data(1201:1600,2);
co_eff_4=polyfit(temperature_4,cp_4,3);
predicted_cp_4=polyval(co_eff_4,temperature_4);

temperature_5=cp_data(1601:2000,1);
cp_5=cp_data(1601:2000,2);
co_eff_5=polyfit(temperature_5,cp_5,3);
predicted_cp_5=polyval(co_eff_5,temperature_5);

temperature_6=cp_data(2001:2400,1);
cp_6=cp_data(2001:2400,2);
co_eff_6=polyfit(temperature_6,cp_6,3);
predicted_cp_6=polyval(co_eff_6,temperature_6);

temperature_7=cp_data(2401:2800,1);
cp_7=cp_data(2401:2800,2);
co_eff_7=polyfit(temperature_7,cp_7,3);
predicted_cp_7=polyval(co_eff_7,temperature_7);

temperature_8=cp_data(2801:3200,1);
cp_8=cp_data(2801:3200,2);
co_eff_8=polyfit(temperature_8,cp_8,3);
predicted_cp_8=polyval(co_eff_8,temperature_8);

%plotting original data
plot(temperature,cp,'linewidth',4,'color','b')
hold on
plot(temperature_1,predicted_cp_1,'linewidth',2,'color','r')
plot(temperature_2,predicted_cp_2,'linewidth',2,'color','r')
plot(temperature_3,predicted_cp_3,'linewidth',2,'color','r')
plot(temperature_4,predicted_cp_4,'linewidth',2,'color','r')
plot(temperature_5,predicted_cp_5,'linewidth',2,'color','r')
plot(temperature_6,predicted_cp_6,'linewidth',2,'color','r')
plot(temperature_7,predicted_cp_7,'linewidth',2,'color','r')
plot(temperature_8,predicted_cp_8,'linewidth',2,'color','r')
axis([0 4000 900 1400])
xlabel('Temperature[K]')
ylabel('Specific Heat [KJ/Kmol-K]')
grid on
legend('original data','curve fit','location','northeast')

RESULT:

        

CONCLUSION:

                  From the above result we have concluded that the cubic fit can be improved by splitting the data.

REFERENCES:

https://365datascience.com/sum-squares/

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

https://www.wikihow.com/Calculate-the-Sum-of-Squares-for-Error-(SSE)

https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

 


Projects by Avinash A S

AIM:      To perform a car crash analysis using ansys workbench. PROBLEM STATEMENT: To perform a parametric study using 3 different values of thickness for the car body. Here the car body remains stationary but the wall displaces to a distance of 500 Read more

AIM:     To perform structural analysis to simulate the rolling operation on a copper workpiece of length 90mm. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the effects of loads and internal forces on a struc Read more

AIM:    To perform explicit dynamics simulation of a bullet penetrating into a bucket considering three different non-linear materials for the bucket and analyse their behaviour for the same bullet velocity. CASE SETUP:1 Assign the bucket material as Copp Read more

AIM:

    To perform an explicit dynamics simulation to simulate the machining operation with using a planer and observe their behaviour at 2 different velocities.

CASE SETUP:1Read more

AIM:    To perform the tension and torsion test on the specimen considering the case setups and find out the total deformation, equivalent stress and temperature of the specimen. TENSION TEST: A tensile test, also known as a tension test, is one Read more

AIM:    To perform transient structural analysis on a worm gear assembly. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process of calculating and determining the effects of loads and internal forces that are a function of time on a Read more

AIM:     To perform a transient structural analysis on a double universal joint with a spring using three different materials and then compare the results. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process of calculating and Read more

AIM:       To perform a transient structural analysis on a piston and cam mechanism model considering different case setups and then compare the results of the case setups. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process o Read more

AIM:      To perform structural analysisto simulate a sphere pressing on plate considering the case setup and experience the plastic deformation on the plate. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the Read more

AIM:    To perform an structural analysis to simulate the bending of a wire considering three different case setups and compare their results. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the effects of loads and Read more


Loading...

The End