Curve Fitting Using Matlab
Aim:- To produce the curve fit using MATLAB and to determine the best fit to predict the data
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 approxiimately fits the data.
After creating a fit, we can apply a variety of post processing methods for plotting, interpolation and extrapolation;estimating confidence intervals;and calculating integrals and derivatives.
There are many types of regression analysis to fit the data. Here we are using the polynomial regression method. The general formula for polynomial regression for a function of type y=f(x)
1. Linear Polynomial:- `y = ax + b`
2. Quadratic Polynomial:- `y = ax^2 + bx +c`
3. Cubic Polynomial:- `y = ax^3 + bx^2 + cx +d`
Choosing the best fit
Upon finding a curve which satisfies the data points there are 4 quantities which help us in measuring the goodness of fit. These are
- The sum of squares due to error(SSE)
- R-square
- Adjusted R-square
- Root mean squared error(RMSE)
`text(Error) = |y(i) - f(x(i))|`
`text(SSE) = sum_(i=1)^ntext(Error)(i)^2`
`R^2 = (SSR)/(SST), text(where, )`
`SSR = sum_(i=1)^n(f(x(i)) - Mean)^2`
`text(SST )= SSR +SSE`
`text(RMSE ) = sqrt((SSE)/n) text( ,where n = total no of datapoints available)`
Code
clear all
close all
clc
%Loading the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fit
% cp = a*T + b 1st degree polynomial
% cp = a*T^2 + b*T + c 2nd degree polynomial
% cp = a*T^3 + b*T^2 + c*T + d 3rd degree polynomial
co_effs1 = polyfit(temperature,cp,1);
co_effs2 = polyfit(temperature,cp,2);
co_effs3 = polyfit(temperature,cp,3);
predicted_cp(:,1) = polyval(co_effs1,temperature);
predicted_cp(:,2) = polyval(co_effs2,temperature);
predicted_cp(:,3) = polyval(co_effs3,temperature);
%Plotting the curves
subplot(2,2,1)
plot(temperature,cp,'linewidth',1,'color','b')
xlabel('Temperature[K]')
ylabel('Specific Heat[kJ/kMol-K]')
legend('Original Data')
subplot(2,2,2)
plot(temperature,cp,'linewidth',1,'color','b')
hold on
plot(temperature,predicted_cp(:,1),'linewidth',1,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat[kJ/kMol-K]')
legend('Original Data','Linear Fit')
subplot(2,2,3)
plot(temperature,cp,'linewidth',1,'color','b')
hold on
plot(temperature,predicted_cp(:,2),'linewidth',1,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat[kJ/kMol-K]')
legend('Original Data','Quadratic Fit')
subplot(2,2,4)
plot(temperature,cp,'linewidth',1,'color','b')
hold on
plot(temperature,predicted_cp(:,3),'linewidth',1,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat[kJ/kMol-K]')
legend('Original Data','Cubic Fit')
%Finding error through different methods
%Linear Fit Model
SSE_linear = sum((cp - predicted_cp(:,1)).^2);
RMSE_linear = sqrt(SSE_linear./3200);
SSR_linear = sum((predicted_cp(:,1)-mean(cp)).^2);
SST_linear = SSE_linear + SSR_linear;
R_square_linear = SSR_linear./SST_linear;
%Quadratic Fit Model
SSE_quadratic = sum((cp - predicted_cp(:,3)).^2);
RMSE_quadratic = sqrt(SSE_quadratic./3200);
SSR_quadratic = sum((predicted_cp(:,3)-mean(cp)).^2);
SST_quadratic = SSE_quadratic + SSR_quadratic;
R_square_quadratic = SSR_quadratic./SST_quadratic;
%Cubic Fit Model
SSE_cubic = sum((cp - predicted_cp(:,3)).^2);
RMSE_cubic = sqrt(SSE_cubic./3200);
SSR_cubic = sum((predicted_cp(:,3)-mean(cp)).^2);
SST_cubic = SSE_cubic + SSR_cubic;
R_square_cubic = SSR_cubic./SST_cubic;
Explanation
The Temperature v/s Specific Heat Data is imported from the file provided using the 'load' command.
The 'polyfit' command gives the values of the coefficients in the fitting polynomial. Two fitting polynomials are used in this project:- one linear and other one cubic.
The 'polyval' command is used to find out the polynomial values at all the temperature points.
The graphs are plotted using the 'plot' command. These graphs are plotted as a grid using the 'subplot' command.
The blue line corresponds to the original data set. Red line corresponds to the fitted curve.
The error values Sum of Squares Error(SSE),Root Mean Square Error(RMSE),R-square are calculated to find out the fitness of the curve.
Results
The graphs were plotted and are obtained as below.
The error values obtained are as below
Conclusion and Inference
Typically if the R² error value is more than 0.95,the fit is assumed to be good.
Lower values of RMSE indicate better fit. RMSE is a good measure of how accurately the model predicts the response, and it is the most important criterion for fit if the main purpose of the model is prediction.
From the results we can conclude that the higher polynomial gives less error values and the curve fits better.
Comparing the values of R² of all the order of polynomials we can say that third order has 0.9967 which is greater than 0.95 hence cubic polynomial gives a better fit in this case.
1. How to make a curve fit perfectly?
To fit the curve perfectly either we increase the order of the polynomial, or divide the data set into parts or ranges and calculating the coefficients for each data set to yield the perfect curve.
2. How to get the best fit?
As per least sum of squares of errors, the curve which has the least sum of squares of errors is the best fit.
As per R² method, if the R² value is >0.95, the curve is said to be best fitted.
As per RMSE, the polynomial which has the least RMSE value is the best fit.
3. What could be done to improve the cubic fit?
Cubic fit can be improved by determining prediction intervals, and by centering and scaling.
References
Introduction to MATLAB Programming - NPTEL Lecture Course
Essential MATLAB for Engineers And Scientists - Brian Hahn & Daniel T Valentine