Project

Write a MATLAB code to fit a Linear & Cubic polynomial curve for the Specific Heat (Cp) & Temperature data .

AIM : To perform a Curve Fitting for the given DATA_set & to Find the Best Fit & Perfect Fit for that particular DATA_set using MATLAB.

OBJECTIVE :

  • To Write code to fit a Linear & Cubic polynomial for the specific Heat (`C_p`) data .
  • To Plot the Linear & Cubic fit curve along with the raw data points .
  • To Study about the parameters used to Measure the Fitness charateristics for both the Curve .

Curve Fitting :

Curve Fitting is the process of Constructing a Curve or Mathematical Function , which possess closest proximity to the Original or Real series of DATA_set. By Curve Fitting , we can mathematically construct the fuctional relationship between the Observed DATA_set and Parametric values. I is the Statistical Technique use to drive Coefficient value fo euqtion that express tha value of one (Dependent) variable as aFunction of another (Independent) variable.It is Highly effective in the Mathematical Modelling of some natural processes .By Curve Fitting we can actually Study & Manipulate the Data for deifferent problems and different Condition.

This Method has very High use in Engineering Application & Science, So the question arises “ Why it is required to Study..? “

  • To carry out systems Simulation & Optimization .
  • To determine value of properties of intermediate points.
  • To do trend Analysis.
  • For Hypothesis Testing.

Suppose We have a set of n data susch as :

`(y1,x1) ,(y2,x2) ,(y3,x3) ,……… (yn,xn)`

The first step is to plot this data and Postulate (assume) a Function `f(x)` form to describe the general trend from those data.

How to fit a Curve ?

To fit a curve the parameter of the real dataset is function of undefined constant of `〖 n〗^th` order polynomial.

  • The 1st degree polynomial equation ,

`y=ax+b`

is aline with slope “a” .A line will connect any two points ,so as the 1st degree polynomial equation is an exact fit through any two points with distinct co-ordinates.It is also called as equation for linear fit.

  • If the order of degree is increased to a 2nd degree polynomial,the following results,

`y=ax^2+ bx+c`

This will exactly fit a simple curve to three points.

  • If the order of equation is increase to a 3rd degree polynomial ,the following is obtained,

`y=ax^3+ bx^2+ cx+d`

This is also called as Cubic Fit and it will fit the Curve in exactly 4 point,So as we increase the order of polynomial,the nature of Curve change accordingly.

Goodness of a Fit :

Regression :

It is the method of describing the reletion between one dependent variable `(y)` with series of changing variable which are known as independent variable , such that it is the best fit that satisfie entire range of independent variable.

In order to choose the best fit we follow Least Square Method.it is procedure of finding curve in set of data points by minimizing the sum of the Square of the Offset (Residual) of the point on the curve.

The sum of square of the offset is used instead of the offset absolute value, to permit the residual to be treated as a continuous differentiable quantity.

If we use Higher Exponent it would be harder to find minimum value `(S)` , and we could find possibly non unique minimums,we use square error because it gives us a minimum that is easy to find and is guaranteed to be the only minimum.

Total sample deviation = Explained deviation + unexplained deviation

Total sum of square (SST) = Sum of square of regression (SSR) + sum of square due to Error (SSE)

SST = SSR + SSE

SST = `(y_i-y ̅ )^2`

SSR = `(y ̂-y ̅ )^2`

SSE = `(y_i-y ̂ )^2`

Parameters used to Measure the Fitness Characteristics :

After finding the curve which satisfies the given data points , we need to check the fitness characteristics of the curve or how well the equation is representing the data points. This can be done by 4 quantities which help us measure the goodness of fit criteria . they are,

  • The Sum of Square due to Error (SSE)
  • R-Square
  • Adjusted R-Square
  • Root Mean Squared Error (RMSE)
  1. The Sum of Square due to Error (SSE) – Sum Of Square Error (SSE),is the sum of the square of the results or deviation predicted from actual value of data ,It is the measure of discrepancy between the data and an estimation model. A small SSE indicate a tight fit of the model to the data.

