Project

Curve Fitting using MATLAB

AIM : MATLAB program to perform Curve Fitting using given input data.

The program should achieve the following objectives:

  1. Coding to fit a linear and cubic polynomial for the given Temperature v/s Cp data.
  2. Plotting the linear and cubic fit curves along with the raw data points.
  3. Further perform curve fitness analysis on both the curves.

BACKGROUND : Curve fitting is the way we model or represent a data spread by assigning a ‘best fit‘ function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.

It is process of manipulating the free variables to try and make the equations reproduce the known curve as closely as possible.
If you know the underlying equations are correct, then this may be quite a reasonable way of identifying the value of the free variables. If the underlying equations are not correct, it will produce a solution which apparently fits the data quite well, but is completely bogus. For this reason, it is only valid in situations where you know the equations are correct. Often, but not always, this is the case.
Because "curve fitting" can make totally incorrect sets of equations superficially look correct, it is often used as a pejorative term indicating the underlying equations are wrong. Incorrect use of curve fitting can be detected by checking the validity of solutions which are outside the range of data used to curve fit - the solutions don't extrapolate properly.

Now, with respect to Curve Fitting, the following questions are generic and need to be answered while performing the curve fitting analysis.

  1. How to make a curve fit perfectly?

Upon finding the curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equations are representing the data points. These are –

  • Sum of Squares due to Error(SSE) : 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)^n{w_i(y_i=f_i)^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)^n{w_i(y_i-f_i)^2}]}/{[sum_(i=1)^n{w_i(y_i-y_a)^2}}`

`R-Square= 1-frac{SSE}{SST}`

Here fi is the predicted value from the fit, ya 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.

  • Degree 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 fit 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-frac{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)^frac{1}{2}`

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

`MSE=frac{SSE}{v}`

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

2. How to get the best fit?

Regression analysis is generally performed to find the best fit for a given set of data points. Regression is the process of analyzing the relationship between different variables and building a mathematical model that can be used to predict the value of a variable(label) based on the values of remaining variables(features).

Dependent variable: It is the variable to be predicted, also known as label and usually represented by Y.

Independent variable: It is the predictor or explanatory variable, also known as feature and usually represented by X.

Linear regression is a form of regression analysis in which the relationship between the independent variable X and the dependent variable Y is modeled as a 1st degree polynomial in X i.e. the relationship between Y and X is modeled by a straight line.

Simple linear regression is a model with single dependent variable and single independent variable.

(X1, Y1), (X2, Y2), …….., (Xn, Yn).

SOLUTION ALGORITHM

  1. We will begin by reading the data for Temperature v/s Specific Heat from an external text file.
  2. The temperature and the specific heat will be stored in separate variables and a curve will be plotted with them which will be actual curve.
  3. Now, first we will assume a linear function to represent the data and with the help of polyfit command we will generate the coefficients of the linear function.
  4. Then using polyval command we will generate predicted values of specific heat form the linear function.
  5. After this, we will compare the curve generated with the predicted values with the actual curve to determine whether the curve fits perfectly.
  6. Steps from 2 to 5 will then be repeated for an assumed cubic function.
  7. Then we will perform fitness characteristic for both the curves(linear and cubic) using the 4 criteria mentioned above.
  8. Finally we will compare the results.
  9. The code for the entire program will be written in MATLAB.

MATLAB CODE :

%matlab program to perform curve fitting on a given data set
%the data set is of c_p variation with temperature for O2

%reading the data
actual_data= load('data');
actual_temp= actual_data(:,1);
actual_cp= actual_data(:,2);

%plotting the actual curve
figure(1)
plot(actual_temp,actual_cp,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Cp(KJ/Kmol-K)')

%performing the curve fitting

%assuming a linear function to represent the data
coefficients1= polyfit(actual_temp,actual_cp,1);

%predicting the solution
predicted_cp1= polyval(coefficients1,actual_temp);

%comparing the curve obtained after curve fitting with actual curve
%checking if the curve of linear function fits with the actual curve
figure(2)
plot(actual_temp,predicted_cp1,'linewidth',3,'color','b')
hold on
plot(actual_temp,actual_cp,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Cp(KJ/Kmol-K)')
legend('predicted','actual')


%assuming a cubic function to represent the data
coefficients2= polyfit(actual_temp,actual_cp,3);

%predicting the solution
predicted_cp2= polyval(coefficients2,actual_temp);

%comparing the curve obtained after curve fitting with actual curve
%checking if the curve of cubic function fits with the actual curve
figure(3)
plot(actual_temp,predicted_cp2,'linewidth',3,'color','g')
hold on
plot(actual_temp,actual_cp,'linewidth',3,'color','r')
xlabel('temperature(K)')
ylabel('Cp(KJ/Kmol-K)')
legend('predicted c','actual')

%plotting actual curve, curve with linear variation and curve with cubic
%variation
figure(4)
hold on
plot(actual_temp,actual_cp,'linewidth',3,'color','r')
plot(actual_temp,predicted_cp1,'linewidth',3,'color','b')
plot(actual_temp,predicted_cp2,'linewidth',3,'color','g')
xlabel('temperature(K)')
ylabel('Cp(KJ/Kmol-K)')
legend('actual','predicted linear','predicted cubic')


%evaluating the fitness characteristic of both the curves

%for linear relationship
%sum of squared errors(SSE)
SSE1=0;
for i= 1:length(actual_data(:,1))
    SSE1= SSE1 + (actual_cp(i)-predicted_cp1(i))^2;
end

%R-square criterion
sum1=0;
for i=1:length(actual_cp)
sum1= sum1 + actual_cp(i);
end
mean_data1= sum1/length(actual_cp);
SSR1=0;
for i=1:length(actual_cp)
    temp1= (predicted_cp1(i)-mean_data1)^2;
    SSR1= SSR1 + temp1;
end
SST1= SSR1+SSE1;
R_square1= SSR1/SST1

%root mean squared error criterion
RMSE1= sqrt(SSE1/3200)

%for cubic relationship
%sum of squared errors(SSE)
SSE2=0;
for i= 1:length(actual_data(:,1))
    SSE2= SSE2 + (actual_cp(i)-predicted_cp2(i))^2;
end

%R-square criterion
SSR2=0;
for i=1:length(actual_cp)
    temp2= (predicted_cp2(i)-mean_data1)^2;
    SSR2= SSR2 + temp2;
end
SST2= SSR2+SSE2;
R_square2= SSR2/SST2

%root mean squared error criterion
RMSE2= sqrt(SSE2/3200)

OUTPUT

The following output is obtained after running the code :

* the plot is for actual data set.

* the plot is for actual data v/s Linear function.

* the plot is actual data v/s cubic function.

* the plot compares all the 3 curves(actual,linear,cubic).

R-square and RMSE are also calculated for linear and cubic functions which are obtained in the output.

ERRORS :

1. The error occured because "sum" was used as a variable name. It was removed by changing the name of the variable.

* Apart from this some typing errors were made which were easily removed.

CONCLUSION

  • From the output, it is clear that the curve for cubic function fits more perfectly as compared to the linear function.
  • It hence can be generalized for the given data set that as we increase the order of the function representing the given data set, the accuracy is enhanced. However, it does not necessarily imply for all the other data sets.
  • The accuracy of the cubic function can be further enhanced by using several other techniques such as performing split curve fitting.
  • R-square value above 0.95 is considered as a characteristic of a good curve. From the output, it is clear that that the cubic curve is a good fit having a value of R-square as 0.9967.

REFERENCES

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.