Curve Fitting using Linear & Cubic Polynomials and improving the fit
Report
Curve Fitting of Temperature vs Specific Heat data :
- Introduction
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.
- Objective of this program is to make a curve fitting of the provided temperature vs specific heat (cp) data .
- To improve the fit and obtain the error results showing how good our fit is and understanding it in curve fitting tool in matlab
- Prediction of future path of the fit by extending its range and predicting the direction and curve if our chosen method , polynomials in this case is true and future proof.
- To begin with we load the data we have using load command in matlab and specifying what value belongs to temperature and then specific heat .
- The size of the data is mentioned in the above image . Temperature is in kelvin and begins from 300 , and Cp (specific heat) is in KJ/KMOL-K .
- Program 1 :
clear all
close all
clc
% preparing data
whos temperature cp
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
whos temperature cp;
% curve fit
% linear polynomial - cp = a*T + b
coeffs = polyfit(temperature,cp,1);
predicted_cp_linear_poly = polyval(coeffs,temperature);
% cubic polynomial - cp = a*T^3 + b*T^2 + c*T + d
coeffs = polyfit(temperature,cp,3);
predicted_cp_cubic_poly = polyval(coeffs,temperature);
% Compare curve fit with original data
figure('name','Curve fit','numbertitle','off');
plot(temperature,cp,'linewidth',1,'color','b')
hold on
plot(temperature,predicted_cp_linear_poly,'g--')
plot(temperature,predicted_cp_cubic_poly,'r--')
title('Comparing curve fits with original data')
xlabel('temperature (K)')
ylabel('specific heat [KJ/KMOL-K]')
legend('original dataset','Linear polynomial curve fit','Cubic polynomial curve fit')
legend( 'Location', 'NorthWest' );
- Explanation of Code :
- The temperature vs cp is plotted using the standard given data called original data in the program
- The curve fit for the original data is plotted using linear polynomial & cubic polynomial .
- cp = a*T + b & cp = a*T^3 + b*T^2 + c*T + d which gives us 2 and 4 coefficient values .
- The coefficient is plotted using polyfit command between temperature and cp for 1 order of polynomial . polyfit function computes least square polynomials for a given set of data and generates coefficients to model the curve .
- Then predicted_cp_linear_poly is assigned polyval function which returns the coefficients from the above polyfit function of the given polynomial order .
- The same as above is repeated for cubic polynomial and variable predicted_cp_cubic_poly is assigned to it .
- Then a graph is plotted between the temperature vs linear polynomial predicted values & temperature vs cubic poly predicted values .
- This gives the plot between all the three, original data and 2 predicted paths from linear and cubic polys.
- As it can be seen that the linear path doesnt provides the accurate fit but cubic one is close to accurate .
- To know for sure the error in them is needed to be known and how good the fit is .
- Rather than using the mathematical approach , "curve fitting tool" of matlab is used .
- For the linear predicted polynomial with 2 coefficients , the fit is not good , as the results can be seen , R-square is 0.92 < 0.95 the fit is improper and not good and its also visvually agreeable .
- whereas for the predicted poly with 4 coefficients i.e with 3 order can be seen in the tool as , :
- This looks visvually great and close to good fit , and also mathematically the error R-square and adjusted R-square are 0.99 > 0.95 , hence it is a good fit , also the SSE , Rmse other quantities are solved for automatically to determine the goodness of the fit .
- But when the 3rd order polynomial is used in the above curve fit we get a warning :
That the polynomial is badly conditioned and we need to add points with distinct x values and reduce the degree or polynomial . so need is to find a better solution to fit the curve and not just simply increase the polynomial order .
- Program 2 :
clear all
close all
clc
% preparing data
whos temperature cp
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% seperating the main data of temp and cp in half (1-1500)
cp01 = cp([1:1500]);
temp01 = temperature([1:1500]);
% seperating the main data of temp and cp in other half (1500-3200)
cp001 = cp([1500:3200]);
temp001 = temperature([1500:3200]);
figure('name','Curve fit','numbertitle','off')
plot(temperature,cp,'linewidth',1.5,'color','g')
hold on ;
[pred_cp2 , gof] = fit(temp01,cp01,'poly2')
plot(pred_cp2,'r--')
[pred_cp3 , gof] = fit(temp001,cp001,'poly2')
%pred_cp3 = fit(temp001,cp001,'poly2')
plot(pred_cp3,'b--')
hold off
title('Curve fit for temperature vs specific heat')
xlabel('temperature (K)')
ylabel('specific heat [KJ/KMOL-K]')
legend('original data','poly2 for 1-1500' , 'poly2 for 1500-3200 ','location','northwest')
figure
plot(temp01,cp01, 'linewidth',1.5,'color','g');
xlim( [1,3200] );
hold on
plot( pred_cp2 ,'predfun');
hold off
xlabel('temperature (K)')
ylabel('specific heat [KJ/KMOL-K]')
legend('prediction of curve ahead of 1-1500 ','curve fit','predicion', 'Location', 'NorthWest' );
figure
plot(temp001,cp001, 'linewidth',1.5,'color','g' );
xlim( [1500,5000] );
hold on
plot( pred_cp3 , 'predfun');
hold off
xlabel('temperature (K)')
ylabel('specific heat [KJ/KMOL-K]')
legend('prediction of curve ahead of 1500-3200','curve fit','predicion', 'Location', 'NorthWest' );
- In this program , the warning has been solved by using two second order polynomial equations and dividing the data set into two parts . i.e , from the data set of 3200 values of temp & cp , temp01 & cp01 is assigned 1-1500 values and temp001 and cp001 is assigned 1500-3200 values .
- The fit obtained is good for both half of the cases .
- Explanation of code : (only the changes from the previous program explained here)
- The code is seperated in two halves : **cp01 = cp([1:1500]); temp01 = temperature([1:1500]); ** cp001 = cp([1500:3200]); temp001 = temperature([1500:3200]);**
- Temperature and cp are plotted like before
- Fit commad is used and assigned in variable pred_cp2 for first half with gof -(goodness of fit) in an array to get all the fitness characteristics values .
- The pred_cp2 is only uptil the first 1500 values !! and is assigned POLY2 that is polynomial of second order
- Same exact procedure is repeated for pred_cp3 but the values assigned are from 1500-3200 ! with again POLY2 .
- Till first 1500 the poly2 for 1-1500 acts acts then till 3200 second poly2 for 1500-3200 .
- On a more suitable view it looks like for first half:
- Now for second half :
- The figure 2 and 3 are mentioned because its part of the lower program before prediction range values , actual half range values were used to observe the curve fit .
- This data was also plotted on the "Curve fitting tool" and the results obtained are as follows , :
- The goodness of fit is 0.99 > 0.95 and other quantities are obtained also the coefficients of poly 2 are obtained . This is a good fit .
- Now for the second half :
- Again the fit is good
- This way the warning showing in the previous program of cubic polynomial is no more , by seperating the data and using two second order polynomial equations to obtain a good fit .
- Further the half data for both polys are predicted further as to how they will behave .
- Predfun command is used and value for half data is assigned 1-3200 and for second data in the next figure is assigned from 1500 - 5000 .
- Plot appears to be :
- Hence we see the further data prediction from both .
- The Gof used in the fit command produced the same results obtained in the curve fitting tool :
both are good fits .
**Comparing it with the first programs cubic polynomial & quadratic , using two poly2 equations and seperating the data we obtained 0.99 which is near accurate and very good fit and better than rsquare of poly2 equation of the first program **
- In the two half data set created , when wondered what would happen if cubic was used instead of polynomial . well , we got best possible fit . :
with r-square and adjusted r-square as 1 and SSE as 89.
- The parameters used to measure the fitness characteristics are data set , its coefficient values , the order of polynomial equation and other inputs, also :-The 4 quantities
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
- Every quantity has a mathematical method by which it determines the gof.
- Are the quantities which help measure the fitness characteristics . we have identified our predicted curves by SSE , r-square and adjusted r-square .
- Other details are explained above in the explanation of code .
- These are also the criterian used to get the best fit , also at the very last image of curve fitting tool the best possible fit was obtained with rsquare =1 .
- The closer the curve fits to the original data the best the fit is obtained . Various interpolations , polynomials and using subregions of data can be done to achieve it ,while increasing the number of variables etc .
- Making a curve fit perfectly , the definition of curve fit says :"Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data"
- It means curve fitting is used to obtain the best and the closest possible fit as the parameters above and the quantities it utilizes are all approximate methods .
- Its very rare to have an exact perfect curve fit according to the original data , you need the original governing equation by which the data is created or the exact mathematical solution to make the perfect curve fit possible and predict the future but its rare .
- The best fit is practically relevant .
- "perfect fit' when the curve goes through every point. The sum-of-squares is 0.0, and R^2 is 1.00. If you are fitting actual data, and the fit is perfect, you either got very lucky or have very few data points. It is not possible to compute the standard errors and confidence intervals of the parameters when the fit is perfect, nor is it possible to compare models.
- To improve the cubic fit - Call polyfit with 3 outputs to let x be scaled and shifted automatically - ex = [p, S, mu] = polyfit(x, y, n) .
- [p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation
- References :
- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
- https://in.mathworks.com/help/matlab/ref/polyfit.html
- https://en.wikipedia.org/wiki/Curve_fitting
- https://in.mathworks.com/help/curvefit/examples/polynomial-curve-fitting.html
- https://in.mathworks.com/help/curvefit/examples.html?s_cid=doc_ftr
- https://www.youtube.com/watch?v=tl5QNhSe0Yk&t=547s