Project

Curve Fitting and Factors for Best Fit

Curve Fit and Its Different Methods

Aim:- To plot curve fit for a equation and finding out method for best fit.

Theory:-

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 inference 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. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.

Sum of Squares Due to Error

This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labelled as SSE.

SSE = Sum(i=1 to n){wi (yi - fi)2}

Here yi is the observed data value and fi is the predicted value from the fit. wi is the weighting applied to each data point, usually wi = 1.

A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction.

R-Square

This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

R-square is defined as

R-square = 1 - [Sum(i=1 to n){wi (yi - fi)2}] /[Sum(i=1 to n){wi (yi - yav)2}] = 1 - SSE/SST

Here fi is the predicted value from the fit, yav is the mean of the observed data yi is the observed data value. wi is the weighting applied to each data point, usually wi=1. SSE is the sum of squares due to error and SST is the total sum of squares.

R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model. For example, an R-square value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average.

If you increase the number of fitted coefficients in your model, R-square will increase although the fit may not improve in a practical sense. To avoid this situation, you should use the degrees of freedom adjusted R-square statistic described below.

Note that it is possible to get a negative R-square for equations that do not contain a constant term. Because R-square is defined as the proportion of variance explained by the fit, if the fit is actually worse than just fitting a horizontal line then R-square is negative. In this case, R-square cannot be interpreted as the square of a correlation. Such situations indicate that a constant term should be added to the model.

Degrees of Freedom Adjusted R-Square

This statistic uses the R-square statistic defined above, and adjusts it based on the residual degrees of freedom. The residual degrees of freedom is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.

v = n-m

v indicates the number of independent pieces of information involving the n data points that are required to calculate the sum of squares. Note that if parameters are bounded and one or more of the estimates are at their bounds, then those estimates are regarded as fixed. The degrees of freedom is increased by the number of such parameters.

The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested – that is, a series of models each of which adds additional coefficients to the previous model.

adjusted R-square = 1 - SSE(n-1)/SST(v)

The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.

Root Mean Squared Error

This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as

RMSE = s = (MSE)½

where MSE is the mean square error or the residual mean square

MSE=SSE/v

Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.

Input:-

clear all
close all
clc
% Preparing Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);

% Curve Fitting

co_effs=polyfit(temperature,cp,2);
predicted_cp=polyval(co_effs,temperature);

% Curve Fitting

co_effs=polyfit(temperature,cp,2);
predicted_cp=polyval(co_effs,temperature);

ssr=0;
sse=0;
mean_cp=mean(cp);
for i=1:length(cp)
    a=(predicted_cp(i)-mean_cp)^2;
    ssr=ssr+a;
end

for j=1:length(cp)
    b=(cp(j)-predicted_cp(j))^2;
    sse=sse+b;
end

sst=ssr+sse
R=ssr/sst;

rmse=(sse/length(cp))^0.5

t=table(temperature,cp,predicted_cp,cp-predicted_cp,'VariableNames',{'temperature','cp','cpfit','fit_error'});   % Table containing values from data file
Mean=mean(t.cp);                             % Mean of specific heat
t.fit_square=(t.fit_error.^2);
t.cp_mean=(t.cp-Mean);
t.cp_square_mean=(t.cp_mean.^2);
summation_fitsquare=sum(t.fit_square);
summation_cp_square_mean=sum(t.cp_square_mean);
t(1:5,:);
corelation_coeff=((summation_cp_square_mean-summation_fitsquare)/summation_cp_square_mean)^1/2;

% Comparing Curve fit with Orginal Data by Plotting
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
plot(temperature,cp,'linewidth',3,'color','b')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Dataset','Curve Fit')

Explaination:-

Information for plotting curve is taken by loading 'data' file

Number of columns and rows for values is specifies

Curvefit functions like polyfit,polyval is used for plotting having equation of specific order

Error are find out using formulae

Table is formed containing different values of file

Curve is plotted of temperature Vs Specific Heat

Output:-

Graph with order of equation 1 (Linear)

Graph with order of equation 2 (Cubic)

Graph with order of equation 3 (Cubic)

Error faced:-

Error Rectified By,

Using t(1:5,:); Instead of t=(1:5,:);

Conclusion:-

It can be concluded that as the order of equation increases curve fitting also increases making RMSE value and Error value change.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.