All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim : To perform curve fitting for given data set and to find best and perfect fit for that particular data set using MATLAB programming Objective : Writing a code to fit a Linear, Quadratic and Cubic polynomial for the Cp data. Plotting the Linear, Quadratic and Cubic polynomial curves along with data points and…
RAJAMAHANTI HARSHITH
updated on 02 Sep 2020
Aim :
To perform curve fitting for given data set and to find best and perfect fit for that particular data set using MATLAB programming
Objective :
Writing a code to fit a Linear, Quadratic and Cubic polynomial for the Cp data.
Plotting the Linear, Quadratic and Cubic polynomial curves along with data points and improving the model fit by splitting the data by explaining the parameters which effect the fitness characteristics.
Curve Fitting :
Curve fitting is the process of constructing a curve or the mathematical function which possess the closest proximity to the real series of data.By curve fitting we can mathematically construct the functional relationship between the observed data set and parameter values.This method really helps in the mathematical modeling of an experimental or natural process.
Fitting a curve :
The first degree polynomial equation
y=ax+b
It is a line with slope a . A line will connect any 2 points so, a first degree polynomial is an exact fit through any 2 points with distinct x co ordinates.
The second degree polynomial equation
y=ax^2+bx+c
It is a curve which fits through 3 points.
The Third degree polynomial equation
y=ax^3+bx^2+cx+d
This curve will exactly fit to 4 points.
Parameters used to measure the fitness of the curve :
There are 4 important parameters
a) Sum of Squares due to error (SSE) :
It measures the total deviation of response values from the fit to the response values.It is also called as summed square of residuals.
If SSE ----> 0 then it is set to be a perfect fit.
b) R Square :
It measures how succesful the fit is in explaining the variation of the data. It is the square of correlation between the response values and the predicted response values. It is also called square of multiple multiple correlation coefficient and the coefficient of multiple determination.
It is defined as the sum of squares of regression(SSR) to the total sum of squares(SST)
SSR is basically the difference between the value of fitting function at the given point and the arthematic mean of data set.
R square can take on any value between 0 and 1. If the value is one then it is an indication of best fit.
c) Adj R Square :
It is the normalising variation of R^2 which neglects the variation of R Square due to insignificant Independent variables. It disregards the infinitesimally small increaments to R^2 and only considers the variables which actually contribute to the output of the system.
where n the no of data points and k is the no of independent variables
d) Root Mean Square Error :
It is an estimate of the standard deviation of the random component in the data and is also known as fit standard error and the standard error of regression.
where n the no of data points
Centering and scaling :
These are both forms of preprocessing numerical data, that is, data consisting of numbers, as opposed to categories or strings, for example; centering a variable is subtracting the mean of the variable from each data point so that the new variable's mean is 0; scaling a variable is multiplying each data point by a constant in order to alter the range of the data.
For numerical values it is very much common to normalise and standardise the data points.
Normalising is the process of scaling the dataset between 0 and 1
Standardisation scales the data around 0 with the help of mean and standard deviation
Where , σ=standard deviation and μ= mean
Downloading the given data file :
https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
MATLAB Code :
%generating a program for curve fitting
%Loading the data to matlab
Data=load('data');
%taking the temperature values from the data
temperature=Data(:,1);
%taking cp values from the data
cp=Data(:,2);
%considering the first order polynomial
coeffs=polyfit(temperature,cp,1);
predvalues=polyval(coeffs,temperature);
figure(1)
plot(temperature,cp,'linewidth',3,'color','b')
hold on
plot(temperature,predvalues,'linewidth',3,'color','r')
axis([0 3800 600 1600])
title('Linear plot')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
legend('actual cp','predicted cp with 1st order polynomail')
%considering 2nd order polynomial
coeffs1=polyfit(temperature,cp,2);
predvalues1=polyval(coeffs1,temperature);
figure(2)
plot(temperature,cp,'linewidth',3,'color','b')
hold on
plot(temperature,predvalues1,'linewidth',3,'color','g')
axis([0 4000 600 1600])
title('Quadratic plot')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
legend('actual cp','predicted cp with 2nd order polynomail')
%considering cubic polynomial
coeffs2=polyfit(temperature,cp,3);
predvalues2=polyval(coeffs2,temperature);
figure(3)
plot(temperature,cp,'linewidth',3,'color','b')
hold on
plot(temperature,predvalues2,'linewidth',3,'color','y')
axis([0 4000 600 1600])
title('Cubic plot')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
legend('actual cp','predicted cp with 3rd order polynomail')
%comparing all the curves in a single plot
figure(4)
hold on
plot(temperature,cp,'linewidth',3,'color','b')
plot(temperature,predvalues,'linewidth',3,'color','r')
plot(temperature,predvalues1,'linewidth',3,'color','g')
plot(temperature,predvalues2,'linewidth',3,'color','y')
legend('actual cp','predicted cp with 1st order polynomial','predicted cp with 2nd order polynomial','predicted cp with 3rd order polynomial')
title('comparision of Actual cp with predicted cp of all polynomial orders')
axis([0 4000 600 1600])
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
%splitting the temperature data
temperature1=Data(1:800,1)
cp1=Data(1:800,2)
coeffs3=polyfit(temperature1,cp1,3);
predvalues3=polyval(coeffs3,temperature1);
temperature2=Data(801:1600,1)
cp2=Data(801:1600,2)
coeffs4=polyfit(temperature2,cp2,3);
predvalues4=polyval(coeffs4,temperature2);
temperature3=Data(1601:2400,1)
cp3=Data(1601:2400,2)
coeffs5=polyfit(temperature3,cp3,3);
predvalues5=polyval(coeffs5,temperature3);
temperature4=Data(2401:3200,1)
cp4=Data(2401:3200,2)
coeffs6=polyfit(temperature4,cp4,3);
predvalues6=polyval(coeffs6,temperature4);
figure(5)
plot(temperature,cp,'linewidth',5,'color','b')
hold on
plot(temperature1,predvalues3,'linewidth',3,'color','m')
plot(temperature2,predvalues4,'linewidth',3,'color','m')
plot(temperature3,predvalues5,'linewidth',3,'color','m')
plot(temperature4,predvalues6,'linewidth',3,'color','m')
title('Best fit obtained with splitting data')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
legend('cp actual','cp predicted by 3rd order polynomial by split plots of the data')
%comparing data of without splitting by splitting
figure(6)
subplot(2,1,1)
plot(temperature,cp,'linewidth',5,'color','b')
hold on
plot(temperature,predvalues2,'linewidth',3,'color','y')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
title('comparing predicted values of cp 3rd order polynomials by without splitting and splitting data')
legend('actual cp','predicted cp with 3rd order polynomail')
subplot(2,1,2)
plot(temperature,cp,'linewidth',5,'color','b')
hold on
plot(temperature1,predvalues3,'linewidth',3,'color','m')
plot(temperature2,predvalues4,'linewidth',3,'color','m')
plot(temperature3,predvalues5,'linewidth',3,'color','m')
plot(temperature4,predvalues6,'linewidth',3,'color','m')
xlabel('temperature in (K)')
ylabel('cp(KJ/KMol-K)')
legend('cp actual','cp predicted by 3rd order polynomial by split plots of the data')
Explanation for the Code :
1) At first the downloaded data file has to be present in the same directory at which the Matlab files are saved and now load the data in to the matlab program
2)Read temperature and cp values from the data file.
3)Considering the first order polynomial
4) Evaluating the coefficients of the equation and calculating the predicted values by using polyfit and polyval commands.
5) Now considering the second order polynomial and evaluating the coefficients of equation and also calculating the predicted values
6)Futher incresing the polynomial order the 3rd order is considered and the predicted values are generated.
7)Plotting each and every order polynomial curve seperately by comparing with the original data curve
8)plot all the polynomial curves in a single plot which determines the best order curve.It tells that the cubic curve fits the best but a warning is generated which can be sorted out by centering and scaling in the fittool box or by any other technique.
9)Now we split the set of temperature and cp data into some intervals and repaet the same procedure and the corresponding predicted values and the fit is compared with unsplitted data.
Output plots :
It is observed that the R square value is 0.9249 which is < 0.95 so obviously its not a better fit.
Its some what better than Linear.
R square value is improved and its nearly equal to 1. Here centering and scaling are enabled for overcoming the warning of Bad fit.
It is clear that the yellow one fits better with the blue one.
It is clear that the split data fits exact with the actual curve.
Errors :
No code errors but a warning of bad data fit is appeared which can be sorted out by scaling and centering.
Conclusion :
There are many methods that make the curve fit properly
Higher order equation of polynomials are required to achieve the best fit
By splitting the data we get more better fit ,More the no of splits better will be the accuarcy of fit .
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
3D simulation of water in a curved pipe using snappyhexmesh in openfoam
Aim : To simulate the flow of water in a curved pipe using snappyhexmesh in openfoam Geometry in Meshlab : The different .stl files can be viewed in Meshlabs and the identification of refinement zones would become easy . Input data: velocity of water at the pipe inlet = 7m/sec at 25 deg blockMeshDict File : FoamFile…
02 Jun 2021 04:49 AM IST
2D simulation of flow over a square obstacle using openfoam
Aim : To calculate velocity contour and dragforce on a square obstacle placed in the air field and to automate the simulation process in openfoam Geometry : The geometry is created by blockMeshDict file in Openfoam. Input data : velocity at inlet = 10 m/sec Temperature at inlet = 25 deg c Reynolds number= Re…
01 Jun 2021 10:59 PM IST
Emission characterization on a CAT3410 engine
Emission Characterization on a CAT3410 Engine Aim: To run the simulation for emission characterization on a CAT3410 Engine. Objective: To run the simulation for emission characterization on a CAT3410 engine for two different piston profiles (i.e. Open-w,…
11 Apr 2021 09:06 PM IST
FINAL TEST
Q1. What is the Compression ratio for the engine? From the Volume vs Crank Angle Plot Here the maximum volume is = 0.0005738 and the minimum volume is = 5.7032 e-5 m^3 And the compression ratio is Maximum Volume / Minimum Volume = 0.0005738 / 5.7032e-5 = 10.06 Q2. Modify the compression ratio for this…
09 Apr 2021 11:25 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.