Project

Project on Curve Fitting

Project:-- Cuve Fitting

Aim of the project:-

To write code in MATLAB to fit a linear & cubic polynomial for cp data. To plot the curves obtained from linear & cubic polynomial. To find a best fit curve.

Code to fit linear & cubic polynomial for cp data & ploting of curves.

%curve_fitting - linear & cubic polynomial

clc
clear all

%Preparing data
cp_data=load('data'); %[data file given is loded in the varriable 'cp_data']
temp=cp_data(:,1);  %[data on tempereture is stored in column matrix 'temp']
cp=cp_data(:,2);    %[data on cp is stored in column matrix 'cp']

%curve fit

%cp=a*T+b (Linear)
[cp1,gof]=fit(temp,cp,'poly1','Normalize','on')
                   %[two no co efficient of linear polynomial are
                   %calculated by 'fit' function]
                   %[goodness of curve fittings is assessed for linear curve]
%cp=a*T^3+b*T^2+c*T+d (Cubic)
[cp3,gof]=fit(temp,cp,'poly3','Normalize','on')
                     %[[Four number co efficients of cubic polynomial are
                     %calculated by 'fit' function.]
                      %[goodness of curve fittings is assessed for cubic curve]

%Compare curve_fit_data with the original data
plot(temp,cp,'linewidth',1,'color','r');
hold on
plot(cp1,'g');      %[Plotting of curve for original data, linear & 
plot(cp3,'b');      % cubic polinomial]     
%hold off
xlabel('Temperature[K]')
ylabel('specific heat[kJ/kmol-K]')
title('Curve fitting, Linear & Cubic')
legend('original data set','data-linear','data-cubic','location','northwest')

In the code given above, at first, the "data ' file (the data file contains data of cp at various temperature) is loaded in matrix 'cp_data'. This data file should be in same directiry as of code file.

Here linear (degree of polynomial-1) & cubic (degree of polynomial-3) polynomial equations are framed. The coefficients of those polynomial equations are calculated with help of given values of temperature & cp, to find the new set of values of cp(i.e cp1,cp3) according to linear & cubic polynomial equations respectively, by the help of 'fit' function.

Number of co-efficients of a polynomial equation is always greater than the degree of the polynomial by 1.

For example here,in the cubic equation framed in the codeing, no. of co-efficient is 4 which are p1,p2,p3 & p4 accoding to the screenshot given below along with their values. The cubic polynomial equation framed here is

cp3(x)=p1*x^3 + p2*x^2 + p3*x + p4

During calulating the coefficient of linear & cubic polynomial equation,the input data are 'Normalized' by the default method'Zscore'. In this method input data are 'Normalized' by centering to have mean '0' & scaling it to have standard deviation 1.

Here 'fit' function calculates these new data set of cp (according to the linear /cubic polynomial equation framed) by Regression analysis & the method applied here is 'Method of Least sqaure'

On the basis of linear & cubic polynomials equation framed, respective curves are ploted along with the curve for given /original data set. The screenshot is given below.

From the above graph, it is clear that, the straight line of green color representing the linear polynomial equation, does not fit properly with the curve of red color, obtained fron original dataset (given in "DATA' file).

But the curve of blue color is fitting with curve of original data(red color) in a better way than the straight line (green color).

Evaluation of curve fittings:--

1)Better fitting of the cubic curve over the linear curve, can also be explained by examining the 'Goodness of Fitting' (gof) statistics.

Screenshots of the 'gof' statistics of the curve for both linear polynomial(cp1) & cubic polynomial(cp3) as obtained when the above program is run, are given below.

'gof'-cp1 (for curve of linear polynomial)

'gof' -cp3 (for curve of cubic polynomial)

Parameters used to measure fitness charecteristics (according to 'gof' statistics) :--

'sse' (sum of square of error),

'rsquare',

'dfe' (degree of freedom of error),

'adj-r-square',

'rmse'(root mean square error)

From above two 'gof'-statistics we find that 'sse' (sum of square of error) value for cubic polynomial curve(cp3) is less than that of linear polynomial curve (cp1).

Values of both 'rsquare' & 'adj-r-square' of cp3 are greater than those of cp1.(tending to 1).

'rmse' vaue of cp3 is less than that of cp1.

So from the 'gof' statistics we get that, curve of cubic polynomial fits better with the curve obtained from the original data set, than the curve of linear polynomial.

2) Besides this, we can also explain the fitness charecteristics by ploting the curve with error estimation as below:---

