Matlab Program to fit a linear and cubic polynomial for the Cp data
Aim:
To write a Program in Matlab to fit a linear and cubic polynomial for the Cp data and to Plot the linear and cubic fit curves along with the raw data points.
Objective:
1.The main objective is to write a code to fit a linear and cubic polynomial for the Cp data
2.And to Plot the linear and cubic fit curves along with the raw data points.
Introduction:
What is Curve Fitting?
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.
Goodness of Fit is decided on the basis of 4 measurable quantities.
1- Sum of Squared Error (SSE).
2- R- Square
3- Adjusted R square.
4- Root Mean Squared Error (RMSE).
Using these four quantities Matlab effectively deduces the goodness of fit, typically is R-square is greater than 0.95 the fit is assumed to be good.
How to make perfect fit:
In order to make perfect fit, we must consider error estimates as well. Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling errors) in that particular degree of polynomial. Perfect fit can always be a best fit but best fit cannot be a perfect fit. To calculate best fit, we must increase the order or use split-wise method.
How to get the best fit:
To get the best fit, RMS(Root Mean square) or simple linear regression can be used . We can change the order of polynomial to get the perfect fit. And the best way is, split the data in two or more data folds and to do the curve fit.
What could be done to improve the cubic fit?
Split-wise method is best method that can be used to improve the cubic fit.
Software used:
MATLAB R2020(Online)
CODE:
For Linear polynomial :
%Program for linear polynomial curve
clear all
close all
clc
%Extracting Data from 'data' file
cp_data = load('data'); %Loading Data from file
temperature = cp_data(:,1); %taking 1st column as temperature values
cp = cp_data(:,2); %taking 2nd column vs specific heat values
%Curve Fitting
%Let us assume function f(x) as
%cp = a*x + b
%For Linear polynomial
co_eff1 = polyfit(temperature,cp,1); %Polyfit for 1st order Polynomial
predicted_cp1 = polyval(co_eff1,temperature);
%Comparing the original & linear curves
plot(temperature,cp,'linewidth',3,'color','b'); %Original Data Curve
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r') %Curve for linear eqn
hold off
%Labelling the x and y axis
xlabel('temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Data Set','Linear')
%To measure the goodness of fit
%Mean = Sum (cp) / lenght (cp)
sum1= 0;
for i = 1:length(cp);
sum1 = sum1+cp(i);
end
mean= sum1/length(cp); %mean value
%Calculating the value of SSR, SSE, SST for linear
%Caculating SSR1
SSR1=0;
for i = length(cp)
SSR1 = SSR1+(cp(i)-mean)^2;
end
%Caculating SSE1
SSE1=0;
for i = length(cp)
SSE1 = SSE1+(predicted_cp1(i)-cp(i))^2;
end
%Caculating SST1
SST1 = SSR1+SSE1;
%Caculating R1
R1 = sqrt(SSR1/SST1);
%Caculating RMSE1
RMSE1 = (SSE1/length(cp))^0.5
For Cubic polynomial :
%Program for cubic polynomial curve
clear all
close all
clc
%Extracting Data from 'data' file
cp_data = load('data');%Loading Data from file
temperature = cp_data(:,1);%taking 1st column as temperature values
cp = cp_data(:,2);%taking 2nd column vs specific heat values
%Curve Fitting
%Let us assume function f(x) as
%cp = a*x^3 + b*x^2 + c*x + d
%For Cubic polynomial
co_eff3 = polyfit(temperature,cp,3);
predicted_cp3 = polyval(co_eff3,temperature);
%Comparing the original & cubic curves
plot(temperature,cp,'linewidth',3,'color','r'); %Original Data Curve
hold on
plot(temperature,predicted_cp3,'linewidth',3,'color','g') %Curve for cubic eqn
hold off
%Labelling the x and y axis
xlabel('temperature [K]')
ylabel('specific heat [KJ/Kmol-K]')
title('Comparing Original Curve vs Cubic Curve')
legend('Original Data Set','Cubic')
%Mean for actual data
%Mean = Sum (cp) / lenght (cp)
sum1= 0;
for i = 1:length(cp);
sum1 = sum1+cp(i);
end
mean= sum1/length(cp); %mean value
%Calculation for 3rd order of polynomial
%Calculating the value of SSR, SSE, SST for Cubic polynomial
%Caculating SSR3
SSR3=0;
for i = length(cp)
SSR3 = SSR3+(cp(i)-mean)^2;
end
%Caculating SSE3
SSE3=0;
for i = length(cp)
SSE3 = SSE3+(predicted_cp3(i)-cp(i))^2;
end
%Caculating SST3
SST3 = SSR3+SSE3;
%Caculating R3
R3 = sqrt(SSR3/SST3);
%Caculating RMSE3
RMSE3 = (SSE3/length(cp))^0.5
Code Explanation:
1) In order to extract the values of temperature and specific heat values to plot the curve first Load the data file in to Matlab using load('data') command.Before loading the file ensure that the file is in the same folder where our current program is running.
2) Extract the data of specific heat(cp = cp_data(:,2)) and temperature(temperature = cp_data(:,1)) from the raw data present in the “data”file.
3) Use polyfit command to return the coefficient of the respective polynomial .
4) Use polyval command to evaluate the respective polynomial for each point of x.
5) Plotting the original data set and linear data points and comparing both the graph using the Matlab toolbox using (cftool command) to improve the curve fit.
6)Define the x-axis as temperature and y-axis as our cp or say predicted cp to find the value of R through the tool.
7)Label the x axis and y axis as temperature and specific heat respectively.
8)Add legends and titles to provide clear understanding visually also add linewidth and colors to differentiate the curves.
9) Here we go for the error calculation method to find the goodness of curve using the R-square method.We find the value of SSE, SSR, SST, R^2 and RMSE value using their relations as described above.
10) The same process is followed in both the programs to join the curve to get the smooth fit and perfect fit.
Output:
For Linear polynomial :
For Cubic polynomial :
Values Of RMSE
Linear Polynomial = 0.5759
Cubic Polynomial = 0.2376
Errors faced:
1)
Mentioned the comments without "%" operator.
2)
Entered the wrong iterating variable(j) instead of 'i'
References:
1.https://en.wikipedia.org/wiki/Curve_fitting
2.https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
3.https://skill-lync.com/projects/Week-4-Curve-fitting-31550
Conclusions:
Finally, using MATLAB fit for linear and cubic polynomial for the Cp data and Plot for the linear and cubic fit curves along with the raw data points is successfully performeds.Similarly using programs like these the higher degree polynomial equations can also be solved.