CURVE FITTING USING MATLAB
CURVE FITTING USING MATLAB
AIM :
Writing a matlab code to fit linear and cubical data for cp data and plotting linear and cubical fit curves along with raw data fit points .
INTRODUCTION :
Basically the curve fitting is the process of constructing a curve or a mathematical function that has the best fit to a series of data ponts , posibly subject to constraints .Curve fitting is one of the popular methods of analysing data . Curve fitting can involve either interpolation , wherean exact fit to the data is required , or smoothing in which a smooth function is constructed that approximately fits the data . A related topic regarding this concern is regression analysis.
Fitting a god curve through (or to) a given set of points is a very important numerical method . It is often better to fit a smooth function passing through most of the data pointys that may be fit better than an interpolating polynomial . A smooth fit may be eliminate some of the erros of the experimental data .Let the fitting function be called F(x). The fitting function could be a polynomial such as
πΉ(π₯) =π0 + π1π₯ + π2π₯^2 + β― + ππ π₯π
In place of π₯, π₯2, π₯3 .. we could have other functions π1(π₯), π2(π₯), π3(π₯), etc
CODE TO PLOT LINEAR FIT
% code to plot linear fit
clear all
close all
clc
% setting up the points
cp_data = load('data');
temperature = cp_data(:,1);
specific_heat = cp_data(:,2);
% curve fit
% cp = a*T + b
coeffs = polyfit(temperature , specific_heat , 1);
predicted_cp = polyval(coeffs, temperature);
% comparing both the curves
hold on
plot (temperature , specific_heat, 'linewidth',3,'color','b')
plot (temperature , predicted_cp, 'linewidth',3,'color','r')
xlabel('Temperature (K)')
ylabel('specific Heat (KJ/Kmol-k) ')
legend('Original Dataset','curvefit')
OUTPUT:
CODE TO PLOT QUADRATIC FIT
%code to plot quadratic fit
clear all
close all
clc
% setting up the points
cp_data = load('data');
temperature = cp_data(:,1);
specific_heat = cp_data(:,2);
% curve fit
% cp = a*T^2 + b*T + c
coeffs = polyfit(temperature , specific_heat , 2);
predicted_cp = polyval(coeffs, temperature);
% comparing both the curves
hold on
plot (temperature , specific_heat, 'linewidth',3,'color','b')
plot (temperature , predicted_cp, 'linewidth',3,'color','r')
xlabel('Temperature (K)')
ylabel('specific Heat (KJ/Kmol-k) ')
legend('Original Dataset','curvefit')
OUTPUT:
CODE TO PLOT CUBIC FIT
%code to plot cubic fit
clear all
close all
clc
% setting up the points
cp_data = load('data');
temperature = cp_data(:,1);
specific_heat = cp_data(:,2);
% curve fit
% cp = a*T^3+ b*T^2 + c*T +d
coeffs = polyfit(temperature , specific_heat , 3);
predicted_cp = polyval(coeffs, temperature);
% comparing both the curves
hold on
plot (temperature , specific_heat, 'linewidth',3,'color','b')
plot (temperature , predicted_cp, 'linewidth',3,'color','r')
xlabel('Temperature (K)')
ylabel('specific Heat (KJ/Kmol-k) ')
legend('Original Dataset','curvefit')
OUTPUT
OBSERVATIONS :
- By increasing the data oin curve fitting the error between original data fit and curve fit gets redused.
- For higher order polynomials and especially for unevenly spaced points the determinant tends to be → 0
and create problems. - Interpolation is the best option to make the curve fit better as the errors are getting minimized so as to tends to zero as well
- More over the best curve fit obtained by splitting the data into multiple number of polynomials for differnet set of values .
- If we apply much coefficients for curve fitting then the curve fit becomes more converged
- we can try different algorithms and increase the number of iterations function evaluations allowed inorder to improve curve fitting
- The cubic fit can be improved by splitting the curve into multiple number of polynomials for differnet sets of temperature values
- It can be impreoved by reducing the error between original data set and curve fit data
-
To make a curve fit perfect the error estimates should be considered. The perfect fit is a fit where it should match the original curve and shouldn't have any scaling and centering errors in that particular degree of the polynomial. The R^2 value must be equal to 1 and SSE=0.
A perfect fit can all the time be the best fit but the best fit may not be always a perfect fit. There are basically two methods to obtain the best fit.
- To increase the degree of the polynomial.
- To use the split-wise method i.e to split the data into folds and do the curve fitting for it.
What could we be done to improve the cubic fit? its to To make the cubic fit better, we need to split the data into smaller datasets and fit them separately