Project

Curve fitting

Aim : To write a code to perform curve fitting.

Theory : curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation , where an exact fit to the data is required, or smoothning , in which a "smooth" function is constructed that approximately fits the data.

perfect fit :

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.

best fit :

best fit is achived when the geometric distance from the given point to the fitting curve are minimiazed, in the least square sense . Finding the best fit reduces to the minimization of the objective functions which denotes the curve fitting.

how to get best fit ?

upon finding a curve which satisfies the data points there are 4 quantities which help us mesaure the godness of fit criteria or how well the equation is representing the datapoints.these are,

  • sum of squares due to error (SSE)
  • R-square
  • adjusted R-square
  • Root mean squared error (RMSE)

Minimizing error : There are two ways through which we can minimize our error

1. By increasing the order of polynomial

2. By dividing the temperature values into smaller parts using for loop, this method was used mainly to improve the cubic fit.

References :

Matlab code : liner (a*t+b=0)

clear all
close all
clc

%loading the data of specific heat and temperature
%cp is specific heat

cp_data = load('data'); %load cmd is used to load the file in the directory
temp = cp_data(:,1); %temp is the first column of cp_data
cp = cp_data(:,2); %cp is thee second column of cp_data

%curve fit for linear polynomial

%cp= a*t+b linear

  b = 0;
  n=10
  
%for loop to improve the fit 
for i=1:n
    a = b+1;
   b = b+3200/n; 
        %using polyfit to obtain the coefficient
        co_eff = polyfit(temp(a:b),cp(a:b),1);
        %we need predicted specifit heat for curve fitting 
        predicted_cp(a:b)=polyval(co_eff,temp(a:b));

        
end


predicted_cp = predicted_cp'%to change the array type from 1x3200 to 3200x1

%plotting the graphs

hold on
figure(1) 
plot(temp,predicted_cp,'linewidth',3,'color','b');
hold on
plot(temp,cp,'linewidth',3,'color','r')
xlabel('temperature [k]')
ylabel('specific heat[kj/kmol-k]')
legend('curvefit1','orginal data set','location','northwest')
title('liner polynomial')
grid on

%error calculation 

ssr =sum(predicted_cp - mean(predicted_cp)).^2

sse = sum(cp-predicted_cp).^2

sst = ssr+sse

r2 = (ssr/sst)

rmse = sqrt(sum((cp-predicted_cp).^2)/n)

R2 valve : 0.9249


RMSE value : 26.1

Figure(1)

as you can see in the above figure there is a deviation of curvefit1 to original data set.

Matlab code : cubic (a*t^3+b*t^2+c^t+d=0)

clear all
close all
clc

%loading the data of specific heat and temperature
%cp is specific heat

cp_data = load('data'); %load cmd is used to load the file in the directory
temp = cp_data(:,1); %temp is the first column of cp_data
cp = cp_data(:,2); %cp is thee second column of cp_data

%curve fit for cubic polynomial

%cp= cubic(a*t^3+b*t^2+c^t+d=0)

  b = 0;
  n=10
  
%for loop to improve the cubic fit

for i=1:n
    a = b+1;
    b = b+3200/n; 
        %using polyfit to obtain the coefficient
        % 3 denotes the degree of polynomial
        
        co_eff = polyfit(temp(a:b),cp(a:b),3);
        
        %we need predicted specifit heat for curve fitting 
        predicted_cp(a:b)=polyval(co_eff,temp(a:b));

        
end

%to change the array type from 1x3200 to 3200x1

predicted_cp = predicted_cp'

%plotting the graph


figure(2) 
plot(temp,predicted_cp,'linewidth',3,'color','b');
hold on
plot(temp,cp,'linewidth',3,'color','r')
xlabel('temperature [k]')
ylabel('specific heat[kj/kmol-k]')
legend('curvefit2','orginal data set','location','northwest')
title('cubic polynomial')
grid on

%error calculation 

ssr =sum(predicted_cp - mean(predicted_cp)).^2

sse = sum(cp-predicted_cp).^2

sst = ssr+sse

r2 = (ssr/sst)

rmse = sqrt(sum((cp-predicted_cp).^2)/n)

R2 vaule =0.9967

RMSE = 5.43

figure (2)

as you can see in the above figure there is very less deviation of curvefit2 to original data set

figure (3) polynomial of degree 8

perfect fit :

R2 value = 1

RMSE = 0.5107

as you can see in the above figure there is no deviation and R2 value is 1 , so its a perfect fit.

Conclusion :


the accuracy of the curve fit can be increased by increasing the degree of polynomial and splittign the data set into several equal parts.

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.