Project

CURVE FITTING USING MATLAB

AIM: write a program on curve fitting using linear and cubic fitting from given raw data points. Determine the best fit and perfect fit.

INTRODUCTION:

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.

by curve fitting we can mathematically construct functional relationship between observed dataset and parameter values.

Types of Curve fitting:

(a) Most commonly, one fits a function of the form `y=f(x)`.

(b) The first degree polynomial equation

`y=ax+b`

(c) If the order of the equation is increased to a second degree polynomial, the following results:

`y=ax^2+bx+c`

This will exactly fit a simple curve to three points.

(d) If the order of the equation is increased to a third degree polynomial, the following is obtained:

`y=ax^3+bx^2+cx+d`

This will exactly fit four points.

The goodness of fit of a stastitical model describes how well it fits a set of observations. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question. Such measures can be used in statistical hypothesis testing eg. "test of normality" to test whether two samples are drawn from identical distributions (Kolmogorov–Smirnov test) or whether outcome frequencies follow a specified distribution (see Pearson's chi-squared test). In the analysis of variance, one of the components into which the variance is partitioned may be a lack of fit sum of squares.

OBJECTIVE:

1. How to get best fit ?

2. How to make a curve fit perfectly ?

3. What could be done to improve the cubic fit ?

write a program on Linear and cubic curve using above points consideration.

HOW TO GET BEST FIT ?

The parameters used to the fitness characteristics for both the curve:

  1. The sum of the square due to error (SSE)
  2. R-square
  3. Adjust R-square
  4. Root mean square error (RSME)

1. The sum of the square due to error(SSE)

ERROR is often necessary to assume that the measurement error in one measurement is statistically independent of the measurement error in all other measurements.When reading this, think of the function as representing the coefficients in the curve-fit, and the set of random variables as the sample of measured data.

Error formula is given in the image that i found is to calculate error of different point that far from line or random line.

`SSE=sum_(i=1)^n E(i)^2`

where E = Error.

2.R-square:

In statistics the coefficient of determination, denoted R2 or r2 and pronounced "R squared", is the proportion of the variance in the dependent variable that is predictable from the independent variable.

A data set has n values marked y1,...,yn (collectively known as yi or as a vector y = [y1,...,yn]T), each associated with a fitted (or modeled, or predicted) value f1,...,fn (known as fi, or sometimes ŷi, as a vector f).

If is the mean of the observed data:

`bar y = 1/n sum_(i=1)^n yi`

then the variability of the data set can be measured using three sums of square formula:

the total sum of squares (SST)

`SST=sum_i(yi-bary)^2`

The regression sum of squares(SSreg)

`SSreg=sum_i (fi-bary)^2`

The sum of squares of residuals(SSR)

`SSR=sum_i(yi-fi)^2`

so, value of `R^2` can be calculated as

`R^2=1-(SSR)/(SST)`

3.Adjusted R:

R2 shows how well terms (data points) fit a curve or line. Adjusted R2 also indicates how well terms fit a curve or line, but adjusts for the number of terms in a model. If you add more and more useless variables to a model, adjusted r-squared will decrease. If you add more useful variables, adjusted r-squared will increase.
Adjusted R2 will always be less than or equal to R2.

You only need R2 when working with samples. In other words, R2 isn’t necessary when you have data from an entire population.

`R^2adj=1-[((1-R^2)(n-1))/(n-k-1)]`

where, n = number of data points

k = number of independent variable.

Here are two cases:

  • `'R^2adj' darr when 'k' uarr` and R-square changes very less
  • `'R^2adj' uarr when 'R^2' uarruarr and 'k' uarr`.

4.Root mean square error(RMSE):

The root-mean-square deviation (RMSD) or root-mean-square error (RMSE) is a frequently used measure of the differences between values (sample or population values) predicted by a model or an estimator and the values observed. The RMSD represents the square root of the second sample moment of the differences between predicted values and observed values or the quadratic mean of differences.

`RMSE=sqrt((SSE)/n)`

where n = total number of data points.

General Example of Curve Fitting:

Lets do it with MATLAB:

Code :

1. Linear fit

%curve fitting for linear 
clear all
close all
clc

%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%curve fit for linear fit
%cp = a*T+b
co_eff = polyfit(temperature,cp,1);
predicted_cp = polyval(co_eff,temperature);

%comparing the results
plot(temperature,cp,'linewidth',3)       %first plot of observed data
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r') %second plot of predicted data
title('LINEAR FIT')
xlabel('Temperature [K]')
ylabel('specific Heat [KJ/Kmol.K]')
legend('Original Data','Curve Fit')
axis([0 5000 800 1500])

output:

2. cubic fit :

%curve fitting for cubic fit
clear all
close all
clc

%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%curve fit for cubic fit
%cp = a*T^3+b*T^2+c*T+d   equation
co_eff = polyfit(temperature,cp,3);
predicted_cp = polyval(co_eff,temperature);

%comparing the results
plot(temperature,cp,'linewidth',3,'color','m')       %first plot of observed data
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','k') %second plot of predicted data
title('CUBIC FIT')
xlabel('Temperature [K]')
ylabel('specific Heat [KJ/Kmol.K]')
legend('Original Data','Curve Fit')
axis([0 5000 800 1500])

output:

HOW TO MAKE CURVE FIT PERFECTLY ?

In order to make curve fit perfectly we have to increase degree of order of polynomial to get perfect fit. in our case if I put degree of polynomial 9. let see

HOW TO GET BEST FIT :

Since, we see that the polynomial function gives mathematical curve fitting model of data.first degree of polynomial gives simple linear fit which is not that appropriate,its leads to an error of points that we discuss earliar. if we put degree of polynomial 2 the graph changes slightly towards exact solution so degree of polynomial is play important role so higher is degree of polynomial more closely polynomial fits the data.

Choosing the polynomial degree is matter of experimenting to determine what works best.

WHAT COULD BE DONE TO IMPROVE CUBIC FITS:

while execute the cubic fit problem matlab throws a warning message :

Overcome : This can be done by using split the Program data into subparts in our case 5 subparts.

Code:-

I use linewidth 4 for observed data and linewidth 2 for curve fitting points. this for our visualisation that curve is exactly fits.

%curve fitting for cubic fit  
clear all
close all
clc

%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%splitting into 5 subparts
temperature1 = cp_data(1:640,1);
cp1 = cp_data(1:640,2);

temperature2 = cp_data(641:1280,1);
cp2 = cp_data(641:1280,2);

temperature3 = cp_data(1281:1920,1);
cp3 = cp_data(1281:1920,2);

temperature4 = cp_data(1921:2560,1);
cp4 = cp_data(1921:2560,2);

temperature5 = cp_data(2561:3200,1);
cp5 = cp_data(2561:3200,2);

%curve fit for Cubic fit
%cp = a*T^3+b*T^2+c*^T
[co_eff1,S,mu] = polyfit(temperature1,cp1,3);
predicted_cp1 = polyval(co_eff1,temperature1,[],mu);

[co_eff2,S,mu] = polyfit(temperature2,cp2,3);
predicted_cp2 = polyval(co_eff2,temperature2,[],mu);

[co_eff3,S,mu] = polyfit(temperature3,cp3,3);
predicted_cp3 = polyval(co_eff3,temperature3,[],mu);

[co_eff4,S,mu] = polyfit(temperature4,cp4,3);
predicted_cp4 = polyval(co_eff4,temperature4,[],mu);

[co_eff5,S,mu] = polyfit(temperature5,cp5,3);
predicted_cp5 = polyval(co_eff5,temperature5,[],mu);

%comparing the results

plot(temperature,cp,'linewidth',4,'color','r')       %first plot of observed data
hold on
plot(temperature1,predicted_cp1,'linewidth',2,'color','y') %splited data plot
hold on
plot(temperature2,predicted_cp2,'linewidth',2,'color','y')
hold on
plot(temperature3,predicted_cp3,'linewidth',2,'color','y')
hold on
plot(temperature4,predicted_cp4,'linewidth',2,'color','y')
hold on
plot(temperature5,predicted_cp5,'linewidth',2,'color','y')
title('CUBIC FIT')
xlabel('Temperature [K]')
ylabel('specific Heat [KJ/Kmol.K]')
legend('Original Data','Curve Fit')
axis([0 5000 800 1500])

for calculation of `R^2`:

code:

this code is Calculate R^2 value for both Linear and Cubic fit

%curve fitting for predicting the R^2 
clear all
close all
clc

%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%curve fit for linear fit
%cp = a*T+b
[co_eff1,S,mu] = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_eff1,temperature,[],mu);

