CURVE FITTING IN MATLAB
AIM: To code a program to fit a linear and cubic polynomial and to find the best fit for the give data points.
SOFTWARE USED: MATLAB R2019b
THEORY:
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.
BEST FIT: Best fit simply means that the differences between the actual measured values and the values predicted by the model equation are minimized. It does not mean a "perfect fit"; in most cases, a least-squares best fit does not go through all the points in the data set.
PERFECT FIT: Perfect fit is when the R^2 value is exactly 1, and there are no errors between the dataset curve and the polynomial curve created.
There are 4 main quantities that help us measure the goodness of the fit or how well the equation is representing the given data points;
SSE: SSE is the sum of the squared differences between each observation and its group's mean. It can be used as a measure of variation within a cluster. If all cases within a cluster are identical the SSE would then be equal to 0.
R^2: R-squared is a statistical measure that represents the proportion of the variance for a dependent variable that's explained by an independent variable or variables in a regression model. So, if the R^2 of a model is 0.50, then approximately half of the observed variation can be explained by the model's inputs.
Adj.R^2: The adjusted R-squared is a modified version of R-squared that has been adjusted for the number of predictors in the model. The adjusted R-squared increases only if the new term improves the model more than would be expected by chance. It decreases when a predictor improves the model by less than expected by chance. The adjusted R-squared can be negative, but it’s usually not. It is always lower than the R-squared.
RMSE: Root Mean Square Error is the standard deviation of the residuals (prediction errors). Residuals are a measure of how far from the regression line data points are; RMSE is a measure of how spread out these residuals are. In other words, it tells you how concentrated the data is around the line of best fit.
PROGRAM:
LINEAR AND CUBIC CURVE FITTING;
%Curve fitting
%Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%Curve fit
%linear
%cp= a*T^2+b*T+c
co_effs=polyfit(temperature,cp,1);
predicted_cp=polyval(co_effs,temperature);
%Linear Curve fit VS original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
%cubic
co_effs=polyfit(temperature,cp,3);
predicted_cp=polyval(co_effs,temperature);
%Cubic Curve fit VS original data
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','b')
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ]')
legend('original data','linear curve fit','cubic curve fit')
This program gives us the curve fitting for linear and cubic polynomial w.r.t the original data,
PLOT DIAGRAM FOR CURVE FITTING:
From the graph,we infer that the cubic polynomial curve is better fitted with the original dataset curve compared to the linear polynomial curve.
LINEAR AND CUBIC CURVE FOR BEST FIT;
%BEST FIT
%Data
cp_data = load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%LINEAR
a=1;
b=400;
figure(1)
for i=1:8
co_effs_linear=polyfit(temperature(a:b),cp(a:b),1);
predict_cp_linear(a:b)=polyval(co_effs_linear,temperature(a:b));
a=b+1;
b=b+400;
end
plot(temperature,cp,'linewidth',3,'color','r')
hold on
plot(temperature,predict_cp_linear,'linewidth',3,'color','g')
hold off
legend('cp','linear best fit')
xlabel('Temperature[K]')
ylabel('Specific heat[KJ]')
%CUBIC
a=1;
b=400;
for i=1:8
co_effs_cubic=polyfit(temperature(a:b),cp(a:b),3);
predict_cp_cubic(a:b)=polyval(co_effs_cubic,temperature(a:b));
a=b+1;
b=b+400;
end
figure(2)
plot(temperature,cp,'linewidth',3,'color','r')
hold on
plot(temperature,predict_cp_cubic,'linewidth',3,'color','b')
hold off
legend('cp','cubic best fit')
xlabel('Temperature[K]')
ylabel('Specific heat[KJ]')
clear sum
s=sum(cp);
mean=s/length(cp);
for i=1:length(cp)
ssr1(i)=(predict_cp_linear(i)-mean)^2;
sse1(i)=(cp(i)-predict_cp_linear(i))^2;
end
SSR = sum(ssr1)
SSE = sum(sse1)
SST = SSR + SSE
R1 = SSR/SST
for i=1:length(cp)
ssr2(i)=(predict_cp_cubic(i)-mean)^2;
sse2(i)=(cp(i)-predict_cp_cubic(i))^2;
end
SSR1 = sum(ssr2)
SSE1 = sum(sse2)
SST1 = SSR1 + SSE1
R2 = SSR1/SST1
Using splitwise function we are able to generate a perfect curve for a cubic polynomial,which exactly matches with the given dataset curve.
And also,by using the 4 quantities to determine the goodness of a system,we were able to bring the R^2 value to an exact '1' ,and generate a perfect curve.
RESULTS:
PLOT DIAGRAM FOR BEST FITTING:
LINEAR:
CUBIC:
RESULT: Thus using MATLAB we were able to polt a "Perfect curve fit";w.r.t the given dataset.