Project

CURVE FITTING TECHNIQUE USING OCTAVE

AIM:

To fit a curve for the given Cp vs Temperature data by a linear and cubic polynomial and to verify the goodness of the curve fit.

INTRODUCTION:

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. It can involve either interpolation where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data (or captures the trend of the data). In other words it 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.

“Goodness of Fit” of a linear regression model indicates how well a model fits a given set of data, or how well it will predict a future set of observations. Goodness of fit can be determined by

  • Examining residuals from the model
  • A global measure of “variance explained”, `R^2`
  • Outlier detection
  • Model validation via an outside data set or by splitting a data set.

Assume data points (x1,y1) , (x2,y2) , (x3,y3) , ... (xn,yn) and assume the best fit for these set of points will be y = f(x), the criterion under which the equation of the curve will be the best fit possible is as follows,

Suppose each of these n points has an individual error (difference in y co-ordinate values)

The error for a general "i" point can be given by the function,

Error=|y(i)−value of f(x) at x(i)|=|y(i)−f(x(i))|

  1. In order to make a curve fit perfectly, the error estimate needs to be taken into consideration. Perfect fit means the interpolated function and the original function should overlap, i.e. there should be no error. One way to improve the accuracy of curve fit is to increase the order of the polynomial (as this would reduce the error associated).

The best fit line is a good curve fit but still has some error associated with it. This can be calculated by 4 methods:

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

For a perfect fit, `R^2=1`. This need not be the case for the best fit line.Therefore a perfect fit is always a best fit curve while the converse need not be true.

  1. In order to improve a cubic fit, the error associated with the fit needs to be estimated. The regression fit `R^2=1` can be estimated. If `R^2` value is not equal to one or if the error associated is not equal to zero, the degree of polynomial fit can be increased or a splitwise can be used.

PROCESS:

The code is split into 2 parts.

PART 1:

Here the polyfit() function is used to obtain the coefficients for the first degree polynomial curve fit. Then the polyval() function is used to obtain the value of the function. Then both the original curve and estimated curves are plotted together.

A similar procedure is carried out for the cubic fit.

CODE for PART 1:

%Curve fit
clc
clear all
close all

%Fetching data from data file 
Cp_data=load('data');

%Providing temp and Cp values 
Temperature=Cp_data(:,1);
Cp=Cp_data(:,2);

%Plotting the existing data
figure(1)
plot(Temperature,Cp,'linewidth',2,'color', 'r');
xlabel('Temperature in K');
ylabel('Specific heat in [kJ/kmol-K]');
hold on

%Coefficients of linear curve fit
coeff_1=polyfit(Temperature,Cp,1);

%Plottting the curve obtained
Pred_Cp_first=polyval(coeff_1,Temperature);
plot(Temperature,Pred_Cp_first,'linewidth',2,'color', 'black')
legend('Actual data','Estimated fit')
hold on
grid on

%Coefficients of cubic curve fit
coeff_2=polyfit(Temperature,Cp,3);

%Plottting the curve obtained;
Pred_Cp_sec=polyval(coeff_2,Temperature);
figure(2)
plot(Temperature,Pred_Cp_sec,'linewidth',2,'color', 'green')
hold on
plot(Temperature,Cp,'linewidth',2,'color', 'r')
legend('Actual data','Estimated fit')
grid on

RESULTS :

  1. For the first order fit,

2. For the cubic fit,

PART 2:

Here, the best fit curve is obtained by `R^2` method by targeting an `R^2` value of 0.99999.

This is done by using a while loop, calculating the `R^2` for each degree of polynomial, and incrementing the degree if R^2 is less than 0.9999 or R^2 is greater than 1.00001.

%Best curve fit to reduce error using R^2 method
sum=0;
%Finding Mean Cp value
for j=1:length(Temperature)
  sum=sum+Cp(j);
end

Mean=sum/length(Temperature);

Rsq=0;
k=1;

while(Rsq<=0.99999 || Rsq>=1.00001)

SSR=0;
SSE=0;

coeff=polyfit(Temperature,Cp,k);
Cpe=polyval(coeff,Temperature);

 for i=1:length(Temperature)
  error=Cpe(i)-Cp(i);
  SSE=SSE+error^2;
  temp=Cp(i)-Mean;
  SSR=SSR+temp^2;
 end

SST=SSE+SSR;
Rsq=SSR/SST;


k++
count=k-1  
fprintf("Rsquare value is %d", Rsq)
fprintf("\n")
end

coeff_final=polyfit(Temperature,Cp,count)
CpF=polyval(coeff,Temperature);
figure(3)
plot(Temperature,CpF,'linewidth',3,'color', 'green')
hold on
plot(Temperature,Cp,'linewidth',2,'color', 'r')
legend('Actual data','Estimated fit')
grid on

RESULT:

The `R^2` value obtained is 1 and for a degree of 9.

REFERENCES

  • https://en.wikipedia.org/wiki/Curve_fitting
  • http://www.medicine.mcgill.ca/epidemiology/joseph/courses/EPIB-621/fit.pdf
  • https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
  • http://web.iitd.ac.in/~pmvs/courses/mel705/curvefitting.pdf
  • http://www.synergy.com/Tools/curvefitting.pdf
  • https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html
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.