Program to for curve fitting Linear and Cubic polynomials and checking the goodness of the fit using Matlab
Introduction:
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. 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.
Generally, curve fit algorithms determine the best-fit parameters by minimizing a chosen merit function. In order to optimize the merit function, it is necessary to select a set of initial parameter estimates and then iteratively refine the merit parameters until the merit function does not change significantly between iterations. The Levenberg-Marquardt algorithm has been used for nonlinear least squares calculations in the current implementation.
The goodness of fit is shown as an R^2 value. A value of R2=1.0 indicates a perfect fit, whereas R2=0.0 indicates that the regression model might be unsuitable for this type of data.
R^2:
The R2-value measures how much of the variation in the data points can be explained by the selected regression model:

where
(the regression sum of squares)
(the residual or error sum of squares)
(the total sum of squares, SST= SSE+SSR)
and
represents the ith fitted value (calculated using the selected model) of the dependent variable Y.
CODE:
clear all
close all
clc
%Loading given Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%no. of data points in cp
n=length(cp);
%Plot
plot(temperature,cp,'linewidth',3,'color','b');
%Curve fit
%Linear n=1 (a*t+c)
co_eff_linear=polyfit(temperature,cp,1);
predict_cp_linear=polyval(co_eff_linear,temperature);
%Plot
hold on;
plot(temperature,predict_cp_linear,'linewidth',3,'color','r');
%Cubic Ploynomial n=3 (a*t^3+b*t^2+c*t+d)
co_eff_cubic=polyfit(temperature,cp,3);
predict_cp_cubic=polyval(co_eff_cubic,temperature);
%Plot
hold on;
plot(temperature,predict_cp_cubic,'linewidth',3,'color','m');
legend('Original data','Linear Curve Fit','Cubic Curve Fit');
title('Curve Fitting of Ploynomial');
xlabel('Temperature (K)');
ylabel('Specific Heat (KJ/Kmol-K)');
grid on;
%To check the goodness of Curvefit
%Linear
error_linear=cp-predict_cp_linear;
sse_linear=sum(error_linear.^2);
ssr_linear=sum((predict_cp_linear-mean(cp)).^2);
sst_linear=sum((cp-mean(cp)).^2);
rsquare_linear=ssr_linear/sst_linear;
%root mean square error
rmse_linear=sqrt(sse_linear/n);
%Cubical
error_cubic=cp-predict_cp_cubic;
sse_cubic=sum(error_cubic.^2);
ssr_cubic=sum((predict_cp_cubic-mean(cp)).^2);
sst_cubic=sum((cp-mean(cp)).^2);
rsquare_cubic=ssr_cubic/sst_cubic;
%root mean square error
rmse_cubic=sqrt(sse_cubic/n);
RESULT: In the above code the given set of values of temperature and specific heat are loaded using load command. Temperature vs Specific heat curve is plotted. Plolyfit and Polyval commands are used to find the values of co-effecients of polynomials and predict the values of Specific heat,here values of n is taken as 1 & 3 for Linear and Cubic Polynomials.Curves are plotted for predicted values.
To check the goodness of the curve fit R^2 regression is calculated for linear and cubic polynomials as shown in the above code, R^2 values for linear and cubic are 0.9249 and 0.9967 respectively. Closer the value of R^2 to 1 better the curve fit as shown in the above figure.
Q1): How to make a curve fit perfectly?
Curve can be fitted perfectly using Splitwise Method, here the given data is split into number of smaller curves and such small curves are plotted together to form the perfect fit.
Q2): How to get best fit?
If the value of R^2 as calculated above code gives the value >0.95 then the curve is set to best fit.
Q3): What could be done to improve cubic fit?
Cubic fit can be improved by using either splitwise method or changing the order of polynomials.
https://en.wikipedia.org/wiki/Curve_fitting
https://docs.tibco.com/pub/spotfire/7.0.1/doc/html/curve/curve_curve_fit_theory.htm