Project

CURVE FITTING

AIM: Program in MATLAB to fit a linear and cubic polynomial for the given Cp data.

THEORY:

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.

Q:1 How to make a curve fit perfectly?

Perfect fit means, the curve should fit original curve without showing any errors. To make a curve fit perfectly over the data points provided, all the errors including centering and scaling errors should be nulled i.e., the value of R-square should be equal to ‘1’.

Q:2 How to get the best fit?

A line of best fit can be roughly determined using an eyeball method by drawing a straight line on a scatter plot so that the number of points above the line and below the line is about equal (and the line passes through as many points as possible).

If the value of R- square is greater than 0.95 (but not exactly 1)then it is considered as best fit.

Q:3 What could be done to improve the cubic fit?

By using the splitwise polynomial technnique.

By reducing the residue.

Goodness of Fit Statistics

Upon finding a curve which satisfies the data points there are 4 quantities which help us to measure goodness of fit criteria or how well the equation is representing the data points. They are listed as:-

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squared error (RMSE)

Sum of Squares Due to Error: This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.

Sum of Squares due to Error: `SSE=∑_(i=1)^n(Y(i)-f(x(i)))^2`

R-Square: This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).

R-square can take on any value between 0 and 1, with a value closer to 1 indicating a better fit.

R-Square: `R^(2 )= (SSR)/(SST)`

Adjusted R-square: The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.

The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit.

Adjusted R-Square: `R_a^2=1-[((1-R^2 )(n-1))/(n-k-1)]`

Root Mean Squared Error: This statistic is also known as the fit standard error and the standard error of the regression.

A RMSE value closer to 0 indicates a better fit.

Root Mean Squared Error: `RMSE = √((SSE)/n)`

INPUT:

Data file-

Data file

GOVERNING EQUATION:

SSE:

`SSE=∑_(i=1)^n(Y(i)-f(x(i)))^2`

SSR:

`SSR=∑_(i=1)^n(f(x(i)) - Mean)^2`

SST:

`SST=SSR+SSE`

R-Square:

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

Adjusted R-Square:

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

RMSE:

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

where,

n = no. of data points

k = no. of independent variables.

OBJECTIVE:

  1. To write a code to fit a linear and cubic polynomial for the cp data curve fitting.
  2. To plot the linear and cubic polynomial along with the raw data.

  3. To write a code to show the splitwise method.

PROGRAMS:-

PROGRAM 1:

clear all
close all
clc
 
% Preparing the data
cp_data = load('Data');
temperature = cp_data(:,1);
cp = cp_data(:,2);


% Curve fit

% cp = a*T^2 + b*T + c
co_effs = polyfit(temperature, cp, 3)
predicted_cp = polyval(co_effs, temperature);

%Compare my curve fit with original data
% Plotting
plot(temperature, cp, 'linewidth', 2 )

hold on
plot(temperature, predicted_cp, 'linewidth', 3, 'color', 'r' )
title('CURVE FITTING')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'Curve fit')

PLOT 1:

Warning occured:

As ,it was written that polynomial is badly conditioned. We have to try CENTERING and SCALING for the better curve fiitting.

PROGRAM 2: (Using Centering and scaling)

clear all
close all
clc
 
