Project

curve fitting

Aim:The aim of this study is that to understand the curve fitting and how can we select suitable fitting method for finding the predictable value at unknown value .Under this study we will understand the curve fitting by matlab programing.In matlab, we will show the different polynomial fit and error corresponding to their polynomial fit.

1.Introduction: curve fitting is the technique to fitting a mathmatical function like any polynomial function to a given data points.we can try different polynomial degree to know which degree polynomial is best for fitting to given data or by any other technique like interpolation .there are some understanding which can be useful to selecting suitable various function to given data.

Type of Curve Fits:

1.Least Squares Curve Fits.

2.Smoothing Curve Fits.

3.Nonlinear Curve Fits.

1.1 Least Squares Curve Fits:It is most popular method.This method minimizes the square of error between the original data and value predicted by equation.There is one problem with this method,it is that if data is widely different from majority of data then th regression result by it skewed means not symmetric ,alighn on one side.The some least square fits are polynomial fits,linear,exponential fits etc.

Polynomial function: Under this function fits curve through the data in the form of y=a0+a1x + a2x^2+ a3x^3+...anx^n.

1.1.1 Linear: Under this, function fits a straight line through given data by following the equation y=a+bx. (a is the slope of line, b is the intercept on y axis by line, x is independent variable and y is dependent variable). if we notice that function value (dependent value) of given data is increases or decreases by constant slope value then we can select linear function to fit in given data.It is basically one degree polynomial.

1.1.2 Two degree polynomial: Function up to two degree polynomial refers as parabola shape , it means if given data points are scattered in the form of parabola shape or by one valley shape then we can use two degree polynomial fit.Here is one valley at 0 on horizontal axis.

Fig 1. Two degree polynomial fit [1]

if we find that data is scattered in the two valley shape we can use three degree polynomial fit or we can say cubic fit. Here in given figures there are two valleys present. First valley is in between 199 and 399 and second valley is near just before to 799 at horizontal axis

Fig 2. Three degree polynomial fit [2]

similiarly if we find that data is scattered in the form of three valleys shape then we can select four degree polynomial fit . It means if data is scattered in n number of valleys then we can select n+1 number of polynomial degree fit.

1.1.3 Exponential:Under this function fits the curve in the form of y=a e^(bx) , here 'e' referd function as exponential function.This curve fit can be use if the given data increases or decreases at high rate. one important thing that this exponential function does not govern the negative or zero value.So it is one another markable point with increasing and decreasing of data at high rate to exponential fitting.

Fig 3. exponential fit [3]

here in above given figure the data drastically changes or decrease with high rate from 450 to nearest 100 on vertically axis with respect to horizontal axis points from nearest 1 to 10.

1.2 Smooth Curve Fits:There is no single equation is used to represent whole fitting curve.It is different from other two method .when we want to good appearence of fitting then that type of fitting is used Under this we match the data points to each other.The some smooth curve fits are Interpolation,cubic spline etc.

1. 2.1Interpolation Technique: Interpolation is kind of algorithm.It is not as continous function to following any equation as in second degree ,third degree polynomial or any other through out all given data point.We can use it as a piecewise interpolation between point to point.It is just matching the given closed data points,so this matching can be linear interpolation,polynomial interpolation,or spline interpolation. There is the difference between the interpolation fitting and polynomial fitting.

Fig 4. Difference between Interpolation and Curve fitting (linear or one degree polynomial) [4]

Here we can see f(x)= ax+b equation follow pattern piece to piece means one pattern will be in between two points and other pattern will be in another two points but in curve fitting ,a single f(x)=ax+b equation pattern will be followed. In Interpolation fitting, the piece wise fitting or matcing point can be by polynomial degree or smooth function like splne fitting.

1.2.2 Cubic Spline:Under this ,every group of four consecutive data points defines a cubic spline curve and the collection of series of these cubic polynomial sets known as cubic spline fits

1.3 Nonlinear Curve Fits:It is most powerful curve fits,Under this user can defined it's own equation and can fits the data points

2.Parameters to computing best fitting: After fitting the curve ,there is one important thing we should know that how much our predicted data curve fitting is closest to given data function.For this there are some parameters:

R squared( R^2).It's value varies between 0 to 1. 0 refers poor fitting and 1 refers to the best fitting and by varying values between 0 and 1 we can predict how much our fitting is good or bad.