Regression analysis with error estimation.

The code for ploting the curves (for linear & cubic polynomial) with 'Error Estimation' is given below.

%Code for curve_fitting for 'Error Estimation'
clc
clear all

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

%curve fit
%cp=a*T+b (Linear polynomial)
[coeff1,s1,mu1]=polyfit(temp,cp,1);

[cp1,delta] =polyval(coeff1,temp,s1,mu1);

%cp=a*T^3+b*T^2+c*T+d (Cubic polynomial)
[coeff3,s3,mu3,]=polyfit(temp,cp,3);
[cp3,delta]=polyval(coeff3,temp,s3,mu3);



%Compare my curve fit with original the original data
plot(temp,cp,'linewidth',1,'color','r');
hold on
plot(temp,cp1+delta,'b --', temp, cp1-delta,'b--')
plot(temp,cp3+delta,'g --', temp, cp3-delta,'g--')
hold off
xlabel('Temperature[K]')
ylabel('specific heat[kJ/kmol-K]')
legend('original data set','','data-linear','','data-cubic','location','northwest')
title('Curve fitting with Error estimation')

In this code, 'polyfit' & 'polyval' functions are used to frame polynomial equation & from that to geterate new set of data on cp at different temperature, using original data (given in 'Data' file).

Here, error estimation structure ('s') is used as an input of 'polyval' function to obtained error estimation. Standard error estimate is returned in delta.

'mu' is used for centering & scaling of data. It gives two element vector mu(1) & mu(2). mu(1) does the centering the data to have mean =0 & mu(2) does scaling the data to have standard deviation 1. All these done for better representing the input data for generating new set of data (on cp here).

The screenshot of the output of the above codeing, which graphically represeting the curve for both linear & cubic polynomials with error estimation, is given below.

Here data of linear & cubic polynomial have been ploted with 95% prediction interval (cp1+-2delta), (cp3+-2delta) along with original data set.

From the graph it is clear that many parts of predicted data zone by linear curve (blue color) do not match with the curve obtained by original data (red color). But predicted data zone given by cubic curve(green color), fit with curve of original data set much better way than the linear curve.

So from the results of 'gof'statistics & Regression analysis with error estimation, it is evident that curve fitting for cubic polynomial is much better than linear polynomial.

Perfect fit & Best fit :--

Perfect cuve fitting is a type of curve fitting where all the points of predicted curve match with curve of original data set with zero error i.e. exact fitting with the original curve. That looks nice, but the perfect fit curve may have many oscillating points & it can fail miserably during extrapolation (future prediction).

Best fit cuve fits with curve of original data set in best possible way & it has practical application with minimum error estimation. It can also perform well in case of future prediction.

In our case we can improve the curve fitting charecteristics by increasing degree of polynomial where error of curve gradually decreasing with a certain extent.

Screen shots of ploting curves of polynomial of higher order have been given below with 'gof' statistics. For example here curves of polynomials of degree 6 & 9 are given.

For Polynomial of degree 6

For Polynomial of degree 9

Here we can see for polynomial of degree 6 & 9, 'gof'-statistics improves within the given data range. But the prediction by those curve outside of the given range may fail. Curve fitting of polynomial of degree 6 & 9 within the given data range are going to match perfectly with the curve of original data set.

Improvement of Cubic curve:--

We can improve the cubic fit by using splitwise curve fitting.

Codes of splitwise curve fitting, are given below.

% Code for Splitwise curve fitting
clc
clear all
%Preparing data
cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);

