CURVE FITTING USING MATLAB
1.AIM :
PROGRAM TO FIT LINEAR AND CUBIC POLYNOMIAL FOR THE GIVEN SPECIFIC HEAT(Cp) DATA
2.THEORY AND GOVERNING EQUATIONS :
Curve fitting
Curve fitting is the process of constructing a curve which very closely resembles the variation of mathematical function of the used set of data points. By curve fitting one can easily construct a mathematical relation between dependent and independent variable.
Curve fitting using MATLAB
Calculating the predicted specific heat(Cp) using the ployfit() and polyval() function and by assuming cp to be a quadratic function of temperature, cp=a*T^2 + b*T + c and plotting the Specific heat(Cp) v/s Temperature curve gives
MATLAB program for the above is
clear all
close all
clc
cp_data=load('data');%Extract values from the file 'data' and store in variable 'cp_data'
%extracting temperature data (column 1) from the variable 'cp_data'
temperature=cp_data(:,1);
%extracting Specific heat(Cp) data (column 2) from the variable 'cp_data'
cp=cp_data(:,2);
%curve fit
% assuming cp to be a quadratic function of temperature, cp=a*T^2 + b*T + c
%using inbuilt function for curve fitting
co_effs = polyfit(temperature,cp,2); %Extracts the value of coeefcients of the function of cp and store in variable co_effs
predicted_cp = polyval(co_effs,temperature); %predict the value of cp and store in variable predicted_cp
%plotting Specific heat(Cp) v/s Temperature curve
%2 curves are used to compare curve fit with the original data
plot(temperature,cp,'linewidth',3);
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r');
xlabel('Temperature (kelvin)');
ylabel('Specific Heat (KJ/Kmol-K)');
legend('original dataset','curve fit','location','northwest');
The resulted plot is given below:
As seen from the plot the original data set curve and the curve found using the predicted values does not allign with each other.
How to improve the fit of curve
So to find a curve which closely resembles the curve of the original database we have to eliminate the error between the original dataset points and predicted dataset points.
Assume the original data points to be (x1,y1) , (x2,y2) , (x3,y3) , ... (xn,yn) and assume the mathematical function for best to be y = f(x). The error at individual point will be difference in y co-ordinate values.
`"Error" = | y ( i ) − "value of f(x) at x(i)" | = | y ( i ) − f ( x ( i ) ) |`
There are basically two methods to minimize this error and get the best fit.
- Increase the degree of the polynomial
- Using the split-wise method
To evaluate a fit the factor R-square will help us, it will measure the goodness of fit and check how well the equation satisfies the set of data points.
R-square:- This quantity measures how closely the fit resembles the variation of the data, it is the correlation between the dataset values and the predicted values.
R-square ranges from a value of 0 to 1, higher the value of R-square the better is the fit.
`R^2 = (S S R )/(S S T)`
where
1) `S S T = S S R + S S E`
2) SSE is Sum of squared errors
To remove the modulus sign we square the error term and if we add the errors for all the points we get
`SSE = sum_(i=0)^n [ y ( i ) − f ( x (i )) ] ^2`
3) SSR is sum of squares of the regression
It is the square of the difference between the value of the assumed function at a given point and the arithmetic mean of the given dataset.
`S S R = sum_(i=0)^n [(f ( x ( i ) ) − M e a n ]^ 2`
3.OBJECTIVES :
- Write a program to curve fit for a linear and cubic polynomial for the given Cp data.
- Plot the linear and cubic curve fit along with original dataset points.
- To evaluate the curve fit.
4.MATLAB PROGRAM AND RESULTS :
1) If we use the first method and increase degree of the polynomial of predicted specific heat(Cp) as a function of temperature.
a) Code to fit a linear polynomial for the Cp data
clear all
close all
clc
cp_data=load('data');%Extract values from the file 'data' and store in variable 'cp_data'
%extracting temperature data (column 1) from the variable 'cp_data'
temperature=cp_data(:,1);
%extracting Specific heat(Cp) data (column 2) from the variable 'cp_data'
cp=cp_data(:,2);
%curve fit
%assuming cp to be a linear function of temperature,cp=a*T + b
%using inbuilt function for curve fitting
co_effs = polyfit(temperature,cp,1); %Extracts the value of coeefcients of the function of cp and store in variable co_effs
predicted_cp = polyval(co_effs,temperature); %predict the value of cp and store in variable predicted_cp
%plotting Specific heat(Cp) v/s Temperature curve
%2 curves are used to compare curve fit with the original data
plot(temperature,cp,'linewidth',3);
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r');
xlabel('Temperature (kelvin)');
ylabel('Specific Heat (KJ/Kmol-K)');
legend('original dataset','curve fit','location','northwest');
title('linear curve fit')
%calcualtion for the evaluation of fit
sum=0;
for i=1:length(cp_data(:,2))%loop will start with first value till the end value of dataset
sum=sum+cp_data(i,2);%for the sum
end
mean=sum/length(cp_data(:,2));% calculating mean
SSR=0;
for j=1:length(cp_data(:,2))%loop will start with first value till the end value of dataset
SSR=SSR+(predicted_cp(j)-mean)^2;%calculating SSR
end
SSE=0;
for k=1:3200%loop will start with first value till the end value of dataset
SSE=SSE+(cp_data(k,2)-predicted_cp(k))^2;%calculating SSE
end
SST=SSR+SSE;%calculation of SST
R_square=SSR/SST%calculating the value of R^2
output window:-
plot:-
The value of R-square comes out to be 0.9249, this value can be improved by increasing the degree of polynomial
b) Code to fit a cubic polynomial for the Cp data.
clear all
close all
clc
cp_data=load('data');%Extract values from the file 'data' and store in variable 'cp_data'
%extracting temperature data (column 1) from the variable 'cp_data'
temperature=cp_data(:,1);
%extracting Specific heat(Cp) data (column 2) from the variable 'cp_data'
cp=cp_data(:,2);
%curve fit
%assuming cp to be a cubic function of temperature,cp=a*T^3 + b*T^2 + c*T +d
%using inbuilt function for curve fitting
co_effs = polyfit(temperature,cp,3); %Extracts the value of coeefcients of the function of cp and store in variable co_effs
predicted_cp = polyval(co_effs,temperature); %predict the value of cp and store in variable predicted_cp
%plotting Specific heat(Cp) v/s Temperature curve
%2 curves are used to compare curve fit with the original data
plot(temperature,cp,'linewidth',3);
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r');
xlabel('Temperature (kelvin)');
ylabel('Specific Heat (KJ/Kmol-K)');
legend('original dataset','curve fit','location','northwest');
title('cubic curve fit')
%calcualtion for the evaluation of fit
sum=0;
for i=1:length(cp_data(:,2))%loop will start with first value till the end value of dataset
sum=sum+cp_data(i,2);%for the sum
end
mean=sum/length(cp_data(:,2));% calculating mean
SSR=0;
for j=1:length(cp_data(:,2))%loop will start with first value till the end value of dataset
SSR=SSR+(predicted_cp(j)-mean)^2;%calculating SSR
end
SSE=0;
for k=1:3200%loop will start with first value till the end value of dataset
SSE=SSE+(cp_data(k,2)-predicted_cp(k))^2;%calculating SSE
end
SST=SSR+SSE;%calculation of SST
R_square=SSR/SST%calculating the value of R^2
output window:-
plot:-
We get a curve which closely resembles the curve of the original dataset.
The value of R-square comes out to be 0.9967 which implies that we got a better curve fit by increase the degree of polynomial
2) If we use the split wise method
In which we split the data into segments and do the curve fitting for each segment.
a) Code to fit a linear polynomial for the Cp data
clear all
close all
clc
cp_data=load('data');%Extract values from the file 'data' and store in variable 'cp_data'
%assign given data
%extracting temperature data (column 1) from the variable 'cp_data' in
%segments
%extracting Specific heat(Cp) data (column 2) from the variable 'cp_data'
%in segments
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);
%curve fit
%assuming cp to be a linear function of temperature,cp=a*T + b
%using inbuilt function for curve fitting
co_effs1 = polyfit(temp1,cp1,1); %Extracts the value of coeefcients of the function of cp and store in variable co_effs1
co_effs2 = polyfit(temp2,cp2,1);
co_effs3 = polyfit(temp3,cp3,1);
co_effs4 = polyfit(temp4,cp4,1);
predicted_cp1 = polyval(co_effs1,temp1); %predict the value of cp and store in variable predicted_cp
predicted_cp2 = polyval(co_effs2,temp2);
predicted_cp3 = polyval(co_effs3,temp3);
predicted_cp4 = polyval(co_effs4,temp4);
%plotting Specific heat(Cp) v/s Temperature curve
%2 curves are used to compare curve fit with the original data
plot(temp,cp,'linewidth',3);
hold on
plot(temp1,predicted_cp1,'linewidth',3,'color','r');
plot(temp2,predicted_cp2,'linewidth',3,'color','r');
plot(temp3,predicted_cp3,'linewidth',3,'color','r');
plot(temp4,predicted_cp4,'linewidth',3,'color','r');
xlabel('Temperature (kelvin)');
ylabel('Specific Heat (KJ/Kmol-K)');
legend('original dataset','curve fit','location','northwest');
title('linear curve fit')
plot:-
b) Code to fit a cubic polynomial for the Cp data.
clear all
close all
clc
cp_data=load('data');%Extract values from the file 'data' and store in variable 'cp_data'
%assign given data
%extracting temperature data (column 1) from the variable 'cp_data' in
%segments
%extracting Specific heat(Cp) data (column 2) from the variable 'cp_data'
%in segments
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);
%curve fit
%assuming cp to be a cubic function of temperature,cp=a*T^3 + b*T^2 + c*T +d
%using inbuilt function for curve fitting
co_effs1 = polyfit(temp1,cp1,3); %Extracts the value of coeefcients of the function of cp and store in variable co_effs1
co_effs2 = polyfit(temp2,cp2,3);
co_effs3 = polyfit(temp3,cp3,3);
co_effs4 = polyfit(temp4,cp4,3);
predicted_cp1 = polyval(co_effs1,temp1); %predict the value of cp and store in variable predicted_cp
predicted_cp2 = polyval(co_effs2,temp2);
predicted_cp3 = polyval(co_effs3,temp3);
predicted_cp4 = polyval(co_effs4,temp4);
%plotting Specific heat(Cp) v/s Temperature curve
%2 curves are used to compare curve fit with the original data
plot(temp,cp,'linewidth',3);
hold on
plot(temp1,predicted_cp1,'linewidth',3,'color','r');
plot(temp2,predicted_cp2,'linewidth',3,'color','r');
plot(temp3,predicted_cp3,'linewidth',3,'color','r');
plot(temp4,predicted_cp4,'linewidth',3,'color','r');
xlabel('Temperature (kelvin)');
ylabel('Specific Heat (KJ/Kmol-K)');
legend('original dataset','curve fit','location','northwest');
title('cubic curve fit')
plot:-
As we can see the cubic curve fit resembles more to the original dataset curve than the linear cubic curve
Errors faced:-
1)While using the load() command to load the data the single quotes where not written while mentioning the file name
2)While using the polyfit() command the degree of polynomial was not written in the inout to function
5.CONCLUSION :
- As we increase the degree of the polynomial, the curve fit becomes better.
- splitting the data points in segments and increasing the degree of the polynomial gives a better fit.
6.REFERENCES :
https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860