SSE = `∑_(i=1)^n▒〖Error (i)^2 〗`

  1. R-Square - R-Square is a statistical measure of how close the data are to the fitted Regression line

R-Square is always range between 0 & 1

  • 0 indicates that the model explains none of the variability of the response data around its mean.
  • 1 indicates that the model explains all the variability of the response data around its mean.
  • In general, the higher the R-squared, the better the model fits the real data

`R^2= SSR/SST`

Where,

SSR = Sum of Square of the Regression

SST = SSR + SSE

  1. Adjusted R-Square – Adjusted R-Square is the normalizing variation of R-Square due to insignificant independent variable, Adjusted R-Square disregards infinitesimally small increments to R-square, and only considers the variable which actually contribute to the output of the system in a drastic way.
  2. Root Mean Square Error : It is an estimate of the standard deviation of the random component in the data, and is defined as,

`RMSE= √(SSE/n)`

MATLAB Code For Linear and Cubic fit :

The code for Linear (1st ) order and Cubic (3rd ) order fit follows,

%  Code for Linear and Cubic Fit

clear all
close all
clc

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

% Linear Curve Fitting
% Cp = a*T + b
[co_eff1,S,mu] =  polyfit(temperature,cp,1);
predicted_cp1  =  polyval(co_eff1,temperature,[],mu);


% Cubic Curve Fitting
% Cp =  a*t^3 + b*T^2 + c*T + d
[co_eff3,S,mu] =  polyfit(temperature,cp,3);
predicted_cp3  =  polyval(co_eff3,temperature,[],mu);


% Compare Curve fit with the original data and plot
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
axis([0 4000 900 1400])
xlabel('Temperature [K]')
ylabel ('Specific Heat [KJ/Kmol-K]')
legend('Original Data','Linear Curvefit','Location','Southeast')
title('Linear Curve Fit')
grid on


figure (2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp3,'linewidth',3,'color','r')
axis([0 4000 900 1400])
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Data','Cubic Curvefit','Location','Southeast')
title('Cubic Curve Fit')
grid on


% R^2 Method to find Fitnes Charateristics of Curve

M = mean(cp);

for   i  =  1:length(cp)
    SSR1 = sum((predicted_cp1(i)-M)^2);
    SSE1 = sum((cp(i)- predicted_cp1(i))^2);
end

SST1 =  SSR1 +  SSE1;
R1   =  SSR1/SST1;

for  i   =  1:length(cp)
    SSR3 = sum((predicted_cp3(i)-M)^2); 
    SSE3 = sum((cp(i)-predicted_cp3(i))^2);
end

SST3 = SSR3 + SSE3 ;
R3   = SSR3/SST3 ;

MATLAB Code Explained :

  1. The code start with clear all; close all; clc, where,
  • “Clear all” is used to clear the data stored in workspace.
  • “Close all” command is used to close all plots and figures.
  • “Clc” command is used to clear the command window.
  1. Then the Data of Specific Heat is loaded with “load” command & “Temperature” as well as “Cp” array is defined.
  2. Then Curve fitting is performed by using two Functions.
  • Polyfit – p = polyfit(x,y,n) returns the co-efficient for a polynomial p(x) of degree ‘n’ that is the best fit (in least square sense) for the data in ‘y’ .The co-efficient in p are in descending power and the length p is n+1 .Here, the value on ‘n’ is “1” for Linear & “3” for Cubi,’x’ is ‘temperature’ and ‘y’ is ‘Cp’ data. So[co_eff,S,mu] = polyfit (x,y,n) also returns mu,which is two-element vector with Centering & Scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.
  • Polyval – polyval (co_eff, temperature) returns the value of a polynomial of degree ’n’ evaluated at ‘temperature’ .The input argument ‘co-eff’ is a vector of whose elements are the co-efficient in descending power of the polynomial to be evaluated .”Polyval” evaluate “co-eff” at each element of ‘temperature’. Here polyval (co_eff, temperature,[],mu) is use for Centering and Scaling parameter of ‘mu’ which is computed by polyfit.
  1. Then the curve fit data is compared with original data by plotting the curve for getting “Linear” &”Cubic” Curve Fitting. The Axis is shifted by using “axis” command and “Legend” is provided for the plot.
  2. Here we are using R-Square Method to evaluate the Fitness Charateristic of Linear & Cubic Curve respectively, as mention below.
  3. M = mean(cp) returns the mean of the elements of ‘cp’ along the 1st Array dimension.
  4. The value of SSR, SSE, SST and R-Square for Linear and Cubic polynomial in a”for” Loop and the value of R-Square are to be Displayed on command window.

