Curve Fitting using MATLAB
Aim: To fit a linear and cubic curve on a given dataset of temprature and specific heat
Theory:
Curve fitting is a process of construction of a curve or a mathematical function, that has a best fit to a series of data points. Fitted curve or mathematical function can be used to infer value of function at points where no data is available.
1. Linear curve fitting:
A straight line is fitted with given datapoints to get a best fit.
`y = mx + c`
The objective is to find values of m and c that best fits the data points x and y.
2. Polynomial curve fitting:
A `n^(th)` degree polynomial is fitted with given data point for best fit.
`y = a_1x^n + a_2x^(n-1) + a_3x^(n-2) + ........+ a_(n-1)x + a_n`
The objective is to find values of `a_1,a_2,a_3,..........,a_n` that best fits the data points x nd y.
For one degree polynomial, n=1
`y = a_1x + a_2`
For two degree polynomial, n=2
`y = a_1x^2 + a_2x + a_3`
For cubic polynomial, n=3
'`y = a_1x^3 + a_2x^2 + a_3x + a_4`
MATLAB Main Program:
% Program for Curve Fitting
close all
clear all
clc
% data preparation
cp_data = load('data'); % Data Load
temprature = cp_data(:,1); % Separating Temp data
cp = cp_data(:,2); % Separating speccific heat data
%Curve fitting
% Linear Curve Fitting
coeff_linear = polyfit(temprature, cp, 1);
cp_linear = polyval(coeff_linear, temprature);
% Cubic Curve Fitting
coeff_cubic = polyfit(temprature, cp, 3);
cp_cubic = polyval(coeff_cubic, temprature);
% comparing fits with plots
plot(temprature, cp, 'linewidth', 2, 'color', 'b')
hold on
plot(temprature, cp_linear, 'linewidth', 2, 'color', 'r')
plot(temprature, cp_cubic, 'linewidth', 2, 'color', 'g')
xlabel('Temprature[K]')
ylabel('Specific Heat[KJ/Kmol-K]')
legend('Raw data', 'Linear fit', 'Cubic fit')
% Computing R_square
R_2_linear = R_square(cp, cp_linear)
R_2_cubic = R_square(cp, cp_cubic)
R_square Function:
% function to find value of R_square
function R_2 = R_square(cp_original, cp_predicted)
x = length(cp_original); % Length of dataset
sum = 0;
for i = 1 : x
sum = sum + cp_original(i);
end
mean = sum/x; %calculating mean of Cp data
ssr = 0; % ssr = sum of squares of regression
for i = 1 : x
ssr = ssr + (cp_predicted(i)-mean)^2;
end
sse = 0; % sse = sum of squares of error
for i = 1 : x
sse = sse + (cp_original(i)-cp_predicted(i))^2;
end
sst = ssr + sse;
R_2 = ssr/sst;
end
s
R_sqaure calculates `R^2` error for the fit.
polyfit command fits a polynomial curve for data points and returns the coefficients.
polyval command takes coefficients and input data to return value of the function at that point.
Output:
Fit Compare:
It is evident from the above plot that cubic fit gives a better fit than linear fit. It can also be concluded using `R^2` values observed in command window.
The `R^2` value is higher for cubic fit. Higher the value better the fit.
Linear fit using Curve Fitting Tools:
Cubic Fit using Curve Fitting Tools:
How to make a curve fit perfectly?
As degree of polynomial is increased, the curve gets more smoother. As it can concluded from above figures that cubic polynomial gives a smoother and better fir than single degree polynomial (Straight line). Thus by increasing the degree of polynomial perfect fit can be obtained.
How to get the best fit?
The curve can be tested for best fit based on following four quantities.
1. Sum of squared errors(SSE)
It is sum of square of difference between actual value and predicted value of function.
`SSE = sum(y_i-f(x_i))^2`
As this value decreases the curve goes towards best fit.
2. R_sqaure
It is square of correlation between the actual value and the predicted value.
`R^2 = (SSR)/(SST)`
`SST = SSR + SSE`
SSR is sum of squared regression. It is sum of square of difference between predicted value and mean of actual values.
`SSR = sum(f(x_i) - mean)^2`
As the value of `R^2` increases, fitted curve goes toward best fit.
It can be seen that in present example `R^2` for linear curve is 0.9249 and for cubic curve it is 0.9967.
3. Adjusted R_Square
It is a normalized variation of `R^2` which neglects variation of `R^2` due to insignificant independent variables.
`R_(adj)^2 = 1 - [((1-R^2)(n-1))/(n-k-1)]`
where,
n = number of datapoints
k = number of independent variables
4. Root mean squared error(RMSE)
It is square root of mean of SSE.
`RMSE = sqrt((SSE)/n`
where,
n = number of datapoints
What could be done to improve the cubic fit?
When cubic curve is fitted for given data set of temprature and specific heat then it throws a warning about bad polynomial coefficients. The error was due to disimilar division on temprature axis. The warning can be eliminated by using center and scale option in curve fitting tool. This will improve the fit.
Reference :
projects.skill-lync.com/Curve-Fitting-and-criterion-to-choose-best-fit-41860