Curve Fitting in MATLAB
AIM: Write a program in MATLAB to fit a linear and cubic polynomial for the Cp data.
CURVE FITTING
Curve fit us really useful to describe the how the data changes mathematically. In the curve we have benefit of additional mathematical operation which might be not possible with initial data set. It is also useful for the future predications that what the value of data in future. So, we derive the mathematical function with the help of curve fit. Based on the accuracy of curve fit the value of data can be change but we have an idea of the rough trend.
Accuracy of the curve fit is also depending on the order polynomial equation. So, it first finds the coefficients of the polynomial. Example For the linear polynomial equation than there are 2 coefficient a and b.
THE PARAMETERS USED TO MEASURE THE FITNESS CHARACTERISTICS FOR CURVE
There are four quantities to measure the goodness of the fit which are as follows:
- The sum of the squares due to errors (SSE):it is square of error where error the difference between predicted data and actual data.
- R-square: R-square is the square of correlation between the response value and predicated response value square ranges from 0 to 1. Higher value of the R square is indicating the better fit.
- Adjusted R-square: Adjusted R-square is a normalizing variation of R-square. In which neglects the variation due to insignificant independent variable in R-square.
Where,
n= the number of datapoints
k= the number of independent variables.
- Root mean squared error (RMSE): It is an estimate of the standard deviation of the random component in data. It is defined as
Here,
n is total number of datapoints.
STEP-WISE EXPLANATION OF CODE FOR CURVE FIT
- First, we have to load the data in MATLAB.
- We have to assign value of temperature and Cp from the data sheet.
- We find the value of coefficient of polynomial with help of polyfit command.
- Now we have to find the value of predicated Cp with help of coefficient and temperature.
- Now we plot the graph for the Temperature vs Cp and Temperature vs predicated Cp. In the graph we show the difference in value of Cp with help of legend command.
- For finding the value of SSE we have the find mean so we calculate the sum of datapoint of Cp and divide the sum with number of data point.
- We find the value of SSE & SSR with the for loop.
- Then we find the value of SST.
- Last, we find the value of R-square.
PROGRAM FOR THE LINEAR FIT
close all
clear all
clc
%loading of data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%crve fit
cp_effs = polyfit(temperature,cp,1);
predicated_cp = polyval(cp_effs,temperature);
%ploting data
plot(temperature,cp,'linewidth',2,'color','r')
hold on
plot(temperature,predicated_cp,'linewidth',2,'color','b')
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('cp','predicated cp')
title('Cp(Kj/Kmol-K) vs Temperature(K)')
sum =0;
for i= 1:3200 %for the calculate mean of cp first we require to sum of the cp value
sum = sum+ cp_data(i,2);
end
mean=sum/3200; %mean is calculated by ratio of sum of data and no.of data
ssr=0; %sum of squares of regression.
sse=0; %sum of squared errors
for j=1:3200
ssr=ssr+ (predicated_cp(j)-mean)^2;
sse= sse+ (cp_data(j,2)-predicated_cp(j))^2;
end
sst=ssr+sse;
R_square=ssr/sst %it indicates the crve fit is good or not
Value of R-square for linear fit
Output
PROGRAM FOR THE CUBIC FIT
close all
clear all
clc
%loading of data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%crve fit
cp_effs = polyfit(temperature,cp,3);
predicated_cp = polyval(cp_effs,temperature);
%ploting data
plot(temperature,cp,'linewidth',2,'color','r')
hold on
plot(temperature,predicated_cp,'linewidth',2,'color','b')
xlabel('Temperature [K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('cp','predicated cp')
title('Cp(KJ/Kmol-K) vs Temperature(K)')
sum =0;
for i= 1:3200 %for the calculate mean of cp first we require to sum of the cp value
sum = sum+ cp_data(i,2);
end
mean=sum/3200; %mean is calculated by ratio of sum of data and no.of data
ssr=0; %sum of squares of regression.
sse=0; %sum of squared errors
for j=1:3200
ssr=ssr+ (predicated_cp(j)-mean)^2;
sse= sse+ (cp_data(j,2)-predicated_cp(j))^2;
end
sst=ssr+sse;
R_square=ssr/sst %it indicates the crve fit is good or not
Value of R-square for Cubic fit
CURVE FIT TOOLBOX
Curve fit toolbox is inbuild app in the MATLAB. In this app we have to give some input data and it generate the curve fit automatically and it is also giving the value of SSE, R-square, Adjusted R-square and RMSE.
For our data, here the is screenshot of linear and cubic fit of curve fit toolbox respectively
Linear fit
Cubic fit
I find that the value of R-square is 1 for the 8th order of polynomial. Here is screenshot of 8th order of polynomial curve fit
Perfect fit
How to make a curve fit perfectly?
Perfect fit is the fit in which value of R-square is 1. So, this is only possible when actual curve and predicated curve meet at all points. As I say above that I found that R-square value is 1 for the 8th order of polynomial. So, by increasing the order of polynomial we make the curve perfectly fit.
How to get the best fit?
As my research I found that we can use the Lagrange polynomial for the best fit because when we use this than curve passing through the all the points but it yields a polynomial oscillating above and below the true function. This behavior tends to grow with the number of points. So, we can get the best fit.
REFERENCES
1. https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860