Value From WorkSpace :

1. Linear Curve Fit –

2. Cubic Curve fit -

RESULTS for Linear and Cubic fit :

The value of R-Square for Linear fit is found to be 0.9592 & that’s of Cubic fit is to be 0.9907.As we know that the Higher the value of R-Square, better the fitness Characteristics. Here the value of Cubic Fit is higher than Linear fit. So Cubic Curvefit has better fitness characteristic than Linear Curvefit. Also for both the Graph we can see that Cubic Curvefit has better Resemblance to the original curve compared to linear curvefit.

Alternative way to validate Fitness characteristics :

Curve fitting toolbar is another way to validate Fitness characteristic Curve. We just have to prepare the data first and then open the “Curve Fitting Toolbar” then select the “x” & “y” values. The Toolbar will Automatically plot the original curve,Then we have to select the degree of polynomial to fit the original curve. This will plot the curve accordingly to the Degree of polynomial as well as it will display the value of R-Square, SSE, Adjusted R-Square and RMSE.

Curve fitting Tool plot for Linear curve :

Curve fitting Tool plot for Cubic curve :

As we can see that value of SSR, R-Square and RMSE are displayed under “ Goodness of Fit “ and these values are very close to the value that we found in our Code.

What could be Done to improve the cubic fit ?

When “polyfit” and “polyval” command for cubic fit is used as,

[co_eff3,S,mu] =  polyfit(temperature,cp,3);
predicted_cp3  =  polyval(co_eff3,temperature,[],mu);

We get a warning as “The polynomial is badly condition”. This problem can be solved by Centering and Scaling the point of curve fitting equation to get better results. Centering a variable is Subtracting the mean of the variable from each data point so that the new variables mean is 0, Scaling a variable is multiplying each data point by a constant in order to alter the range of the data. As discussed earlier,”mu” which is two element vectors with centering and scaling values is entered into the code. The comparison is shown below.

Without Centering and Scaling With Centering and Scaling

Best fit and Perfect fit :

Best fit – Best fit is simply means that the difference between the actual measured values and the value predicted by the model equation are minimized. It does not mean the ‘Perfect Fit’. In most cases a least squares best fit does not go through all the point in the data set.

Perfect Fit – Perfect fit means the curve should fit the original curve without showing any error or residual values. If the R-Square value of the curve is exactly 1.0000, then the curve is said to be perfect fit. Perfect fit can be best fit but the best fit can’t be perfect fit ,This is the basic difference between Best fit and Perfect fit.

How to get the Best Fit ?

  • To get the Best fit , we should increase the degree of polynomial of the equation and reduce the residual errors.
  • By doing so ,we can get value of R-Square close to 1
  • As stated in the results, the best curve fit from the above program in Cubic curve fit as it value of R-Square = 0.9907.

How to make Curve fit perfectly …?

To get the perfect fit , we need to get the value of R-Square exactly equal to 1 . one such solution can be trial and error method to increase the degree of polynomial of the equation using the Curve Fit Toolbox for increasing the degree of Polynomial to 8 , we get a perfect Fit as Shown.

But due to this , the curve fitting equation becomes more complex as there are 9 number of constant to be determined.

Another way to achieve Perfect Fit is to SPLIT the data into equal sets and then plot the curve . This is called SPLITWISE Method ,where we split thye data of specific heat (Cp) and its corresponding temperature value and we will execute the program with same order of polynomial for each data set.

Program to Split the Data and Plot Curve Fitting :