%curve fit for Cubic fit
%cp = a*T^3+b*T^2+c*^T
[co_eff2,S,mu] = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_eff2,temperature,[],mu);



%comparing the results
figure(1)
plot(temperature,cp,'linewidth',2,'color','m')       %first plot of observed data FOR linear
hold on
plot(temperature,predicted_cp1,'linewidth',2,'color','k') 
title('LINEAR FIT')
xlabel('Temperature [K]')
ylabel('specific Heat [KJ/Kmol.K]')
legend('Original Data','Curve Fit')
axis([0 5000 800 1500])

figure(2)
plot(temperature,cp,'linewidth',4,'color','r')       %first plot of observed data for cubic
hold on
plot(temperature,predicted_cp2,'linewidth',2,'color','y') 
title('CUBIC FIT')
xlabel('Temperature [K]')
ylabel('specific Heat [KJ/Kmol.K]')
legend('Original Data','Curve Fit')
axis([0 5000 800 1500])


%calculation of R^2
M = mean(cp);
for i = 1:length(cp)
    SSR1 = sum((predicted_cp1(i)- M)^2);
    SSE1 = sum((cp(i)-predicted_cp1(i))^2);
end
SST1 = SSR1 +SSE1;
R1 = SSR1/SST1             % 1 for linear

for i = 1:length(cp)
    SSR2 = sum((predicted_cp2(i)- M)^2);
    SSE2 = sum((cp(i)-predicted_cp2(i))^2);
end
SST2 = SSR2 +SSE2;
R2 = SSR2/SST2           %2 for cubic

OUTPUT:

R1 = 0.9592 ....... For linear fit.

R2 = 0.9907 ...... For cubic fit

ERROR:

I forgot to correct variable name from temperature 1 to temperature.

Overcome : by correcting variable name in line 18 and line 13.

CONCLUSION :

(1) I am able to calculate R^2 Value.

(2) I am able to fit the curve using cubic polynomial without warning message.

i understood the concept of curve 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.