Project

MATLAB code for curve fitting for linear as well as cubic polynomial

AIM

To obtain a code which fit a linear and a cubic polynomial for the cp data from which our aim is to select the best fitting curve and how to make curve fit perfectly. At last we have to conclude what could be done to improve the cubic fit.

DEFINITION OF CURVE FITTING

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

In other words, curve obtain after minimizing the error to improve accuracy of the fitting curve when compared to original data curve.

BODY OF CONTENT

Curve fitting curve is used to find the best fit curve to study the behavior of the function. In order to obtain best fit or perfect fit we have to increase the degree of polynomial because it improves the accuracy. But in this program, we plot a best fit without increasing the order of polynomial I use splitting of data to obtain a fit for the original data set.

MATLAB CODE

Before start of coding we have to confirm that the data values are in same directory where you are going to save the MATLAB code. To confirm this you can check the directory of the data values by writing a command ‘pwd’ in the MATLAB.

A. Plotting of original data

clear all
close all
clc

%preparing the data
cp_data = load('data'); % data is having both temperature as well as cp

%extracting temperature from the data
temperature = cp_data(:,1);
%extracting cp from the data
cp = cp_data(:,2);

%plotting the figure 1
figure(1)
hold on                 % to hold the plot
% plot for temperature and cp value for original data set
plot(temperature,cp,'linewidth',3)
%labelling of the plot by giving xlabel and ylabel to define the axis 
xlabel('temperature [k]')
ylabel('specific heat [KJ/kmol-K]')
%legend is used to define the plot
legend('original dataset')
%tittle helps to give the name of the plot
title('original data')
% to hold off the plot
hold off

OUTPUT

1. For linear equation

clear all
close all
clc
%preparing the data
cp_data=load('data');      % data is having both temperature as well as cp

%extracting temperature from the data
temperature=cp_data(:,1);
%extracting cp from the data
cp=cp_data(:,2);

%to obtain a linear curve fit
%finding the coefficients of linear polynomial cp= at + b
cp_coeff=polyfit(temperature,cp,1);
%calculating the predicted cp by giving coefficient as well as temperature
%data
predicted_cp=polyval(cp_coeff,temperature)

%plotting the figure 5
figure(5)
hold on                  % to hold the plot
% plot for temperature and cp value for original data set
plot(temperature,cp,'linewidth',3)
% plot for temperature and predicted cp value for curve fit
plot(temperature,predicted_cp,'color','r','linewidth',3)
%labelling of the plot by giving xlabel and ylabel to define the axis 
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
%limting the length of x and y axis 
axis([250 3600 900 1400])
%legend is used to define the plot
legend('Original Data','Curve Fit Data','location','northwest')
%tittle helps to give the name of the plot
title('Linear Curve Fit')
% to hold off the plot
hold off

OUTPUT

2. For cubic polynomial.

To study fit of cubic polynomial, it is explain step by step coding along with the curve obtain for each coding

2.a. Plotting of original data and curve fit of cubic polynomial by using simple linear regression

clear all
close all
clc

%preparing the data
cp_data = load('data'); % data is having both temperature as well as cp

%extracting temperature from the data
temperature = cp_data(:,1);
%extracting cp from the data
cp = cp_data(:,2);

%to obtain a curve fit
%finding the coefficients of cubic polynomial cp= at^3 + bt^2 + ct +d
[cp_coeff]=polyfit(temperature,cp,3);
%calculating the predicted cp by giving coefficient as well as temperature
%data
predicted_cp=polyval(cp_coeff,temperature);

%plotting the figure 2
figure(2)
hold on              % to hold the plot

% plot for temperature and cp value for original data set
plot(temperature,cp,'linewidth',3)

% plot for temperature and predicted cp value for curve fit
plot(temperature,predicted_cp,'color','g','linewidth',3)
%labelling of the plot by giving xlabel and ylabel to define the axis 
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
%legend is used to define the plot
legend('Original Data','Curve Fit Data')
%tittle helps to give the name of the plot
title('Curve Fit for cubic polynomial')
% to hold off the plot
hold off

OUTPUT

But here we get this error in command window.

So, it is important to solve this error in order to obtain curve fit.

To solve this error, we will use centering and scaling to minimize this error. In order to do this we have to calculate error estimation structure (s) and centering and scaling values (mu) while calculating the coefficient of cubic polynomial in ‘polyfit’ command.

Where ‘s’ is Error estimation structure. This optional output structure is primarily used as an input to the polyval function to obtain error estimates. S contains the following fields:

Field

Description

R

Triangular factor from a QR decomposition of the Vandermonde matrix of x

df

Degrees of freedom

normr

Norm of the residuals

'mu' contains centering and scaling values and are 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. For evaluation we use, (x - mu(1))/mu(2). Here we have 'temp' as 'x' i.e. query point.

By the help of this, polyfit centers x at zero and scales it to have unit standard deviation,

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

2.b. Plotting of original data and curve fit of cubic polynomial for linear regression with error estimation by using ‘s’ and ‘mu’ factor

Now after using 's' and 'mu' in the code

clear all
close all
clc

%preparing the data
cp_data=load('data');           % data is having both temperature as well as cp

%extracting temperature from the data
temperature=cp_data(:,1);
%extracting cp from the data
cp=cp_data(:,2);