% Program to Split the data and plot the curve

clear all
close all
clc


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


%Data Splitting
temperature1 = cp_data(1:400,1);
cp1  =  cp_data(1:400,2);


temperature2 = cp_data(401:800,1);
cp2  =  cp_data(401:800,2);


temperature3 = cp_data(801:1200,1);
cp3  =  cp_data(801:1200,2);


temperature4 = cp_data(1201:1600,1);
cp4  =  cp_data(1201:1600,2);

temperature5 = cp_data(1601:2000,1);
cp5  =  cp_data(1601:2000,2);


temperature6 = cp_data(2001:2400,1);
cp6  =  cp_data(2001:2400,2);


temperature7 = cp_data(2401:2800,1);
cp7  =  cp_data(2401:2800,2);


temperature8 = cp_data(2801:3200,1);
cp8  =  cp_data(2801:3200,2);



%Cubic Fit with Centering and Scaling

[co_eff1,S,mu] =  polyfit(temperature1,cp1,3);
predicted_cp1 =  polyval(co_eff1,temperature1,[],mu);

[co_eff2,S,mu] =  polyfit(temperature2,cp2,3);
predicted_cp2 =  polyval(co_eff2,temperature2,[],mu);

[co_eff3,S,mu] =  polyfit(temperature3,cp3,3);
predicted_cp3 =  polyval(co_eff3,temperature3,[],mu);

[co_eff4,S,mu] =  polyfit(temperature4,cp4,3);
predicted_cp4 =  polyval(co_eff4,temperature4,[],mu);

[co_eff5,S,mu] =  polyfit(temperature5,cp5,3);
predicted_cp5 =  polyval(co_eff5,temperature5,[],mu);

[co_eff6,S,mu] =  polyfit(temperature6,cp6,3);
predicted_cp6 =  polyval(co_eff6,temperature6,[],mu);

[co_eff7,S,mu] =  polyfit(temperature7,cp7,3);
predicted_cp7 =  polyval(co_eff7,temperature7,[],mu);

[co_eff8,S,mu] =  polyfit(temperature8,cp8,3);
predicted_cp8 =  polyval(co_eff8,temperature8,[],mu);



% Compare Curve Fit with the Original data

plot(temperature,cp,'linewidth',3,'color','b');
hold on

plot(temperature1,predicted_cp1,'linewidth',2,'color','y');
hold on
plot(temperature2,predicted_cp2,'linewidth',2,'color','m');
hold on
plot(temperature3,predicted_cp3,'linewidth',2,'color','c');
hold on
plot(temperature4,predicted_cp4,'linewidth',2,'color','r');
hold on
plot(temperature5,predicted_cp5,'linewidth',2,'color','g');
hold on
plot(temperature6,predicted_cp6,'linewidth',2,'color','b');
hold on
plot(temperature7,predicted_cp7,'linewidth',2,'color','w');
hold on
plot(temperature8,predicted_cp8,'linewidth',2,'color','k');
axis([0 4000 900 1400]);
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kmol-K]');
legend('Original Data','Split Curvefit1','Split Curvefit2','Split Curvefit3','Split Curvefit4','Split Curvefit5','Split Curvefit6','Split Curvefit7','Split Curvefit8','Location','Southeast');
title('Cubic Curve Fit with Splitted Data');
grid on

Here is the code, we have splitted the “cp” data into 8 equal data set and for each data set we have assumed the degree of polynomial as ’3’.

Program Output :

From the above Curve plot, we can say that the curve for “original data” is not visible as the “Splitwise data Curvefit “has perfectly fitted the Original Curve.

CONCLUSION :

  • From this Report, we can state that there are different way to perform Curve Fitting.
  • We need to have high order polynomial equation to achieve Best Fit; the higher the order of the polynomial, the better is the Curve.
  • Splitwise Method helps in getting Perfect Fit for the Original Curve. The main factor is Splitting the data is “the number of Split “. More the number of Splits, Better the Output curve.
  • With the help of these methods, we can predict and manipulate the different problem and condition for future study.

REFERENCE:

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.