Curve Fitting
AIM:
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points. Title and axes labels are a must, legends could be shown if necessary.
- Explain the parameters used to measure the fitness characteristics for both the curves.
INTRODUCTION:
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to an orginal dataset. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values or measure the errors between an orginal pre-defined datasets and predicted data. It is highly effective in the mathematical modeling of some natural processes such as forecasting the population of a state or country, or a companys sales over a period of years purely by mathematical assumptions. This pedicted data may not be the exact data liberrated but can be helpful in making decisions and for many other purposes.
The curve fit are of many types such as Best fit, Perfect fit. They repesent the accuracy of the curve fit based on predicted data with respect to the orginal data.
PROCEDURE:
- Make sure the specified file containing the data should be on the same registry as the matlab file registry.
- Load the values using the Load command.
- Temperature data are stored in 'x' variable, Cp data are stored in 'y' variable by deriving the data from the array 'cp_data'.
- For linear fit, we need to find the co efficients. To find those, we use polyfit command and are stored in a variable varriable called p1.
- Similarly, for Cubic Polynomial curve fit using the polyfit command the data are stored in p3.
- Using these co efficients, we find new cp values for linear fit and cubic polynomial curve fit using polyval command and are stored in the variables y_fit1 and y_fit2.
- Plot is made between temperature and cp for all three cases using plot command.
- Appropriate title, legend, and axes label are given for graphs.
CODE 1: Curve Fitting
clear all
close all
clc
%Orginal data
cp_data= load('data');
x= cp_data(:,1);
y= cp_data(:,2);
%x=Temperature
%y=Specific Heat (Cp)
%y_fit= Predicted Cp
%p= coefficient of the polynomial
%Linear Curve Fit
% cp=a*T+b
% y=a*x+b
p1= polyfit(x,y,1);
y_fit1= polyval(p1,x);
figure(1)
hold on
plot(x,y,'linewidth',3,'color','r')
plot(x,y_fit1,'linewidth',3,'color','g')
grid on
title ('Linear Curve Fitting')
xlabel ('Temperature [K]')
ylabel ('Specific Heat KJ/Kmol-K')
legend ('Orginal Data','Linear Curve Fit')
%Cubic Polynomial Curve Fit
% cp=a*T^2+b*T+c
% y=a*x^2+b*x+c
p3= polyfit(x,y,3);
y_fit2= polyval(p3,x);
figure(2)
hold on
plot(x,y,'linewidth',3,'color','r')
plot(x,y_fit2,'linewidth',3,'color','b')
grid on
title ('Cubic Polynomial Curve Fitting')
xlabel ('Temperature [K]')
ylabel ('Specific Heat KJ/Kmol-K')
legend ('Orginal Data','Cubic Polynomial Curve Fit')
%Comparision of Linear and Cubic Polynomial Curve Fit
figure (3)
hold on
plot(x,y,'linewidth',3,'color','r')
plot(x,y_fit1,'linewidth',3,'color','g')
plot(x,y_fit2,'linewidth',3,'color','b')
grid on
title ('Comparision of Linear and Cubic Polynomial Curve Fit')
xlabel ('Temperature [K]')
ylabel ('Specific Heat KJ/Kmol-K')
legend ('Original Data','Linear Curve Fit','Cubic Polynomial Curve Fit')
OUTPUT:
ERRORS:
In the above image, the output was not proper and i did not get the curve fit in figure 2. This is was because i entered y_fit2 as y in the plot command.
Parameters used to measure the fitness characters of the curve are,
- The sum of squares due to error (SSE)
- Root mean squared error (RMSE)
- R-square
- Adjusted R-square
- Sum of squares due to error (SSE)
This can be calculated using the fomula ,
SSE(sum of squared errors)=n∑i=1Error(i)2
This formula is used ind determining the polynomial using the least square functions.
- R-square
This quantity that measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
What that means in layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from a value of 0 to 1
- 0 for the worst possible fit for the data set
- 1 for the best possible fit for the data set
- Adjusted R-square
Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.
for, y=f(x1,x2,x3,...,xn)y=f(x1,x2,x3,...,xn), here, x1,x2,...,xnx1,x2,...,xn are the independent variables and yy is the dependent variable.
- If value of x(i)x(i) contributes to the output yy, then x(i)x(i) is a significant dependent variable
- If value of x(i)x(i) does not contribute to the output yy, then x(i)x(i) is an insignificant dependent variable
- RMSE (Root Mean Squared Error)
This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as,
RMSE=√∑ni=1(Y(i)−f(x(i)))2n=√SSEnRMSE=∑i=1n(Y(i)-f(x(i)))2n=SSEn
where,
n is the total number of datapoints available.
FITNESS CHARACTRISTICS for Linear Curve Fit:
Linear model Poly1:
f(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.09875 (0.09778, 0.09973)
p2 = 995.2 (993.1, 997.3)
Goodness of fit:
SSE: 2.163e+06
R-square: 0.9249
Adjusted R-square: 0.9249
RMSE: 26.01
FITNESS CHARACTRISTICS for Cubic Polynomial Curve Fit:
Linear model Poly3:
f(x) = p1*x^3 + p2*x^2 + p3*x + p4
Coefficients (with 95% confidence bounds):
p1 = 1.906e-08 (1.876e-08, 1.936e-08)
p2 = -0.0001381 (-0.0001399, -0.0001364)
p3 = 0.3879 (0.3849, 0.3909)
p4 = 838.9 (837.5, 840.3)
Goodness of fit:
SSE: 9.427e+04
R-square: 0.9967
Adjusted R-square: 0.9967
RMSE: 5.431
Q & A
- How to make a curve fit perfectly?
To make a curve fit perfectly or get a more robust curve , we can increase the degree of polynomial or use split-wise polynomial that seperates the set of data into two different ranges and are specified to two differetn variables and then plotted together.
- How to get the best fit?
In orde to get Best fit we need to to follwoing steps.
-
Load some data at the MATLAB® command line.
load ('data') -
Open the Curve Fitting app. Enter:
cftool
-
In the Curve Fitting app, select X Data and Y Data.
Curve Fitting app creates a default interpolation fit to the data.
-
Choose a different model type using the fit category drop-down list, e.g., select Polynomial.
-
Try different fit options for your chosen model type.
-
Select File > Generate Code.
- What could be done to improve the cubic fit?
To impove the cubic fit, we can use split-wise polynomial that seperates the set of data into two different ranges and are specified to two differetn variables and then plotted together or we can add points with distinct values for x.
REFERENCES:
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
https://www.youtube.com/watch?v=LP0YrsMGMAM
https://in.mathworks.com/help/curvefit/curve-fitting.html
RESULT: Thus, the Linear Curve Fit and Cubic Polynomial Curve Fit for the set of data containing temperature and specific heat of oxygen have been executed using mathematical functions in MATLAB.