Linear and Cubic curve fitting using MATLAB
Curve fitting
Curve fitting is a way of mimicing the behavoiur of a real series of data with the help of certain mathematical equations and trying to achieve maximum proximity to the real data set. Usually, the curve fit will produce an equation that can be used to find points anywhere along the curve.
Making a perfect curve fit
A perfect curve fit is one in which there are no residuals. In other words, there should not be any error between the curve fit and the actual data set.In most complex scenerios or real world problems, this is very difficult. There appears to be always a smallest degree of error, unless the curve fit is done for an already known function.
However, it is always possible to obtain a 'best fit' for such data set.
Best fit
A best fit is a curve or the mathematical euqation that represents the given data set in the most proximate manner. i.e. The best fit will always have the least errors or resudials of all the other fits that can possibly be used to represent that particular set of data.
Problem definition:
The aim of this project is to obtain linear and cubic curve fits for the given set of data using MATLAB.
Problem Approach:
Firstly the data life is loaded and assigned as an array to a variable. Then, the temperature and the specific values are assigned to seperate variables respectively.
The actual plot is then obtained. This is like a reference against which the fitted curve will bw plotted. This will give us a visual representation of the proximity of the fitted curve.
Linear curve fitting
The 'polyfit' command is then used to obtain the coeffecients of the equation that represents the fitted curve. The degree is set to 1 in the polyfit command so that we will a straight line.
The function that represents the line will be `y=mx`.
Where 'm' is the co-effecient and 't' is the temperature.
The 'polyval' command makes sure to substitue these coeffeceints for all temperature values and returns the value of specific heat at those points. Now, these values can be plotted against the actual curve for visual comparison.
The 'xlabel', 'ylabel' and 'legend' commands are used in order to make the plot more understandable.
The new red colour line represents the curve fit. As it can be seen, the linear fit does not suit this curve and there are lot of errros.
Cubic curve fitting
In order to obtain a better fit, the order of the euation has to be increased under the 'polyval' command. A degree of three is mentioned in order to obtain a cubic curve fit.
The function that represents the line in this case will be `y=at^3+bt^2+ct+d`
Where a,b,c, and d are co-effecients and 't' is the temperature.
It is obvious from this image that the cubic fit provides a better approximation than the linear fit.
Accuracy:
Although the cubic fit is better than the linear fit, we get an error message.
"Badly conditioned" means that the solution of the system of linear equations critically depends on rounding errors due to the limited precision. This error can be solved using scaling options such as specifying the limits of the plot using the 'axis' command.
Improving accuracy of cubic fit
The accuracy of the cubic fit can be further improved by using split-wise or piecewise polynomial approximation technique. It just means, breaking up the entire data set into smaller fragments and then performing seperate fits for those fragments. This improves the accuracy in these fragments and so the overall accuracy is greatly improved.
MATLAB code
% Program to fit a linear and a cubic curve for a set of data that has been given
clear all
close all
clc
% Loading the values from a seperate file
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
%Plotting the actual values
plot(temperature,cp,'linewidth',3)
% Curve fit - Linear
coeff_linear=polyfit(temperature,cp,1);
predicted_cp_linear=polyval(coeff_linear,temperature);
% Comparing linear curve fit with the actual plot
figure(1)
hold on
plot(temperature,predicted_cp_linear,'linewidth',3,'color','r')
xlabel('Temperature (K)');
ylabel('Specific Heat (KJ/Kmol-K)');
title('Linear Curve Fit (Simple Linear Regression')
legend('Original data set','Curve fit','location','northwest')
% Curve fit - Cubic fit
figure(2)
%Plotting the actual values
plot(temperature,cp,'linewidth',3)
%Fitting and obtaining values of cp at the specific temperatures
coeff_cubic=polyfit(temperature,cp,3);
predicted_cp_cubic=polyval(coeff_cubic,temperature);
%Comparing cubic fit with the actual data set
hold on
plot(temperature,predicted_cp_cubic,'linewidth',3,'color','r')
xlabel('Temperature (K)');
ylabel('Specific Heat (KJ/Kmol-K)');
title('Cubic Curve Fit')
legend('Original data set','Curve fit','location','northwest')
Some of the parameters that describe the fitness characteristics of the curves are:
1. R square
2. Sum of sqaures of the errors (SSE)
3. Root mean square (RMS)
4. Adjusted R square
References:
1. http://www.synergy.com/Tools/curvefitting.pdf
3. https://www.originlab.com/index.aspx?go=Products/Origin/DataAnalysis/CurveFitting