Project

Performing curve fitting for a Linear and Cubic polynomial using Matlab

Aim:To perform curve fitting for a linear and cubic polynomial using matlab and plot its curves along with the raw data points.

Objective: The objective of this project is to get a perfect curve fit with minimum deviation.

Description:

Curve fitting: Curve fitting is the process of constructing a curve, or mathematical functions,which possess the closest proximity to the real series of data.By curve fitting we can mathematically construct the functional relationship between the observed dataset and parameter values,etc. It is highly effective in the mathematical modelling of some natural processes.

FIG1:Top: raw data and model
Bottom: evolution of the normalised sum of the squares of the errors

Polynomial curves fitting points generated with a sine function.

Red line is a first degree polynomial, green line is second degree,orange line is third degree and blue is fourth degree.

FIG2:curve fitting representation

Program:

(i) Program to fit a linear and cubic polynomial for the cp data

clear all
close all
clc

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

%curve fit
%cp=a*T^2+b*T+c
cp_1= polyfit(temperature,cp,1);
cp_2=polyfit(temperature,cp,3);   
pr_cp1= polyval(cp_1,temperature);
pr_cp2=polyval(cp_2,temperature)

%plot
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,pr_cp1,'linewidth',1,'color','b')
hold on
plot(temperature,pr_cp2,'linewidth',1,'color','r')
xlabel('tempertaure,[k]')
ylabel('specific heat,[kj/kmol-k]')
title('curve fitting')
legend('original dataset','linear line','cubic line')


%To find rsquare
%To find mean
mean=(sum(cp)/length(cp));

ssr_1=sum((pr_cp1-mean).^2);
ssr_2=sum((pr_cp2-mean).^2);
sse_1=sum((cp-pr_cp1).^2);
sse_2=sum((cp-pr_cp2).^2);

sst_1=ssr_1+sse_1;
sst_2=ssr_2+sse_2;

r_square1=(ssr_1/sst_1)
r_square2=(ssr_2/sst_2)

%To find RMSE
RMSE1=(sse_1/length(cp))^0.5
RMSE2=(sse_2/length(cp))^0.5

Output occured for this program is shown below:

(ii)program to get a perfect curve fit:

clear all
close all
clc

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

%curve fit
%cp=a*T^2+b*T+c
cp_1= polyfit(temperature,cp,1);
pr_cpsplit= polyval(cp_1,temperature);

%split1
cp_split1= polyfit(temperature(1:400),cp(1:400),1);   
pr_cpsplit1= polyval(cp_split1,temperature(1:400));
pr_split(1:400)=pr_cpsplit1;

%split2
cp_split2= polyfit(temperature(401:800),cp(401:800),1);   
pr_cpsplit2= polyval(cp_split2,temperature(401:800));
pr_split(401:800)=pr_cpsplit2;

%split3
cp_split3= polyfit(temperature(801:1200),cp(801:1200),1);   
pr_cpsplit3= polyval(cp_split3,temperature(801:1200));
pr_split(801:1200)=pr_cpsplit3;

%split4
cp_split4= polyfit(temperature(1201:1600),cp(1201:1600),1);   
pr_cpsplit4= polyval(cp_split4,temperature(1201:1600));
pr_split(1201:1600)=pr_cpsplit4;

%split5
cp_split5= polyfit(temperature(1601:2000),cp(1601:2000),1);   
pr_cpsplit5= polyval(cp_split5,temperature(1601:2000));
pr_split(1601:2000)=pr_cpsplit5;

%split6
cp_split6= polyfit(temperature(2001:2400),cp(2001:2400),1);   
pr_cpsplit6= polyval(cp_split6,temperature(2001:2400));
pr_split(2001:2400)=pr_cpsplit6;

%split7
cp_split7= polyfit(temperature(2401:2800),cp(2401:2800),1);   
pr_cpsplit7= polyval(cp_split7,temperature(2401:2800));
pr_split(2401:2800)=pr_cpsplit7;

%split8
cp_split8= polyfit(temperature(2801:3200),cp(2801:3200),1);   
pr_cpsplit8= polyval(cp_split8,temperature(2801:3200));
pr_split(2801:3200)=pr_cpsplit8;

cp_2=polyfit(temperature,cp,3);
pr_cp2=polyval(cp_2,temperature)

%plot
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature(1:400),pr_cpsplit1,'linewidth',2)
plot(temperature(401:800),pr_cpsplit2,'linewidth',2)
plot(temperature(801:1200),pr_cpsplit3,'linewidth',2)
plot(temperature(1201:1600),pr_cpsplit4,'linewidth',2)
plot(temperature(1601:2000),pr_cpsplit5,'linewidth',2)
plot(temperature(2001:2400),pr_cpsplit6,'linewidth',2)
plot(temperature(2401:2800),pr_cpsplit7,'linewidth',2)
plot(temperature(2801:3200),pr_cpsplit8,'linewidth',2)
hold on
plot(temperature,pr_cp2,'linewidth',1,'color','r')
xlabel('tempertaure,[k]')
ylabel('specific heat,[kj/kmol-k]')
title('curve fitting')
legend('raw data','split1','split2','split3','split4','split5','split6','split7','split8','cubic line')

%To find rsquare
mean=(sum(cp)/length(cp));
ssr_1=0
sse_1=0
for i=1:length(cp)/8
    ssr_1=sum((pr_cpsplit(i)-mean).^2);
    sse_1=sum((cp(i)-pr_cpsplit(i)).^2);
    sst_1=ssr_1+sse_1;
    r_square1=(ssr_1/sst_1)
    RMSE1=(sse_1/length(cp))^0.5
   
end
ssr_2=0
sse_2=0
for i=1:length(pr_cp2)
    ssr_2=sum((pr_cp2(i)-mean).^2);
    sse_2=sum((cp(i)-pr_cp2(i)).^2);
    
    sst_2=ssr_2+sse_2;
    
    r_square2=(ssr_2/sst_2)

    RMSE2=(sse_2/length(cp))^0.5
end

Output occured during this program is shown below:

(iii)The above program is made short and precise for better understanding with the help of 'for' loop.

close all
clear all
clc

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

a=1;
b=400;

for i=1:8;
    co_eff=polyfit(temp(a:b),cp(a:b),2);
    pre_cp(a:b)=polyval(co_eff,temp(a:b));
    
    a=b+1;
    b=b+400
end
plot(temp,cp);
hold on
plot(temp,pre_cp)
title('curve fitting')
xlabel('tempertaure,[k]')
ylabel('specific heat,[kj/kmol-k]')

Output for this program is shown below:

Errors and its Rectification:

The errors made during the execution if project is shown above,

The errors made are due to the incorrect variables and the missing of '.' symbol during the execution of square values.The errors are rectified with the help of search documentation and help desk.

The perfect curve fit done by getting the r^2 value upto 1 or very close to 1 like r^2=1 or r^2=0.99631.

The best fit can be obtained only by giving the degree of polynomial upto 1 or 2. If it goes to 3 the deviation will be more and we cant get a perfect fit.

Result:

The result we got during the output is an perfect fit that satisfies our requirement,where r^2=0.99 which is almost 1. The degree of polynomial can be either 1 or 2 so that we can get a best fit.

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.