MatLab Program to fit a Linear and Cubic Polynomial for the given Cp data
AIM : To execute a Program to curve fit for both Linear and Cubical Polynomial for the given Cp data using Matlab.
OBJECTIVE :
1. To write code to fit a linear and cubic polynomial for the Cp data.
2. To plot the linear and cubic fit curves along with the raw data points.
3. To explain the parameters used to measure the fitness characteristics for both the curves.
Introduction to Curve Fitting :
Historians attribute the phrase regression analysis to Sir Francis Galton (1822-1911), a British anthropologist and meteorologist, who used the term regression in an address that was published in Nature in 1885. Galton used the term while talking of his discovery that offspring of seeds “did not tend to resemble their parent seeds in size, but to be always more mediocre [i.e., more average] than they.... The experiments showed further that the mean filial regression towards mediocrity was directly proportional to the parental deviation from it.”
The content of Galton’s paper would probably be called correlation analysis today, a term which he also coined. However, the term regression soon was applied to situations other than Galton’s and it has been used ever since.
Regression Analysis refers to the study of the relationship between a response (dependent) variable, Y, and one or more independent variables, the X’s. When this relationship is reasonably approximated by a straight line, it is said to be linear, and we talk of linear regression. When the relationship follows a curve, we call it curvilinear regression.
THEORY :
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting 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.
Curve fitting are really usefull as they help us in describing how our data changes mathematically, once we have curvefit we can do additional mathematical operations which might not be possible with our initial dataset.
Origin provides tools for linear, polynomial and non linear curve fitting along with validation and goodness of fit tests. You can summerize and present your results with customized fitting reports. There are many time saving options such as a copy and paste operation feature which allows you to paste a just completed fitting operation to another curve or data column. Curve fitting operations can also be part of Analysis template, allowing you to perform batch fitting operations on any number of data files or data column.
Two types curve fitting
1. Least Square Regression
If your data shows a linear relationship between the X and Y variables, you will want to find the line that best fits that relationship. That line is called a Regression Line and has the equation ŷ= a + b x. The Least Squares Regression Line is the line that makes the vertical distance from the data points to the regression line as small as possible
2. Interpolation
In the mathematical field of numerical analysis, interpolation is a type of estimation, a method of constructing new data points within the range of a discrete set of known data points.
We are assuming this below graph as our initial dataset.
Goodness of fit statistics
There are four various goodness of fit statistics which is used to measure goodness of fit using the data points of the curve, The Curve Fitting Toolbox supports these goodness of fit statistics for parametric models:
A) Sum of Squared Errors (SSE) -
This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE. A value closer to 0 indicates a better fit.
SSE is defined as follows,
B) R-Square -
This statistic 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.
R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).
R-square can take on any value between 0 and 1, with a value closer to 1 indicating a better fit. For example, an R2 value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average.
If you increase the number of fitted coefficients in your model, R-square might increase although the fit may not improve. To avoid this situation, you should use the degrees of freedom adjusted R-square statistic described below.
R-Square as defined as follows,
Where, SSR = Sum of squares of the regression, which is basically the square of the difference between the value of the fitting function at a given point and the arithmatic mean of the data set.
C) 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. It is used to define the the fitness quality of function when we add other coefficient to the plot.
Where,
n - Number of data set points.
k - Number of independent variables.
D) Root Mean Squared Error (RMSE) -
This statistic is also know 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,
Where,
n - Number of data set points.
PROGRAMS:
1.PROGRAMMING FOR BOTH LINEAR AND CUBIC POLYNOMIAL FOR THE GIVEN CP DATA WITHOUT SPLITTING AND WITHOUT FINDING THE BEST FIT:
%**PROGRAMMING FOR BOTH LINEAR AND CUBIC POLYNOMIAL FOR THE GIVEN CP DATA**
% Clearing all previous memory in the workspace
clear all
% Terminating all the previous plots
close all
% Clearing all the values in command window
clc
% Loading the dataset from data file
cp_data = load('data');
% Defining the data for 'cp' and 'temperature'
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Fitting Curve
% Linear Polynomial
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
% Fitting Curve
% Cubic Polynomial
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
% Plotting
% Compare my curve fit with the original data
plot(temperature,cp,'linewidth',3);
grid on
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
plot(temperature,predicted_cp2,'linewidth',3,'color','y')
xlabel('Temperature[K]');
ylabel('Specific Heat[Kj/K-mol-K]');
legend('Original Dataset','Linear Curve fit','Curve fit')
title('Curve fit for a Linear Polynomial')
hold off
Output plot of the above program.
Error:
The error sign is due to the usage of cubic polynomial and to resolve this issue we need to split the complete datapoints into smaller quatities the perform the curve fit using linear and quadratic polynomials.
2.PROGRAMMING FOR LINEAR POLYNOMIAL FOR THE GIVEN CP DATA BY SPLITTING WITHOUT USING FOR-LOOP:
%**PROGRAMMING FOR LINEAR POLYNOMIAL FOR THE GIVEN CP DATA BY SPLITTING**
% Clearing all previous memory in the workspace
clear all
% Terminating all the previous plots
close all
% Clearing all the values in command window
clc
% Loading the dataset from data file
cp_data = load('data');
% Defining the data for 'cp' and 'temperature'
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Fitting curve
% Cp = a*T^2+b*T+c
% 1st data point
T1 = temperature(1:300,1)
S1 = cp(1:300,1);
a = polyfit(T1,S1,2)
P1 = polyval(a,T1);
% 2nd data point
T2 = temperature(320:640,1);
S2 = cp(320:640,1);
b = polyfit(T2,S2,2)
P2 = polyval(b,T2);
% 3rd data point
T3 = temperature(640:960,1);
S3 = cp(640:960,1)
c = polyfit(T3,S3,2)
P3 = polyval(c,T3);
% 4th data point
T4 = temperature(960:1280,1);
S4 = cp(960:1280,1)
d = polyfit(T4,S4,2)
P4 = polyval(d,T4);
% 5th data point
T5 = temperature(1280:1600,1);
S5 = cp(1280:1600,1)
e = polyfit(T5,S5,2)
P5 = polyval(e,T5);
% 6th data point
T6 = temperature(1600:1920,1);
S6 = cp(960:1280,1)
f = polyfit(T6,S6,2)
P6 = polyval(f,T6);
% 7th data point
T7 = temperature(1920:2240,1);
S7 = cp(1920:2240,1)
g = polyfit(T7,S7,2)
P7 = polyval(g,T7);
% 8th data point
T8 = temperature(2240:2560,1);
S8 = cp(2240:2560,1)
h = polyfit(T8,S8,2)
P8 = polyval(h,T8);
% 9th data point
T9 = temperature(2560:2880,1);
S9 = cp(2560:2880,1)
k = polyfit(T9,S9,2)
P9 = polyval(k,T8);
% 10th data point
T10 = temperature(2880:3200,1);
S10 = cp(2880:3200,1)
l = polyfit(T10,S10,2)
P10 = polyval(l,T10);
% Plotting
% Compare my curve fit with original data
plot(temperature,cp,'linewidth',4)
hold on
plot(T1,S1,T2,S2,T3,S3,T4,S4,T5,S5,T6,S6,T7,S7,T8,S8,T9,S9,T10,S10,'linewidth',3)
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/Kmol-K')
legend('Original dataset','Curvefit')
Output plot of the above program.
3.PROGRAMMING FOR BOTH LINEAR AND CUBICAL POLYNOMIAL FOR THE GIVEN CP DATA BY SPLITTING:
%**PROGRAMMING FOR BOTH LINEAR AND CUBICAL FOR THE GIVEN CP DATA BY SPLITTING**
% Clearing all previous memory in the workspace
clear all
% Terminating all the previous plots
close all
% Clearing all the values in command window
clc
% Loading the dataset from data file
cp_data = load('data');
% Defining the data for 'cp' and 'temperature'
temperature = cp_data(:,1);
cp = cp_data(:,2);
x = mean(cp)
n = 8
% Linear curve fit
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
% Split
a = 1;
b = 3200/n
for j = 1:n
co_effs1_split = polyfit(temperature(a:b),cp(a:b),1);
predicted_cp1_split(a:b) = polyval(co_effs1_split,temperature(a:b));
a = b+1;
b = b+(3200/n)
end
plot(temperature,cp,'linewidth',4)
hold on
plot(temperature,predicted_cp1_split,'linewidth',3,'color','r')
% Calculation for R Square for linear fit for the split
SSR2 = 0;
SSE2 = 0;
for i = 1:length(cp)/n
SSR2 = SSR2+(predicted_cp1_split(i)-x).^2;
SSE2 = SSE2+(cp(i)-predicted_cp1_split(i)).^2;
Rsquare1 = SSR2/(SSR2+SSE2)
RMSE1 = (SSE2/length(cp)).^0.5
end
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/Kmol-K')
% Cubic curve fit
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
% Split
a = 1;
b = 3200/n
for j = 1:n
co_effs2_split = polyfit(temperature(a:b),cp(a:b),1);
predicted_cp2_split(a:b) = polyval(co_effs2_split,temperature(a:b));
a = b+1;
b = b+(3200/n)
end
plot(temperature,cp,'linewidth',4)
hold on
plot(temperature,predicted_cp2_split,'linewidth',3,'color','Y')
% Calculation for R Square for linear fit for the split
SSR2 = 0;
SSE2 = 0;
for i = 1:length(cp)/n
SSR2 = SSR2+(predicted_cp2_split(i)-x).^2;
SSE2 = SSE2+(cp(i)-predicted_cp2_split(i)).^2;
Rsquare2 = SSR2/(SSR2+SSE2)
RMSE2 = (SSE2/length(cp)).^0.5
end
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/Kmol-K')
legend('Original dataset','Linear curvefit','cubic curvefit')
% Loop for determining good fit using R^2 value
if [Rsquare1>0.95,Rsquare2>0.95]
disp('The fit is GOOD because Rsquare>0.95')
else disp('The fit is NOt GOOD because Rsquare<=0.95')
end
Output plot of the above program.
Final Result.
CONCLUSION:
In the graph before calculating the goodness fit the generated data points doesn't match with the original datapoints, so we used split-wise method. In this method the errors reduced by using least square method and the curves are fitted perfectly. The fitness of the curve is decided by R^2 Value.