Abhishek S. Jadhav
A) Aim:- Write code in MATLAB to fit a linear and cubic polynomial for the Cp data.
B) Objective:-
- 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.
- Validate your fitness characteristics by using curve fitting toolbox in MATLAB.
C) Theory:-
1. Linear polynomial:- A linear polynomial is any polynomial defined by an equation of the form.
`p(x)=ax+b`
Where,
a & b =real numbers and `a≠0`
A linear polynomial is the same thing as a degree 1 polynomial.
For example `x-y=4`
Solution-
Step 1- Find equation of y
`y=4-x`
Step 2- Take random number for x
Step 3- Plot the point on graph & join it, as straight line as below.
2. Cubic polynomial:- A cubical polynomial is any polynomial defined by an equation of the form.
`p(x)=ax^3+bx^2+cx+d`
Where,
a, b & c=real numbers and `a≠0`
For example, `p(x)=x^3+x^2+4x-4`
Solution-
Step 1- To find x axis intercept (s) by put y=0 & solve for x
`y=x^3+x^2+4x-4`
`0=x^3+x^2+4x-4`
`0=x^2 (x+1)-4(x+1)`
`0=(x+1)(x^2-4)`
`0=(x+1)(x+2)(x-2)`
`Now,x=-1,-2,2`
Plot value of x= -1,-2,2 on graph
Step 2- Find y axis interception by setting x=0 & solve for y
`y=x^3+x^2+4x-4`
`y=0^3+0^2+4(0)-4`
`y=-4`
Plot value of x= -4 on graph
Step 3- Select critical x value, find (x) plot add point
Plot value of x=-6,-10 on graph
Step 4- Draw cubic curve by using plotted point on graph that changes direction as below
3. Various curve fitting function uses in programming
3.1) fit
Use the fit function to fit a polynomial to data. You specify a quadratic, or second-degree polynomial, with the string 'poly2'. The first output from fit is the polynomial, and the second output, gof, contains the goodness of fit statistics you will examine in a later step.
- Syntax
[population2,gof] = fit(cdate,pop,'poly2');
3.1.1) gof
The struct gof shows the goodness-of-fit statistics for the 'poly2' fit. When you created the 'poly2' fit with the fit function in an earlier step, you specified the gof output argument.
- Output window
3.1.2) population2 :- This contain polynomial coefficient & confidence bound
- Output window
4. Residuals
- The residuals from a fitted model are defined as the differences between the response data and the fit to the response data at each predictor value.
residual = data - fit
- You display the residuals in the Curve Fitting Tool by selecting the menu item View->Residuals.
- Mathematically, the residual for a specific predictor value is the difference between the response value yand the predicted response value
- Assuming the model you fit to the data is correct, the residuals approximate the random errors. Therefore, if the residuals appear to behave randomly, it suggests that the model fits the data well. However, if the residuals display a systematic pattern, it is a clear sign that the model fits the data poorly.
- A graphical display of the residuals for a first-degree polynomial fit is shown below. The top plot shows that the residuals are calculated as the vertical distance from the data point to the fitted curve. The bottom plot shows that the residuals are displayed relative to the fit, which is the zero line.
- The residuals appear randomly scattered around zero indicating that the model describes the data well.
- A graphical display of the residuals for a second-degree polynomial fit is shown below. The model includes only the quadratic term, and does not include a linear or constant term.
- The residuals are systematically positive for much of the data range indicating that this model is a poor fit for the data.
- Following graph show the residual in cubic polynomial curve fitting residual
5. How to choose best fit
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are,
5.1) sum of squares due to error (SSE)
5.2) R-square
5.3) Adjusted R-square
5.4) Root mean squared error (RMSE)
5.1) Sum of squares due to error (SSE)
Suppose each of these n points has an individual error (difference in y co-ordinate values)
The error for a general "i" point can be given by the function,
`Error=|y(i)-value of f(x)at x(i)|=|y(i)-f(x(i))|`
To remove the modulus sign, it is effective if we square the error term, when we square the error terms and add the errors for all the 'n' points we get,
assume the proposed equation is a nth degree polynomial of the form,
`f(x)=A1+A2.x+A3.x^2+⋯+An.x^(n-1)`
When we differentiate the SSE with each and every coefficient of the proposed equation f(x) we and equate it to zero, we can find the minima and the value of that coefficient.
Upon solving the multiple system of equations, we can find the values of all the coefficients.
This method is known as the Least Squares Method and is the most commonly used technique as long as the fitting function is a polynomial
5.2) 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
The value of R-square can be calculated using the following formula,
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 arithmetic mean of the data set.
if y=f(x)y=f(x) is the fit curve then the SSRSSR is defined as follows,
`SSR=∑_(i=1)^n(f(x(i)-Mean)^2`
Similarly, SSE is defined as follows,
`SSE=∑_(i=1)^n(y(i)-f(x(i)))^2`
Now the term SST is given by the summation of SSR and SSE
SST=SSR+SSE
From here, we can calculate the value of `R^2`
5.3) 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(x_1,x_2,x_3,….,x_n ) here,x_1,x_2,…..x_n` are the independent variables and y is the dependent variable.
If value of x(i) contributes to the output y, then x(i) is a significant dependent variable
If value of x(i)does not contribute to the output y, then x(i)is an insignificant dependent variable
Adjusted R-square disregards these infinitesimally small increments to R-square, and only considers the variables which actually contribute to the output of the system in a drastic way.
R-square is formulated as shown below,
where,
n - the number of datapoints
k - the number of independent variables
5.4) Root mean squared error (RMSE)
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,
where,
n =total number of datapoints available.
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.
D) Linear polynomial: -
Program for linear curve-fitting
Program for linear curve fitting
clear all
close all
clc
%it will load the data of the file to cp_temp variable
cp_temp=load('data');
%first column of the data is the temperature
temperature=cp_temp(:,1);
%second column of the data is cp=specific heat
cp=cp_temp(:,2);
%Calculate Polynomial 1/linear equation
[pol_no1,gof1] = fit(temperature,cp,'poly1');
%plot graph temprature vs cp as per orignal data sheet
plot(temperature,cp,'linewidth',2)
%hold the graph plotted as above
hold on
%plot the linera polynomial equation graph in above graph
plot(pol_no1);
%name the legends
legend('Temprature Vs cp','Linear curve-fit','Location','NorthWest');
%label the x-axis
xlabel('Temperature [k]')
%label the y-axis
ylabel('Specific heat [KJ/kmol-k]')
%holding off 1st ploted graph
hold off
%Produces the linera polynomial equation, its coefficients & confidence bounds
pol_no1
%Produces the Goodneass of fit for linear polynomial.
gof1
2. Linear polynomial curve fit graph
3. Screenshot of linear polynomial program command window
4. Program for linear curve fitting tool box
%Program to validate your fitness characteristics by using Curve fitting toolbox in MATLAB.
clear all
close all
clc
%it will load the data of the file to cp_temp variable
cp_temp=load('data');
%first column of the data is the temperature
temperature=cp_temp(:,1);
%second column of the data is cp=specific heat
cp=cp_temp(:,2);
%plot graph temprature vs cp as per orignal data sheet
figure(1)
plot(temperature,cp,'linewidth',2);
xlabel('Temperature')
%label the y-axis
ylabel('Specific heat')
%name the legends
legend('Temprature Vs cp','Location','NorthWest');
5. Curve fitting toolbox screenshot for linear polynomial
6. Residuals in linear polynomial.
E) Cubical polynomial: -
1) Program for cubical curve-fitting
%Program to cubic curvefitting
clear all
close all
clc
%it will load the data of the file to cp_temp variable
cp_temp=load('data');
%first column of the data is the temperature
temperature=cp_temp(:,1);
%second column of the data is cp=specific heat
cp=cp_temp(:,2);
%Calculate Polynomial 3/cubical equation
[poly_nom3,gof3] = fit(temperature,cp,'poly3','Normalize','on');
%plot graph temprature vs cp as per orignal data sheet
figure(1)
plot(temperature,cp,'linewidth',2);
%hold the graph plotted as above
hold on
%plot the cubical polynomial equation graph in above graph
plot(poly_nom3);
%label the x-axis
xlabel('Temperature [k]')
%label the y-axis
ylabel('Specific heat [KJ/kmol-k]')
%name the legends
legend('Temprature Vs cp','Cubic curve-fit','Location','NorthWest');
%holding off 1st ploted graph
hold off
%Produces the cubical polynomial equation, its coefficients & confidence bounds
poly_nom3
%Produces the Goodneass of fit for cubical polynomial.
gof3
2) Cubic polynomial curve fit graph
3) Screenshot of cubical polynomial program command window
4) Program for cubic polynomial curve fitting tool box
%Program to validate your fitness characteristics by using Curve fitting toolbox in MATLAB.
clear all
close all
clc
%it will load the data of the file to cp_temp variable
cp_temp=load('data');
%first column of the data is the temperature
temperature=cp_temp(:,1);
%second column of the data is cp=specific heat
cp=cp_temp(:,2);
%plot graph temprature vs cp as per orignal data sheet
figure(1)
plot(temperature,cp,'linewidth',2);
%label the x-axis
xlabel('Temperature [k]')
%label the y-axis
ylabel('Specific heat [KJ/kmol-k]')
%name the legends
legend('Temprature Vs cp','Location','NorthWest');
5) Curve fitting toolbox screenshot for cubical polynomial
6) Residuals in cubical polynomial
F) Cubic polynomial with split data
1) Program for cubical curve-fitting
clc
close all
clear all
cp_data=load('data');
%assign given data
temp=cp_data(:,1);
cp=cp_data(:,2);
temp1=cp_data(1:460,1);
cp1=cp_data(1:460,2);
temp2=cp_data(461:1100,1);
cp2=cp_data(461:1100,2);
temp3=cp_data(1101:2200,1);
cp3=cp_data(1101:2200,2);
temp4=cp_data(2201:3200,1);
cp4=cp_data(2201:3200,2);
%Calculate Polynomial 3/cubical equation
[poly_coff1,gof1] = fit(temp1,cp1,'poly3','Normalize','on','Robust','on');
[poly_coff2,gof2] = fit(temp2,cp2,'poly3','Normalize','on','Robust','on');
[poly_coff3,gof3] = fit(temp3,cp3,'poly3','Normalize','on','Robust','on');
[poly_coff4,gof4] = fit(temp4,cp4,'poly3','Normalize','on','Robust','on');
%curve fit
figure(1)
plot(temp,cp)
hold on
plot(poly_coff1,temp1,cp1)
plot(poly_coff2,temp2,cp2)
plot(poly_coff3,temp3,cp3)
plot(poly_coff4,temp4,cp4)
hold off
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Splitwise Curve Fit Data-Cubic Curve Fit (3rd order)')
axis([250 3600 900 1400])
legend('Original Data','Splitwise Data set 1','Splitwise Data set 2','Splitwise Data set 3','Splitwise Data set 4','location','northwest')
poly_coff1
gof1
poly_coff2
gof2
poly_coff3
gof3
poly_coff4
gof4
figure(2)
plot(poly_coff1,temp1,cp1,'residuals');
figure(3)
plot(poly_coff2,temp2,cp2,'residuals');
figure(4)
plot(poly_coff3,temp3,cp3,'residuals');
figure(5)
plot(poly_coff4,temp4,cp4,'residuals');
2) Cubic polynomial curve fit graph
3) Residuals in various data set
Data set 1
Data set 2
Data set 3
Data set 4
G) Various curve fitting at diffrent order of polynomia
- 1st Order polynomial curve fit
- 2nd Order polynomial curve fit
- 3rd Order polynomial curve fit
- 4th Order polynomial curve fit
- 5th Order polynomial curve fit
- 6th Order polynomial curve fit
- 7th Order polynomial curve fit
- 8th Order polynomial curve fit
- 9th Order polynomial curve fit
Observation:-
- As your can see, from 1st order polynomial to 9th order polynomial the residual drop gradually from 1470.708 to 9.7894 respectively.
- By our observation residual reduces by increasing order of polynomial in curve fitting.
- Its not perfect fit but its one of the way to get best fit.
H) What could be done to improve the cubic fit?
- To get improved cubic fit, while using fit command use variable ‘normalize’, ‘on’
- To normalizes data in A using the 'zscore' method, which centers the data to have mean 0 and scales it to have standard deviation 1
- This error occurred due to number of input data point available for given polynomial, also these points may have error.
- So, to eliminate this error, we have to use Normalising on.
I) How to make perfect fit
- In order to make perfect fit there should be no error present in fit,
- 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 cannot achieve because always some error has been present.
- Best fit can achieved. So, in the 3rd order polynomial
- I have eliminated the errors, that means curve fit we got using 'Linear regression with error estimates' is perfect fit for 3rd order polynomial. But it is still not the best fit.
- To calculate best fit, we must increase the order or use split wise method.
J) How to get the best fit
- For this, we can use RMS(Root Mean square) or simple linear regression.
- We can change the order of polynomial to get the best fit. & the best way is, split the data in two or more data folds and do the curve fit.
- I have shown how to make a best fit using 3rd order polynomial in by spliting data in various data sets
- If we use, changing polynomial order method to get the curve fit, then 9th order is the best fit.
- But, in split-wise method that I have shown at the bottom, even the 3rd order can give the best fit.
K) Conclusion
- Curve fit can be improved using increase in order of polynomial, its not always practical then we have to find best way to fit curve
- Another best way to fit curve is to split data into no of data sets & plot the graph as shown above e.g. Of cubical polynomial with split data sets.
- Due to split of data error can be reduce hence your curve fit can be closer to the best fit.
- Best fit can be judged by Goodness of fit.
- When we have large no of data inputs, we need to use centering & scaling or Normaliz on
- Observation of linear & cubic polynomial
References
- Goodness of curve fitting: http://www.math.utah.edu/~palais/pcr/spike/Evaluating%20the%20Goodness%20of%20Fit%20--%20Fitting%20Data%20(Curve%20Fitting%20Toolbox)%20copy.html
- NPTEL:- https://nptel.ac.in/courses/112/106/112106064/
- Skill-lync:- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860