%To obtain a curve fit
%finding the coefficients of cubic polynomial cp= at^3 + bt^2 + ct +d
[cp_coeff,s,mu]=polyfit(temperature,cp,3);
%calculating the predicted cp by giving coefficient as well as temperature
%data and by using 's' and 'mu' factor
predicted_cp=polyval(cp_coeff,temperature,s,mu);

%curve fitting
%plotting the figure 3
figure(3)
hold on                   % to hold the plot
% plot for temperature and cp value for original data set
plot(temperature,cp,'linewidth',3)
%calculating the predicted cp by giving coefficient as well as temperature
%data and by using 's' and 'mu' factor
plot(temperature,predicted_cp,'color','r','linewidth',3)
%labelling of the plot by giving xlabel and ylabel to define the axis 
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
%limting the length of x and y axis 
axis([250 4000 800 1400])
%legend is used to define the plot
legend('Original Data','Curve Fitting for cubic polynomial')
%tittle helps to give the name of the plot
title('Curve Fiting for cubic polynomial')
% to hold off the plot
hold off

OUTPUT

Now there is no error in command window while solving cubic polynomial.

How to achieve perfectly fit curve?

In order to achieve perfectly fit curve, we must consider error estimates also.

Perfect fit means, the curve should fit the original data curve without showing any kind of errors (such as centering and scaling erros).

Note:- Best fit may be perfect fit but perfect fit always best fit. So, in order to obtain perfect fit for cubic polynomial we have to eliminate the possible errors, that means curve fit we got using 'Linear regression with error estimation' is perfect fit for 3rd order polynomial.

But it is still not the best fit. To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method.

How to achieve best fit curve?

To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method

Here I am not going to increase the order of polynomial because our aim is to obtain best fitting curve for cubic polynomial. So we will use split wise method to obtain best fit.

2.c. Plotting of original data and curve fit of cubic polynomial for linear regression with error estimation by the help of split wise method and using ‘s’ and ‘mu’ factor

clear all
close all
clc

%preparing the data
cp_data = load('data');
%extracting temperature from the data
temperature = cp_data(:,1);
%extracting cp from the data
cp = cp_data(:,2);

%splitting the data into four parts
%first part from 1 to 450
cp1=cp([1:450]);
temp1=temperature([1:450]);
%second  part from 451 to 1100
cp2= cp([451:1100]);
temp2=temperature([451:1100]);
%third  part from 1101 to 2400
cp3= cp([1101:2400]);
temp3= temperature([1101:2400]);
%third  part from 2401 to 3200
cp4= cp([2401:3200]);
temp4= temperature([2401:3200]);

%To obtain curve fitting by splitwise method
%plotting the figure 4
figure(4)
hold on                % to hold the plot
% plot for temperature and cp value for original data set
plot(temperature,cp,'color','black','linewidth',3)

%finding the coefficients of cubic polynomial cp= at^3 + bt^2 + ct +d
[co_efficient1,s1,mu1]= polyfit(temp1,cp1,3);
%calculating the predicted cp by giving coefficient as well as temperature
%data and by using 's' and 'mu' factor
predicted_cp1= polyval(co_efficient1,temp1,s1,mu1);
%data and by using 's' and 'mu' factor
plot(temp1,predicted_cp1,'color','r','linewidth',3)

%similarly for rest 3 part
[co_efficient2,s2,mu2]= polyfit(temp2,cp2,3);
predicted_cp2= polyval(co_efficient2,temp2,s2,mu2);
plot(temp2,predicted_cp2,'color','g','linewidth',3)

[co_efficient3,s3,mu3]= polyfit(temp3,cp3,3);
predicted_cp3= polyval(co_efficient3,temp3,s3,mu3);
plot(temp3,predicted_cp3,'color','b','linewidth',3)

[co_efficient4,s4,mu4]= polyfit(temp4,cp4,3);
predicted_cp4= polyval(co_efficient4,temp4,s4,mu4);
plot(temp4,predicted_cp4,'color','r','linewidth',3)

%labelling of the plot by giving xlabel and ylabel to define the axis
xlabel('temperature [k]')
ylabel('specific heat [KJ/kmol-K]')
%legend is used to define the plot
legend('original dataset','splitting data','splitting data','splitting data','splitting data','location','northwest')
%tittle helps to give the name of the plot
title('curve obtain by splitting the data set')
% to hold off the plot
hold off

OUTPUT

ERROR OBTAIN WHILE PROGRAMMMING

  1. Giving warning polynomial is badly conditioned to overcome this we have to calculate the error also by using ‘s’ and ‘mu’ factor in command of ‘polyfit’ as discussed above.

2. By typing mistake, I have written nortwest instead northeast in legend command line 58

RESULTS

To achieve perfectly fit curve

In order to achieve perfectly fit curve, we must consider error estimates also.

Perfect fit means, the curve should fit the original data curve without showing any kind of errors (such as centering and scaling errors).

Note:- Best fit may be perfect fit but perfect fit always best fit. So, in order to obtain perfect fit for cubic polynomial we have to eliminate the possible errors, that means curve fit we got using 'Linear regression with error estimation' is perfect fit for 3rd order polynomial.

But it is still not the best fit. To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method.

To achieve best fit curve

To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method

Here I am not going to increase the order of polynomial because our aim is to obtain best fitting curve for cubic polynomial. So, we will use split wise method to obtain best fit.

REFRENCES

1. Skill lync lectures of MATLAB course

2. Mathworks website

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.