Curve Fitting of a Polynomial
OBJECTIVE
To execute a matlab program wherein to fit a data with the polynomial with n degrees with least error.
This can be achieved by two methods
- fitting the data plots by use of polyfit and polyval commands upto n degree
- fitting the data plots by use of polyfit and polyval commands upto n degree and making use of error between actual and predicted values
INTRODUCTION
In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modelled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x) [1].
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 [2].
In statistics and image processing, to smooth a data set is to create an approximating function that attempts to capture important patterns in the data, while leaving out noise or other fine-scale structures/rapid phenomena. In smoothing, the data points of a signal are modified so individual points (presumably because of noise) are reduced, and points that are lower than the adjacent points are increased leading to a smoother signal [3].
MATLAB PROGRAM
close all
clear all
clc
% preparing the data
cp_data = load ('data');
temperature = cp_data(:,1);
cp= cp_data(:,2);
%% Curve fit and plotting
for i=1:5
% curve fit
% cp= aT^n+ bT^(n-1)+....+c
co_eff= polyfit(temperature, cp,i);
predicted_cp= polyval( co_eff, temperature);
%% comparision
k=i;
H=[ 'Cp Vs Temperature for n=', num2str(k)];
figure(1)
subplot (3, 2, i)
plot(temperature, cp, 'color', 'b')
hold all
plot (temperature, predicted_cp, 'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat [kJ/kmol-K]')
title(H);
hold off
%% to create a table which shows the error between the actaul value and
% predicted value
t= table(temperature,cp,predicted_cp,cp-predicted_cp,'VariableNames',{'Temp','cp','predicted_cp','FitError'});
error_table{i}=t;
figure(2)
error= cp-predicted_cp;
new_cp= cp+(error);
subplot (3, 2, i)
plot(temperature, cp, 'color', 'b')
hold all
plot (temperature,new_cp, 'color','g')
xlabel('Temperature[K]')
ylabel('Specific Heat [kJ/kmol-K]')
title(H);
hold off
end
RESULTS AND DISCUSSION
Fig (1): Cp vs Temperature plots for different values of n=1 to 5 in polyfit command
Fig (2): Cp vs Temperature plots for different values of n=1 to 5 in polyfit command and making use of error between actual and predicted values.
In the above fig(1) and fig(2) the major difference is that in the fig(2) error is considered. This error is the diffrence of the actual value and the predicted values. this error is added to the actual values so that the new values obtained (new_cp) is smoothened for the data set. This new Cp values are used to plot the fig(2).
In fig(1) it can be seen that as the values of n is increased the curve is fitted to best. for n=1 which is the linear fit, there is large error between the actaul and predicted values. This can be obtained in the error table hence plotted (please run the matlab program).
The error hence genreated is used to smooth the predected values as the error is added to the actual values. This can be observed in fig(2). for n=3, a smooth fit can be obtained by using this method.
It can be conclued that by using error function, a curve can be tried to fit with a relatively smaller values of n.
REFERENCE