Project

Curve fitting- Find best and perfect fit

In this project we are learning about how the curve fitting thing works on MATLAB,

Definition of Curve Fitting:

Curve Fitting is the process of plotting/ constructing a curve or the mathematical function which has the best fit to the series of original data points.

In other words, it's a plot having two curves (one is original data curve and the other is predicted/ calculated curve) which shows the accuracy between original and calculated values.

Problem definition:

In this project, we have to plot linear and cubic curbve fit.

First of all, Data is given. In MATLAB we can load the by using load('filename') command, but first check whether current directory is same or not with the data directory using pwd command.

clc, close all, clear all
%loading given data
cp_data=load('data');

%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);


%plotting given data
plot(temp,cp,'linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
axis([250 3600 900 1400]);

Original Data Set Graph -

Now, we will find coefficients for linear curve:

Number of coefficients we can find are= Asked Degree of polynomial + 1 Maximum Degree of polynomial we can find= No. of data figures - 1

Note : Suppose, if we have 10 given values, then we can find maximum 9 coefficients, that means we can go upto 9th degree of polynomial. And if we try to plot curve for the same degree of polynomial as the number of given data points are, then we get below warning as: Warning: Polynomial is not unique; degree >= number of data points.

But in this case, we got 3200 values, but we won't go that far.

Now for linear curve, there will be 1 +1 =2 coefficients. So in linear curve, the graph will be penetrated on two points as we have two coefficients.

clc, close all, clear all
%loading given data
cp_data=load('data');

%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);



%find the coefficients, 
cp_coeff=polyfit(temp,cp,1);

predicted_cp=polyval(cp_coeff,temp)

%curve fit
plot(temp,cp,'linewidth',2.5)
hold on 
plot(temp,predicted_cp,'color','r','linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
axis([250 3600 900 1400])
legend('Original Data','Curve Fit Data','location','northwest')
title('Linear Curve Fit (1st Order)')

Linear Curve Fit:

Now for cubic curve, there will be 3 +1 =4 coefficients So in cubic curve, the graph will be penetrated four points as we have four coefficients.

Simple linear regression :

clc, close all, clear all
%loading given data
cp_data=load('data');

%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);



%find the coefficients, 
[cp_coeff]=polyfit(temp,cp,3);

predicted_cp=polyval(cp_coeff,temp);

%curve fit
plot(temp,cp,'linewidth',2.5)
hold on 
plot(temp,predicted_cp,'color','r','linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Cubic Curve Fit (3rd Order)')
axis([250 3600 900 1400])
legend('Original Data','Curve Fit Data','location','northwest')

Cubic Curve Fit:

But here we will get this error. Check the error in command window:

So, to eliminate this error, we have to use centering and scaling.

Now in 'polyfit' command while calculating the coefficients we will also calculate error estimation structure (s) and centering and scaling values (mu).

's' is a structure and having three fields - R (Triangular Factor) df (Degree of Freedom) and normr (Norm of the residuals)

p=(Rinv*Rinv')*normr^2/df

here, 'p' is 'cp_coeff', i.e. Least-squares fit polynomial coefficient

'mu' contains centering and scaling values and is a two element vector. mu(1) is mean(x), and mu(2) is std(x).

These values center the query points in x at zero with unit standard deviation.Use mu as the fourth input to polyval to evaluate p at the scaled points, (x - mu(1))/mu(2). Here we have 'temp' as 'x' i.e. query point.

Using these values, polyfit centers x at zero and scales it to have unit standard deviation,

xˆ=(x−x‾)/σx.

Now after adding 's'and 'mu' in the code, check the code:

Linear Regression with Error estimates:

clc, close all, clear all
%loading given data
cp_data=load('data');

%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);



%find the coefficients, 
[cp_coeff,s,mu]=polyfit(temp,cp,3);

predicted_cp=polyval(cp_coeff,temp,s,mu);