R^2 = S.S.R / S.S.T

S.S.R means sum of squares of regression.It means it is the sum of squared differences between the prediction for each observation and the population mean (mean of dependent data).

S.S.T is the summation of S.S.R and S.S.E .

S.S.E is sum of squared errors.

Adjusted R squared: is also one parameter to computing the goodness of fitting.This parameter neglect the insignificat data points which have no effect on output,it consider the effective data points.Instead R squared contain number of data points ,it does not take the matter how much data points are effective or non effective on output

Let understand the curve fit with matlab programing by taking an example :

Firstly load data file for which we will do program:

click here to download data file, after this we have done programing below:

clear all
close all 
clc
points=load('data');
temperature=points(:,1);
cp=points(:,2);


for i=1:3
constants=polyfit(temperature,cp,i);
predicted_cp=polyval(constants,temperature)
error=(predicted_cp-cp);
SSEi=sum(error.^2);
%calculation of sum of square of regression
SSRi = sum((predicted_cp-mean(cp)).^2);
SSTi= SSEi + SSRi ;
%calculation of R_square
figure(i)
%subplot(3,2,i)
hold on

plot(temperature,cp,'linewidth',2)
plot(temperature,predicted_cp,'linewidth',2)
legend('real_data_plot','predicted_data_plot')

xlabel('Temperature[k]')
ylabel('specific heat[kj/kmol-k]')
R_square = SSRi/SSTi;
end

In the above programing firstly we load data file.Then seprate that data into temrature data and specific heat (c_p) data.Now we will make 'for loop' which is basically for taking polynomial degree from 1 to 3. By polynomialfit command we will find constant of polynomial degree equation and then by polyval command ,we will find the predicted values at finded constant values.After it we will find the error in between predicted value and given data value and then we will find which is the best fitting.

plots:

(a) For polynomial degree 1, curve fitting

R_square=0.9249

(b) For polynomial degree 2, curve fittig

R_square=0.9812

(c) For polynomial degree 3,curve fitting

R_square=0.9967

Here ,we can see in above curve fitting figures that as we increases the polynomial degree from 1 to 3 the curve fitting is going to better and R_square also increases toward value 1 which is symbol of good curve fitting,means by increasing polynomial degree we can make better curve fitting but it is not always true .Let understand by another matlab programing language.[5]

clear all
close all
clc
%curve fitting understanding for selection of suitable polynomial degree by an example.
x=[0 0.5 1.1 1.7 2.1 2.5 2.9 3.3 3.7 4.2 4.9 5.3 6.0];
y=[1.1 1.6 2.4 3.8 4.3 4.7 4.8 5.5 6.1 6.3 7.1 7.1 8.2];

% Now we will find coefficients of fitting polynomial equation
n=length(x)
for i=1:(n-1)
pcoeff=polyfit(x,y,i)
x2=0:0.1:6;
y2=polyval(pcoeff,x2)
subplot(3,4,i)
plot(x2,y2)
hold on
plot(x,y,'b*')
legend('predicted value','real value')
end
%now by finding the polynomial coefficient value by x annd y data then it will help for finding the values at x2 values  

plots:

for polynomial degree 1,curve fitting

for polynomial degree 2,curve fitting

for polynomial degree 3,curve fitting

as going to increasing degree up to 9 it shows better curve fitting but after increasing polynomial degree 9,the curve fitting shows some irregularity variation at outer most portion in figure,which can be seen in below figures:

for polynomial degree 10,curve fitting:

for polynomial degree 11,curve fitting

for polynomial degree 12,curve fitting

Result: Firstly we founded that by increasing the polynomial degree ,the fitting goes better for higher degree.But we also finded that it is not always true we can see in curve fitting of degree 10,11 and 12 . Here we can see that at x-axis 0 to1 and 5 to 6 we can not predict good comparable values but for low degree polynomial fitting we can predict .

so our conclusion is that we can take appropriate fitting by selecting proper and appropriate degree of polynomial which does not show irregularity peaks of valleys as in curve fitting of degree 10,11 and 12.

Refrences:

[1] figure 1

[2] figure 2

[3] figure 3

[4] figure 4

[5] leacture video on data fitting of second program which consist the peak valleys or irregularties at outer most portion on plotting fitting

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.