Project

CURVE FITTING

CURVE FITTING

INTRODUCTION

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 modeling of some natural processes.

CODE FOR CURVE FITTING

clear all
close all
clc

cpdata=load ('data');
temperature=cpdata(:,1);
cp=cpdata(:,2);

%Linear fit
coeff1=polyfit(temperature,cp,1);
newcp1=polyval(coeff1,temperature);

%Cubic fit
coeff3=polyfit(temperature,cp,3);
newcp3=polyval(coeff3,temperature);

%Linear fit curve plot
figure(1)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,newcp1,'linewidth',3)
legend('Data values','Linear fit')
xlabel('Teperature [K]')
ylabel('Cp [kJ/kmol-K]')
title('Temperature vs Cp')

%Cubic fit curve plot
figure(2)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,newcp3,'linewidth',3,'color','green')
legend('Data values','Cubic fit')
xlabel('Teperature [K]')
ylabel('Cp [kJ/kmol-K]')
title('Temperature vs Cp')

%Comparison between linear fit and cubic fit curves
figure(3)
hold on
plot(temperature,cp,'linewidth',3)
plot(temperature,newcp1,'linewidth',3)
plot(temperature,newcp3,'linewidth',3,'color','g')
legend('Data values','Linear fit','Cubic fit')
xlabel('Teperature [K]')
ylabel('Cp [kJ/kmol-K]')
title('Temperature vs Cp')

LINEAR FIT

CUBIC FIT

COMPARISON BETWEEN LINEAR AND CUBIC FIT

Procedure involved in coding

  1. Data file should be on the same registry as the matlab file registry.
  2. Load command is used to load these values.
  3. X values are stored in temperature variable, Y values are stored in cp variable.
  4. For linear fit, we need to find the co efficients. To find those, we use polyfit command and are stored in coeff1 variable.
  5. Using these co efficients, we find new cp values for linear fit using polyval command and are stored in newcp1 variable.
  6. The same procedure is followed for cubic fit.
  7. Plot is made between temperature and cp for all three cases using plot command.
  8. Appropriate title, legend, and axes label are given for graphs.

Parameters used to measure the fitness of the curve

1)The sum of squares due to error (SSE)

2)R-square

3)Adjusted R-square

4)Root mean squared error (RMSE)

To make the curve fit perfectly,

  • Increase the degree of polynomial.
  • Use split-wise polynomial function for different temperatures.

To improve cubic fit,

  • We can use the command [p,S,mu] for polyfit. This function returns the mean and standard deviation of x and uses these values to plot a curve for higher order of polynomial.
  • Use split-wise polynomial function
  • Add points with distinct X values

SPLIT WISE POLYNOMIAL FUNCTION CODE:

%Using split-wise polynomial functions

clear all
close all
clc

%input
cp_data=load('data');
temperature1=cp_data(1:1600,1);
temperature2=cp_data(1600:3200,1);
cp1=cp_data(1:1600,2);
cp2=cp_data(1600:3200,2);



%linear fit calcualtions
coeff11=polyfit(temperature1,cp1,1);
coeff12=polyfit(temperature2,cp2,1);
new_cp11=polyval(coeff11,temperature1);
new_cp12=polyval(coeff12,temperature2);

%cubic fit calculations
coeff31=polyfit(temperature1,cp1,3);
coeff32=polyfit(temperature2,cp2,3);
new_cp31=polyval(coeff31,temperature1);
new_cp32=polyval(coeff32,temperature2);


hold on
plot(temperature1,cp1,'linewidth',3,'color','b')
plot(temperature2,cp2,'linewidth',3,'color','b')
plot(temperature1,new_cp11,'linewidth',3,'color','r')
plot(temperature2,new_cp12,'linewidth',3,'color','r')
plot(temperature1,new_cp31,'linewidth',3,'color','g')
plot(temperature2,new_cp32,'linewidth',3,'color','g')

xlabel('Temperature [K]')
ylabel('Cp [kJ/kmol-K]')
title('Temperature vs Cp')

legend('data values','data values','linear curve fit','linear curve fit','cubic curve fit','cubic curve fit')

SPLIT-WISE POLYNOMIAL FUNCTION GRAPH:

It is thus clear from the above graph that the fit for cubic polynomial becomes more closer to data value fit as we use split-wise polynomial funtion.

Inorder to get the best fit, we follow the following procedure

  1. Load some data at the MATLAB® command line.

    load ('data')
  2. Open the Curve Fitting app. Enter:

    cftool 
  3. In the Curve Fitting app, select X Data and Y Data.

    Curve Fitting app creates a default interpolation fit to the data.

  4. Choose a different model type using the fit category drop-down list, e.g., select Polynomial.

  5. Try different fit options for your chosen model type.

  6. Select File > Generate Code.

clear all
close all
clc

cpdata=load ('data');
temperature=cpdata(:,1);
cp=cpdata(:,2);

cftool

function [fitresult, gof] = createFit(temperature, cp)
%CREATEFIT(TEMPERATURE,CP)
%  Create a fit.
%  
%  Data for 'untitled fit 1' fit:
%      X Input : temperature
%      Y Output: cp
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.
 
%  Auto-generated by MATLAB on 13-May-2019 17:18:49
 

%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( temperature, cp );

% Set up fittype and options.
ft = 'linearinterp';

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, 'Normalize', 'on' );

% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'cp vs. temperature', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel temperature
ylabel cp
grid on
end

Smoothing spline:
f(x) = piecewise polynomial computed from p
Smoothing parameter:
p = 0.9

Goodness of fit:
SSE: 2.939e-05
R-square: 1
Adjusted R-square: 1
RMSE: 0.000151

Linear interpolant:
f(x) = piecewise polynomial computed from p
where x is normalized by mean 1900 and std 923.9
Coefficients:
p = coefficient structure

Goodness of fit:
SSE: 0
R-square: 1
Adjusted R-square: NaN
RMSE: NaN

MISTAKES MADE, ERRORS SHOWN, AND CORRECTIONS CARRIED OUT

No mistakes were made.

Result:

It has been found that Interpolant and Smoothing spline are the best fits.

Reference:

https://in.mathworks.com/help/curvefit/curve-fitting.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.