%curve fit
plot(temp,cp,'linewidth',2.5)
hold on 
plot(temp,predicted_cp,'color','r','linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Cubic Curve Fit (3rd Order)')
axis([250 3600 900 1400])
legend('Original Data','Curve Fit Data','location','northwest')

Now, there is no error in command window.

This is how we can eliminate centering and scaling errors.

Parameters we have used in coding are:

  1. Query points - temp (x)
  2. Fitted values at query points - cp (y)
  3. Degree of polynomial fit - n
  4. Least-squares fit polynomial coefficients - cp_coeff (p)
  5. Error estimate structure - s (S)
  6. Centering and Scaling values - mu

Now we will find the best fit for the original data set. To avoid errors we will use 's' and 'mu'.

4th degree polynomial fit:

5th degree polynomial:

6th degree polynomial:

7th degree polynomial:

8th degree polynomail:

9th Degree Polynomial :

How to make perfect fit:

In order to make perfect fit, we must consider error estimates as well. Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling erros) in that perticular degree of polynomial. Perfect fit can always be a best fit but best fit can not be a perfect fit. So, in the 3rd order polynomial I have eliminated the errors, that means curve fit we got using 'Linear regression witherror estimates' is perfect fit for 3rd order polynomial. But it is still not the best fit. To calculate best fit, we must increase the order or use splitwise method.

How to get the best fit:

For this, we can use RMS(Root Mean square) or simple linear regression. We can change the order of polynomial to get the perfect fit. ANd the best way is, split the data in two or more data folds and do the curve fit. I have shown how to make a best fit using 3rd order polynomial in the last part of the project.

If we use, changing od polynomial order method to get the curve fit, then 9th order is the best fit.

But, in split-wise method that I have shwon at the bottom, even the 3rd order can give the best fit.

What could be done to improve the cubic fit?

Please check the following method.

Splitwise Curve Fit:

In this method we split the data in various data folds. I have splitted the data in four folds.

Check code:

clc,close all,clear all
cp_data=load('data');

%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);
temp1=cp_data(1:460,1);
cp1=cp_data(1:460,2);

temp2=cp_data(461:1100,1);
cp2=cp_data(461:1100,2);

temp3=cp_data(1101:2200,1);
cp3=cp_data(1101:2200,2);

temp4=cp_data(2201:3200,1);
cp4=cp_data(2201:3200,2);
%find the coefficients, 
[cp_coeff1,s1,mu1]=polyfit(temp1,cp1,3);
predicted_cp1=polyval(cp_coeff1,temp1,s1,mu1);
[cp_coeff2,s2,mu2]=polyfit(temp2,cp2,3);
predicted_cp2=polyval(cp_coeff2,temp2,s2,mu2);
[cp_coeff3,s3,mu3]=polyfit(temp3,cp3,3);
predicted_cp3=polyval(cp_coeff3,temp3,s3,mu3);
[cp_coeff4,s4,mu4]=polyfit(temp4,cp4,3);
predicted_cp4=polyval(cp_coeff4,temp4,s4,mu4);

%curve fit
plot(temp,cp,'linewidth',2.5)
hold on 
plot(temp1,predicted_cp1,'color','r','linewidth',2.5)
plot(temp2,predicted_cp2,'color','r','linewidth',2.5)
plot(temp3,predicted_cp3,'color','r','linewidth',2.5)
plot(temp4,predicted_cp4,'color','r','linewidth',2.5)
hold off
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Splitwise Curve Fit Data-Cubic Curve Fit (3rd order)')
axis([250 3600 900 1400])
legend('Original Data','Splitwise Curve Fit Data','location','northwest')

Splitwise Curve Fit Data-Cubic Curve Fit (3rd order):

No errors in command window. We dont have to go to the higher order if we use this method.

References:

  1. https://in.mathworks.com/matlabcentral/answers/157183-polyfit-with-centering-and-scaling-values
  2. polyfit command in matlab s/w.
1 comment
  1. Sai Karthik Ponnapallican you please tell me for where did you got the definition of prefect and best fit, because i am unable to find the definition
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.