CURVE FITTING
AIM : To write a Matlab program code to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.
( Using Octave 4.0.0 GUI )
OBJECTIVE OF THE PROJECT:
Our main objective is to fit a linear and cubic polynomial curve for the provided data and plot the curves also to check the fitness characteristics of both the curves.
ABOUT CURVE FITTING :
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing in which a smooth function is constructed that approximately fits the data. A related topic is regression analysis, which focuses more on questions of statistical interfernce such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables.
FITNESS OF THE CURVE BY R SQUARE METHOD :
It is a statistics used in the context of statistical models whose main purpose is either the prediction of future outcomes or the testing of hypothesis, on the basis of other related information. It provides a measure of how well observed outcomes are replicated by the model, based on the proportion of total variation of outcomes explained by the model.
In other words it means that the higher the value of R-square the better the fit, the lower the value of R-square lower is 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.
R- square value is given by :
`R^2=(SSR)/(SST)`
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.
`SST = SSE +SSR`
If `y=f(x)`is the fit of the curve then SSR is defined as:
`sum_(i=1)^n(f(x(i))-Mean)^2`
Similarly
SSE is defined as :
`sum_(i=1)^n(Y(i)-f(x(i)))^2`
With the help of this we can calculate:
`R^2=(SSR)/(SST)`
CODES:
clear all
close all
clc
%preparing the data
cp_data = load ('data');
temperature = cp_data(:,1);
cp=cp_data(:,2);
%curve fit
%cp = a*T^2 + b*T + C
%For linear curve fit
co_efficient_linear = polyfit(temperature,cp,1)
predicted_cp_linear = polyval(co_efficient_linear,temperature)
%For cubic curve fit
co_efficient_cubic = polyfit(temperature,cp,3)
predicted_cp_cubic = polyval(co_efficient_cubic,temperature)
%comparing with original dataset
%Plot
figure(1)
hold on
plot(temperature,cp,'linewidth',3.5)
plot (temperature,predicted_cp_linear,'linewidth',3.5,'color','r')
xlabel('Temperature(k)')
ylabel('Specific Heat(kl/kmol-k)')
legend('original dataset','curve fit')
title( 'Linear variation of specific heat with temperature')
figure(2)
hold on
plot(temperature,cp,'linewidth',3.5)
plot (temperature,predicted_cp_cubic,'linewidth',3.5,'color','g')
title( 'Cubic variation of specific heat with temperature')
xlabel('Temperature(k)')
ylabel('Specific Heat(kl/kmol-k)')
legend('original dataset','curve fit')
%checking the fitness of the curve by R_square method
M=mean(cp);
%For linear fit
%Sum of sqaures of regression
SSR_linear =0;
%Sum of sqaures of error
SSE_linear =0;
for i=1:length(cp)
SSR_linear=SSR_linear+(predicted_cp_linear(i)-M)^2;
SSE_linear=SSE_linear+(cp(i)-predicted_cp_linear(i))^2;
end
% Sum of Square of total(SST)
SST_linear = SSE_linear+SSR_linear;
% Calculation of R_square
R_square_linear = SSR_linear/SST_linear;
fprintf( 'R_square for linear fit is %f\n',R_square_linear);
%For cubic fit
%Sum of sqaures of regression
SSR_cubic =0;
%Sum of sqaures of error
SSE_cubic =0;
for i=1:length(cp)
SSR_cubic=SSR_cubic+(predicted_cp_cubic(i)-M)^2;
SSE_cubic=SSE_cubic+(cp(i)-predicted_cp_cubic(i))^2;
end
% Sum of Square of total(SST)
SST_cubic = SSE_cubic+SSR_cubic;
% Calculation of R_square
R_square_cubic = SSR_cubic/SST_cubic;
fprintf( 'R_square for cubic fit is %f',R_square_cubic);
OUTPUT:
FITNESS CHARACTERISTIC BY R-SQUARE METHOD :
ERRORS :
Getting all the plot in same figure and wrong plot because I didn't keptt them in seperate figure.
QUESTIONS:
1. How to make a curve fit perfectly?
Ans. For a perfect fit, we must consider error estimates as well.Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling erros) in that perticular degree of polynomial.perfect fit can always be a best fit but best fit can not be a perfect fit.
2. How to get the best fit?
Ans. The most common way to fit curve to the data using linear regression is to include polynomial terms, such as squared or cubed predictors. Typically, you choose the model order by the number of bends you need in your line. Each increase in the exponent produces one more bend in the curve fitted line.
What could be done to improve the cubic fit?
Ans. As mentioned above split-wise curve fitting can be used to improve the cubic fit. By taking the values of Cp data in four different splits (splits can be of any number), and do the same as we did for the normal method i.e by using polyfit and polyval commands to calculate the new Cp value and plot it. So, The R^2 value will be increased and the curve fit will be improved.
CONCLUSION :
- From this program we have done the curve fitting for linear and cubic polynomial.
- We have plot a graph of linear and cubic fit curves with raw data .
- We have calculated the goodness of the fit using R-square method.
- R_square for linear fit is 0,924878
- R_square for cubic fit is 0.996726.
References:
blog.minitab.com/blog/adventures-in-statistics-2/curve-fitting-with-linear-and-nonlinear-regression