Project

Curve Fitting for best fit to data using linear, cubic polynomial and stepwise fitting

Aim:

To best fit a curve to data provided using linear, cubic polynomial and stepwise fitting using Matlab

Theory:

Curve fitting is one of the most powerful and most widely used analysis tools in Origin. Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.

Problem defenition and Results:

We will be plotting linear and cubic polynomial fit.

Initially the data is loaded using MATLAB function load('filename'), before that check current directory using pwd command.

Below program is used for plotting the original data.

clear all
close all
clc

cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);
plot(temp,cp,'color','g','linewidth',3)
hold on
grid on
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')

Original data

Error Estimation:

If the R-square value is greater than 0.95 the fit is assumed to be good.

LINEAR FIT

Now for linear fit we have line equation as y=ax+b, which has two coefficients. This can be can be calculated using the Matlab function polyfit(x-value,y-value,order). Below program describes the linear fit.

clear all
close all
clc

cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);
plot(temp,cp,'color','g','linewidth',3)
hold on
grid on
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
title('Cubic fit')

[coeff,s,mu] = polyfit(temp,cp,1);
pred_cp = polyval(coeff,temp,s,mu);
plot(temp,pred_cp,'color','r','linewidth',3)
sum_cp=0;
for i=(1:length(cp))
sum_cp=sum_cp+cp(i);
end
mean=sum_cp/length(cp);

SSR=0;
SSE=0;
for j=(1:length(cp))
SSR=SSR+(pred_cp(j)-mean)^2;
SSE=SSE+(pred_cp(j)-cp(j))^2;
end
SST=SSR+SSE;
R2=SSR/SST;
X=['R square vale = ',R2];
disp(X)

Result for linear fit

The R-square value for linear fit is 0.9249.

CUBIC FIT

Now we will try Cubic polynomial fit using equation y=ax^3+bx^2+cx+d. The four coefficients are calculated using polyfit() command and the respective value is calculated using polyval().

clear all
close all
clc

cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);
plot(temp,cp,'color','g','linewidth',3)
hold on
grid on
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
title('Cubic fit')

[coeff,s,mu] = polyfit(temp,cp,3);
pred_cp = polyval(coeff,temp,s,mu);
plot(temp,pred_cp,'color','r','linewidth',3)
sum_cp=0;
for i=(1:length(cp))
sum_cp=sum_cp+cp(i);
end
mean=sum_cp/length(cp);

SSR=0;
SSE=0;
for j=(1:length(cp))
SSR=SSR+(pred_cp(j)-mean)^2;
SSE=SSE+(pred_cp(j)-cp(j))^2;
end
SST=SSR+SSE;
R2=SSR/SST;
X=['R square vale = ',R2];
disp(X)

Result for cubic fit

The R-square value for cubic fit is 0.9967.

While compiling the above code the following error was seen.

It was rectified by using centering and scaling. In polyfit() command while computing coefficient we will also calculate the error estimation structure and cantering and scaling values.

clear all
close all
clc

cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);
plot(temp,cp,'color','g','linewidth',3)
hold on
grid on
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
title('Cubic fit')

[coeff,s,mu] = polyfit(temp,cp,3);
pred_cp = polyval(coeff,temp,s,mu);
plot(temp,pred_cp,'color','r','linewidth',3)

How to get perfect fit

Inorder to get a perfect fit error estimation should also be done. Centering and scaling should be done inorder for the curve to fit perfectly with the original data. Moreover a higher degree polynomial curve should be used to get a perfect fit or a stepwise method should be used.

How to get best fit

we can use Root mean square or simpple linear regression. We can change the order of polynomial.

The best way is to divide the data into multiple steps and then fit a curve for each of those steps as explained below.

Stepwise Fitting

Below program shows stepwise fit where the data is split 10 times inorder to get a best fit.

clear all
close all
clc

cp_data=load('data');
temp=cp_data(:,1);
cp=cp_data(:,2);
plot(temp,cp,'color','g','linewidth',4)
hold on
grid on

%using stepwise for better fit
sum_cp=0;
SSR=0;
SSE=0;
poly_order=3;
step_size=10;
steps=round(length(temp)/step_size);

for k=(1:length(cp))
sum_cp=sum_cp+cp(k);
end
mean=sum_cp/length(cp);

temp1=temp(1:steps);
cp1=cp(1:steps);
[coeff,s,mu] = polyfit(temp1,cp1,poly_order);
pred_cp = polyval(coeff,temp1,s,mu);
for j=(1:length(cp1))
SSR=SSR+(pred_cp(j)-mean)^2;
SSE=SSE+(pred_cp(j)-cp1(j))^2;
end
plot(temp1,pred_cp,'color','r','linewidth',2)
hold on


for i=(2:(step_size-1))
temp1=temp(steps*(i-1):steps*i);
cp1=cp(steps*(i-1):steps*i);
[coeff,s,mu] = polyfit(temp1,cp1,poly_order);
pred_cp = polyval(coeff,temp1,s,mu);
for j=(1:length(cp1))
SSR=SSR+(pred_cp(j)-mean)^2;
SSE=SSE+(pred_cp(j)-cp1(j))^2;
end
plot(temp1,pred_cp,'color','r','linewidth',2)
hold on
end

temp1=temp(steps*(step_size-1):length(temp));
cp1=cp(steps*(step_size-1):length(temp));
[coeff,s,mu] = polyfit(temp1,cp1,poly_order);
pred_cp = polyval(coeff,temp1,s,mu);
for j=(1:length(cp1))
SSR=SSR+(pred_cp(j)-mean)^2;
SSE=SSE+(pred_cp(j)-cp1(j))^2;
end
plot(temp1,pred_cp,'color','r','linewidth',2)
hold on
legend('Original data','Stepwise fit data','location','northwest')
title('Stepwise Fit')


SST=SSR+SSE;
R2=SSR/SST;
X=['R square vale = ',R2];
disp(X)

As expected stepwise fit has provided a very smooth fit to the original data with very less error between the two datas.

The R-square value for stepwisefit is 1.000.

Conclusion:

We have understood the use of curve fitting for data analysis and future predictions.

Using a higher degree polynomial or stepwise fitting gives a best fit which has very less error compared to original data.

The R-square for

linear fit = 0.9249

cubic fit = 0.9967

stepwise fit for 10 steps = 1.000

The above data shows stepwise is giving a perfect 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.