Curve fitting
- AIM
- Write code to fit a linear and cubic polynomial for the Cp data.
- OBJECTIVE
- Write code to fit a linear and cubic polynomial for the Cp data.
- To understand various techniques to improve curve fitting.
- BODY OF THE CONTENT
- Definition of curve fitting:- Curve fitting is the process of constructing a curve, or mathematical function which posses the closest approximation to real series of data.
- In this context, we have series of data of temperature and cp, and we need to fit curve to represent the data graphically.
- For this we use polyfit & polyval command in ‘Matlab’ For linear fit of curve.
- First we start with the load the data from given file.
Linear Fit:-
clc close all clear all %Loading given data file cp_data=load('data'); - Now we have to find mathematical relation between these two variable by constructing the polynomial. By using “POLYFIT” command.
-
%Assign given data Variable temp=cp_data(:,1); cp=cp_data(:,2); %finding the Polynimial, [cp_coeff]=polyfit(temp,cp,1) [predicted_cp]=polyval(cp_coeff,temp); - In first line we, we are finding coefficient of 1st degree polynomial & in 2nd line we are evaluating that polynomial to values of temperature.
- As we can see we are getting two values of coefficient for equation ,y=Ax+b,">y=Ax+b,y=Ax+b,as we derived 1st order polynomial.
-
Linear Curve Fit
-
- In successive part of coding we are plotting both curves that is actual curve for point that we have (temp Vs cp), and curve which we derived from the mathematical relation.(temp Vs Predicted_cp)
- As we can see this curve is not correctly fit to the given data point, hence we have to find a way fit the curve to given data points perfectly. This can be achieve by increasing the order of polynomial or using split wise polynomial fitting
Cubic Curve Fit:-
- As part for improving fitness of curve we are going to increase the order of polynomial. Here we are using cubic polynomial.
clc close all clear all %Loading given data file cp_data=load('data'); %Assign given data Variable temp=cp_data(:,1); cp=cp_data(:,2); %finding the Polynimial, [cp_coeff]=polyfit(temp,cp,3) [predicted_cp]=polyval(cp_coeff,temp); %Ploting Curve Fit plot(temp,cp,'*') hold on plot(temp,predicted_cp,'color','r','linewidth',2.5) xlabel('Temperature [K]') ylabel('Specific Heat [KJ/Kmol-K]') legend('Actual cuve', 'predicted curve','location','northwest')
Output Curve:-
- As we can see syntax of 3rd order polynomial is same as 2nd order polyminal, the only difference is in polyfit command, where we define order of polynomial.
- Also we get the better curve fitting but also we getting following error.
- 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 centering and scaling.
- Now in 'polyfit' command while calculating the coefficients we will also calculate error estimation structure (s) and centering and scaling values (mu).
Where S having three values
1)R=Triangular factor from a QR decomposition of the Vandermonde matrix of x
2)df=Degrees of freedom
3)normr=Norm of the residuals
- 'mu' contains centering and scaling values and is a two element vector.
(Source:- Matlab Help on Polyfit)
- We can pass these veriable along with “polyfit” command to remove this error.
- Syntax after adding these coefficient:-
clc
close all
clear all
%Loading given data file
cp_data=load('data');
%Assign given data Variable
temp=cp_data(:,1);
cp=cp_data(:,2);
%finding the Polynimial,
[cp_coeff,s,mu]=polyfit(temp,cp,3)
[predicted_cp]=polyval(cp_coeff,temp,s,mu);
%Ploting Curve Fit
plot(temp,cp,'*')
hold on
plot(temp,predicted_cp,'color','r','linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Actual cuve', 'predicted curve','location','northwest')
Goodness of curve fitting:-
To find the goodness of curve fitting we have following parameters which we are going to calculate.
- The sum of squares due to error.(SSE)
- R-square
- Root mean squared error
- The sum of squares due to error.(SSE):- This can be define by sum of squared of all error of polynomial.
SSE= ∑(error)2">∑(error)2∑(error)2
Where Error=(Actual value of Y(i) – Predicted value of Y(i))
As Error can be neagative or positive we are taking square and then summation.
Generally SSE closer to 0 is preffered.
- R-square:- In general term R-squared is the closeness of curve to the mean of original values. Hence as R-squared closes to one better the curve fitting.
- R-squared=0.496 means only 49.6% values of original data set are covered by predicted curve.
R-squared=
- Perfect fit of curve comes when curve passes through all input values, but it is not significant to do this as if we have large no. of input value, then we have to increase degree of polynomial to perfect fit but it causes oscillation of curve near the points.
- Hence we have to choose best fit of curve, which have less order of polynomial also having low error, we can consider best fit by using parameters of “Goodness of curve fitting”.
Generally the value of R-squared more the 0.95
Syntax for calculating Goodness of curve:-
%Calculating goodness of curve
format longG
%1) Calculating Sum of Squares of residue(SSR)
dev=predicted_cp-mean(cp);
SSR=sum(dev.^2)
%2) Calculating sum of squar of Error (SSE)
error=cp - predicted_cp;
SSE=sum(error.^2)
%3) Calculating Root mean squared error
rmse=sqrt(SSE/length(cp))
%4) Calculating R-squared
SST=SSE+SSR
Rsq=SSR/SST
The value of R-squared we are getting is “0.996725946714054”
We can see that by using Centering & scalling of data and calculating goodness of curve, cubic fit is best fit for this data.
But for improving goodness of curve there is another technique i.e “split wise polynomial fitting”
Complete Syntax for Curve fitting :-
clc
close all
clear all
%Loading given data
cp_data=load('data');
%Assign given cariable to data
temperature=cp_data(:,1);
cpdata=cp_data(:,2);
a=1;
b=100;
plot(temperature,cpdata,'*')
hold on
%Using For loop for splitting data,
for i=1:32
temp=temperature(a:b,1);
cp=cpdata(a:b,1);
%find the coefficients,
[cp_coeff,s,mu]=polyfit(temp,cp,3);
predicted_cp=polyval(cp_coeff,temp,s,mu);
predictedcp1(a:b,1)=predicted_cp;
%curve fit
plot(temp,predicted_cp,'color','g','linewidth',2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Actual cuve', 'predicted curve','location','northwest')
a=a+99;
b=b+100;
end
%Calculating goodness of curve
format longG
%1) Calculating Sum of Squares of residue(SSR)
dev=predicted_cp-mean(cp);
SSR=sum(dev.^2)
%2) Calculating sum of squar of Error (SSE)
error=cp - predicted_cp;
SSE=sum(error.^2)
%3) Calculating Root mean squared error
rmse=sqrt(SSE/length(cp))
%4) Calculating R-squared
SST=SSE+SSR
Rsq=SSR/SST
Output graph:-
As we can clearly see, the goodness of graph fitting is increase drastically, when we use splitting the data.
Also we are getting R-squared value is “0.99999999997948” , which is very close to 1. hence this is best curve fit for given data.
Errors observed during Coding:-
- Polynomial is badly conditioned.- This problem is observed during cubic fit of curve, and solved by using centering & scalling of data.
- Matrix dimension must agree:- This error occur when I’m using “for loop” for splitting of data, due to this the matrix dimension of given cp_data & Predicted cp_data mismatch because in per execution of loop, predicted cp_data is getting overwrite.
To solve this I need to use dummy operator which save value of predicted_cp at each iteration and make the matrix as same dimension as cp_data.
4. CONCLUSION
- From observation of output graph, we conclude that errors in.
- Curve fitting can be improved by increasing the order of polynomial, but it is not feasible always, hence we have to find best fit curve.
- If we have more number of inputs data points, then we need to use centering & scaling of data to minimize the error in input data points.
- Best fit of curve to given data can be define by goodness of curve fitting.
- The best way to fit the curve in given data by splitting the data in different range, and then fit the polynomial in it, this can reduce the error and we can get best fit curve at lower order of polynomial.
Referances:-
- 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