% Preparing the data
cp_data = load('Data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% curve fit
% S = error estimation structure
% mu = centering and scaling values(input matrix of mean)
% 'delta' represents standard 'error for prediction'

%First degree polynomial
% cp = a*T + b
[co_effs, S, mu] = polyfit(temperature, cp, 1);
[predicted_cp] = polyval(co_effs, temperature, S, mu);

%Compare my curve fit with original data 
plot(temperature, cp, 'linewidth', 2 )
hold on
plot(temperature, predicted_cp, 'linewidth', 3, 'color', 'r' )
hold on

% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T * d
[co_effs, S, mu] = polyfit(temperature, cp, 3);
[predicted_cp , delta] = polyval(co_effs, temperature, S, mu);

plot(temperature, predicted_cp, 'linewidth', 2, 'color', 'g' )
title('CURVE FITTING')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend ('original dataset','linear fit curve ',' cubic fit curve','location','northwest')

PLOT 2:

PROGRAM 3: (Using polynomial splitting technique)

In this splitting technique, we have split the curve fitting data into two polynomials. This technique helps us to better curve fit by suitable polynomial degree.

I have split the curve fit in two 1st and 3rd degree polymial.

clear all
close all
clc
 
% Preparing the data
cp_data = load('Data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

temperature_1 = cp_data(1:600,1);
cp_1 = cp_data(1:600,2);


temperature_2 = cp_data(601:3200 ,1);
cp_2 = cp_data(601:3200,2);

% curve fit using splitting technique

% S = error estimation structure
% mu = centering and scaling values(input matrix of mean)
% 'delta' represents standard 'error for prediction'

%First degree polynomial
% cp = a*T + b
[co_effs_1, S, mu] = polyfit(temperature_1, cp_1, 1);
[predicted_cp_1] = polyval(co_effs_1, temperature_1, S, mu);

%Compare my curve fit with original data 
plot(temperature, cp, 'linewidth', 5 )
hold on
plot(temperature_1, predicted_cp_1, 'linewidth', 2, 'color', 'r' )


% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T * d
[co_effs_2, S, mu] = polyfit(temperature_2, cp_2, 3);
[predicted_cp_2 , delta] = polyval(co_effs_2, temperature_2, S, mu);

plot(temperature_2, predicted_cp_2, 'linewidth',2 , 'color', 'g' )

title('CURVE FITTING')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend ('original dataset','linear fit curve ',' cubic fit curve','location','northwest')


PLOT 3:

PROGRAM 4: (For determining the Goodness of fit)

As mentioned in the theory section there are 4 quantities which help us to measure goodness of fit criteria. All the values are calculated in this program which represent the goodness of fit.

This program is for Cubic fit curve.

clear all
close all
clc
 
% Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% curve fit
% S = error estimation structure
% mu = centering and scaling values(input matrix of mean)
% 'delta' represents standard 'error for prediction'


% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T * d
[co_effs, S, mu] = polyfit(temperature, cp, 3);
[predicted_cp , delta] = polyval(co_effs, temperature, S, mu);

plot(temperature, predicted_cp, 'linewidth', 2, 'color', 'g' )

%Compare my curve fit with original data 
plot(temperature, cp,'--')
hold on 
plot(temperature, predicted_cp, 'linewidth', 2, 'color', 'r' )
hold on
title('CURVE FITTING')
xlabel('temperature[K]')
ylabel('specific heat')
legend('original dataset',' cubic fit curve','location','northwest')

% goodness of fit
% for 2 degree polynomial
% R square method
% initial value
s = 0;

for i = 1:length(cp)
    s = s + cp(i)
end

% Mean of cp values
mean  = s/3200;

% intial value
SSR = 0;
SSE = 0;

for j = 1:length(cp)
     SSR = (SSR + (predicted_cp(j) - mean)^2);
     SSE = (SSE + (cp(j) - predicted_cp(j))^2);
end

SST = SSR +SSE;
R_sq = SSR/SST

% R square adjusted method
% number of datapoints
n = 3200;

% number of independent variables
k = 1;
R_sq_adj = 1 - (((1 -R_sq)*(n-1))/(n-k-1))
 
%RMSE(root mean square)method
RMSE = (SSE/n)^(0.5)

PLOT 4:

OUTPUT:

R-square = 0.9967

Adjusted R-square = 0.9967

Root mean squared error (RMSE) = 5.4277

#When r square is greater than 0.95 the fit is assumed to be good.

EXPLANATION:

1. In every program, we first load the data from the data file.

2. Then, we use the commands polyfit and polyval which specifies the degree of polynomial and evaluate the linear and cubic polynomial.

3. Now, when program 1 produces the warning , we correct it by using the CENTERING AND SCALING method in program 2 (which uses the error estimation structure and the input

matrix of mean)and after plotting it their, is no warning occured. Then we compare our curve with the original data set so that difference can be seen easily.

4. In program 3, we have used the polynomial split technique and I have plit it in two polynomials. By using this method a better curve fit is achieved and it can be observed when we

compare the original data set with the curve fit.

5. At last, in program 4 we have calcualated the different parametres (R-square, Adjusted R-square, RMSE and the SSE) and a by observing the value our fit can be assumed good.

Need for curve fitting:

Curve fitting, also known as regression analysis, is used to find the "best fit" line or curve for a series of data points. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve.

For many applications we need to find a mathematical formula that adheres to a set of data points. This allows us to plot additional points between the collected ones.

CONCLUSION: Curve fitting of the given data is done by suitable methods and techniques to fit the linear and cubic curve.

And the plot to fit linear and cubic polynomial are made.

REFERENCES:

https://en.wikipedia.org/wiki/Curve_fitting

https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

https://www.mathworks.com/help/curvefit/curve-fitting.html

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.