T1=cp_data(1:300,1);
cp1=cp_data(1:300,2);

T2=cp_data(301:1000,1);
cp2=cp_data(301:1000,2);

T3=cp_data(1001:2200,1);
cp3=cp_data(1001:2200,2);

T4=cp_data(2201:3200,1);
cp4=cp_data(2201:3200,2);

%p_cp=a*T^3+b*T^2+c*T+d
[coeff1,s1,mu1]=polyfit(T1,cp1,3);
[p_cp1,gof]=polyval(coeff1,T1,s1,mu1);

[coeff2,s2,mu2]=polyfit(T2,cp2,3);
p_cp2=polyval(coeff2,T2,s2,mu2);

[coeff3,s3,mu3]=polyfit(T3,cp3,3);
p_cp3=polyval(coeff3,T3,s3,mu3);

[coeff4,s4,mu4]=polyfit(T4,cp4,3);
p_cp4=polyval(coeff4,T4,s4,mu4);

plot(temp,cp,'o','color','r')
hold on
plot(T1,p_cp1,'linewidth',2.5,'color','b')
plot(T2,p_cp2,'linewidth',2.5,'color','b')
plot(T3,p_cp3,'linewidth',2.5,'color','b')
plot(T4,p_cp4,'linewidth',2.5,'color','b')
xlabel('Temperature[K]');
ylabel('specific heat[kJ/kmol-K]');
legend('original data','spliwise curve fit data','location','northwest');
title('Splitwise Curve fitting(degree of polynimial-3')

Here I have divided the original data set of cp & temperature into four parts for each by assesing fitness charecteristics of the previos curve of cubic polynomial with the curve from original data visualy.

On the basis of those four data sets of temperature & cp each, four cubic polynomials are framed & coefficient of those cubic polynomials are calculated. On the basis of this, four predicted datasets of cp are generated which are p_cp1, p_cp2, p_cp3, p_cp4.

Those 4 set of predicted data of cp are ploted with respect to temperature along with original data set.

The screen shot of splitwise curve fittings is given below.

Here we can see from the graph given above, the 'split wise curve' of cubic polynomial (blue color) fits in the best way with the curve of original data set (red color).

Here also we can examine the fitnees of the 'splitwise curve' of cuic polynomial by 'gof' (Goodness of Fitting)statistics.

'gof' statistics of all four splited curves of cubic polynomials along with the MATLAB-code to find those statistics, are given below.

% Code for Splitwise curve fitting (for 'gof' statistics)
clc
clear all

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

T1=cp_data(1:300,1);
cp1=cp_data(1:300,2);

T2=cp_data(301:1000,1);
cp2=cp_data(301:1000,2);

T3=cp_data(1001:2200,1);
cp3=cp_data(1001:2200,2);

T4=cp_data(2201:3200,1);
cp4=cp_data(2201:3200,2);

%p_cp=a*T^3+b*T^2+c*T+d
[p_cp1,gof]=fit(T1,cp1,'poly3','Normalize','on')
[p_cp2,gof]=fit(T2,cp2,'poly3','Normalize','on')
[p_cp3,gof]=fit(T3,cp3,'poly3','Normalize','on')
[p_cp4,gof]=fit(T4,cp4,'poly3','Normalize','on')

'gof' data of p_cp1

'gof'data of p_cp2

'gof'data of p_cp3

'gof data of p_cp4

The 'gof' statistics of p_cp1, p_cp2, p_cp3, p_cp4 (the four parts of the splited curves) shows that values of 'rsquare' & adj-rsquare' for all splited curves are 1 which is the best. So mean values of those two parameters seperately, for through out the curve of cubic plynomial is also 1.

Before Values of those two parameters for the curve of cubic polynomial was found as 0.9967 where 'splitwise curve fitting' method was not applied.

This shows the fitness charecteristics of the curve of cubic polynomial has been improved with this splitwise curve fitting method.

So with out increasing the degree of polynomials, we can get the best fit with splitwise curve fitting.

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.