Project

Polynomial Curve Fitting using MATLAB

AIM: To perform a curve fit on given data and estimate the fitness characteristics using MATLAB.

OBJECTIVE:

1. To plot the linear and cubic fit curves along with the raw data points. Write a code to show split wise method.

2. To validate the fitness characteristics using the curve fitting toolbox.

3. Analyze the following questions:

- How to make a curve fit perfectly?

- How to get the best fit?

- What could be done to improve the cubic fit?

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.

Consider N points in the curve. Assume y = f(x) is the curve fitting equation.

The error for any point i can be given by:

`Err(i) = |[y(i) - f(x(i))]|`

To remove the modulus sign, we square the error at each point on the curve and add them to get:

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

Assume the proposed equation is a `N^(th)` degree polynomial function as:

`y(x) = A + Bx + Cx^2 + Dx^3 + ... + Nx^(n-1)`

Differentiate each SSE with individual coefficient and equate to zero to find the minima and all the coefficients of the above equation:

`[(frac[delSSE][delA]) , (frac[delSSE][delB]) , (frac[delSSE][delC]) , (frac[delSSE][delD]) , (.) , (.) , (.) , (frac[delSSE][delN])] = 0`

This method is called as Least Square Method and commonly used for fitting a polynomial function.

R-squared: It is the correlation between the given values and the predicted values. It determines to what extent the given data is a successful fit. It is also called the square of multiple correlation and coefficient of multiple determination.

The value of R-squared ranges from 0 (worst possible fit) to 1 (best possible fit) and can be calculated as:

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

`SSR = sum_(i=1)^l [f(x(i)) - Mean]^2`

`SSE = sum_(i=1)^n [y(i) - f(x(i))]^2`

`SST = SSR + SSE`

where;

`SSR` = Sum of squares of regression

`SST` = Sum of squared total

Adjusted R-squared: It is a normalizing variation of R-squared that neglects the variation of R-squared due to insignificant independent variables.

As you keep on adding new variables even if they seem completely unrelated, they will still be related in an infinitesimally small manner.

Adjusted R-squared can be formulated as:

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

where;

`n` = number of data points

`k` = number of independent variables

Root Mean Squared Error: It is the estimate of the standard deviation of a random component in the data. It is also called the fit standard error and standard error of the regression.

Root Mean Squared Error can be formulated as:

`RMSE = sqrtfrac[SSE][n]`

Best Fit: Refers to the curve that has the least error (maximum R-squared value) with respect to the original curve.

Perfect Fit: Refers to the curve that has the zero error (R-squared value = 1) with respect to the original curve.

SOLUTION:

Programming Language: MATLAB

Procedure/Steps:

1. Load the given data file into the working directory. Assign suitable variables to store the given input values.

2. Create a for loop for generation of polynomial degrees as per the requirement.

3. Compute the coefficients and predicted Cp value using polyfit and polyval commands respectively.

polyfit is a function that computes a least squares polynomial for a give set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data.

polyval evaluates a polynomial for a given set of x values.

4. The parameters for the goodness of fit (SSE, R-sqr, Adjusted R-sqr, RMSE) are defined with proper relations.

5. The curves are plotted with the help of plot function.

Code #1: The MATLAB program to fit the curve is shown below.

% Program to fit a linear and cubic polynomial for given data.

% Inputs:

file = load('data');
temp = file(:,1);
Cp = file(:,2);


% Solution:

for i=1:3
coeff = polyfit(temp,Cp,i);
Cp_pr(:,i) = polyval(coeff,temp);

% Goodness of Fit
SSE(i) = sum((Cp - Cp_pr(:,i)).^2)   % Sum of squared errors

SSR(i) = sum((Cp_pr(:,i) - mean(Cp)).^2);   % Sum of squares of regression
SST(i) = SSE(i) + SSR(i);   % Sum of squared total
R_sqr(i) = SSR(i)/SST(i)   % R-squared
 
n = length(Cp_pr(:,i));   % number of data points
k = 1;   % number of independent variables
R_sqr_adj(i) = 1 - ((1-R_sqr(i))*(n-1)/(n-k-1))   % Adjsuted R-squared
 
RMSE(i) = sqrt(SSE(i)/n)   % Root Mean Squared Error


% Plot:

plot(temp,Cp,'linewidth',4,'color','k')
hold on
plot(temp,Cp_pr,'linewidth',4,'linestyle','-.')
hold off
xlabel('Temperature (K)')
ylabel('Entropy (kJ/kmol-K)')
title('Curve Fit')
grid on

end

% Labeling
legend('Dataset','Curve Fit Linear','Curve Fit Quadratic','Curve Fit Cubic')

Output #1: The program is executed and goodness of fit parameters are computed as shown below.

Plot #1: Entropy varies with respect to Temperature for the original and predicted dataset as shown below.

Curve Fitting Toolbox #1:

Linear:

Quadratic:

Cubic:

Curve Fit Validation #1:

Type SSE R-squared Adjusted R-squared RMSE
Linear 2.1630e+06 0.9249 0.9249 26.0072
Quadratic 5.4011e+05 0.9812 0.9812 12.9978
Cubic 1.0617e+05 0.9963 0.9963 5.7636

`SSE_(Cubic) < SSE_(Quadratic) < SSE_(L i n ear)`

`R_(Cubic)^2 > R_(Quadratic)^2 > R_(L i n ear)^2`

`RMSE_(Cubic) < RMSE_(Quadratic) < RMSE_(L i n ear)`

We can observe that the Cubic form is extrapolated with highest accuracy amongst all.

Code #2: The MATLAB program to split wise fit the curve is shown below.

% Program to split wise curve fit a linear and cubic polynomial for given data.

% Inputs:

file = load('data');
temp = file(:,1);
Cp = file(:,2);


% Solution:

% Splitting the data
temp_1 = file(1:800,1);
temp_2 = file(801:1600,1);
temp_3 = file(1601:2400,1);
temp_4 = file(2401:3200,1);

Cp1 = file(1:800,2);
Cp2 = file(801:1600,2);
Cp3 = file(1601:2400,2);
Cp4 = file(2401:3200,2);

coeff = polyfit(temp,Cp,3);
Cp_pr = polyval(coeff,temp);


% Coefficients in split-wise order
coeff_1 = polyfit(temp_1,Cp1,3);
Cp_pr_1 = polyval(coeff_1,temp_1);

coeff_2 = polyfit(temp_2,Cp2,3);
Cp_pr_2 = polyval(coeff_2,temp_2);

coeff_3 = polyfit(temp_3,Cp3,3);
Cp_pr_3 = polyval(coeff_3,temp_3);

coeff_4 = polyfit(temp_4,Cp4,3);
Cp_pr_4 = polyval(coeff_4,temp_4);

for i = 1:length(Cp)

% Goodness of Fit
SSE = sum((Cp(i) - Cp_pr(i)).^2);   % Sum of squared errors

SSR = sum((Cp_pr(i) - mean(Cp(i))).^2);   % Sum of squares of regression
SST = SSE + SSR;   % Sum of squared total
R_sqr = SSR/SST;   % R-squared
 
n = length(Cp);   % number of data points
k = 1;   % number of independent variables
R_sqr_adj = 1 - ((1-R_sqr)*(n-1)/(n-k-1));   % Adjsuted R-squared
 
RMSE = sqrt(SSE/n);   % Root Mean Squared Error
end


% Plot:

plot(temp,Cp,'linewidth',5,'color','c')
hold on
plot(temp_1,Cp_pr_1,'linewidth',3,'linestyle','-.')
hold on
plot(temp_2,Cp_pr_2,'linewidth',3,'linestyle','-.')
hold on
plot(temp_3,Cp_pr_3,'linewidth',3,'linestyle','-.')
hold on
plot(temp_4,Cp_pr_4,'linewidth',3,'linestyle','-.')
hold off
xlabel('Temperature (K)')
ylabel('Entropy (kJ/kmol-K)')
title('Curve Fit (Split Wise)')
legend('Dataset','Curve Fit 1','Curve Fit 2','Curve Fit 3','Curve Fit 4')
grid on

Output #2: The program is executed as per given parameters as shown below.

Plot #2: Entropy varies with respect to Temperature for the original and predicted dataset (in split fashion) as shown below.

Curve Fitting Toolbox #2:

Cp1 vs temp_1

Cp2 vs temp_2

Cp3 vs temp_3

Cp4 vs temp_4

Curve Fit Validation #2:

Split No. SSE R-squared Adjusted R-squared RMSE
1 1.5215e+05 0.9994 0.9994 1.3825
2 0.1047 1.0000 1.0000 0.0115
3 0.1047 1.0000 1.0000 0.0115
4 0.1047 1.0000 1.0000 0.0115

We can observe that the sections after the first split are the same without any variation and the curve fit is extrapolated with the highest accuracy thereafter.

CONCLUSION: From the above results, we can infer that:

- A perfect curve fit can be achieved by incorporating a split wise method by dividing the given data and polynomials with a different degree at each range.

- A best fit can be achieved by mimicking original dataset and High R2 value with less error.

- Improvement of the cubic fit can be achieved by 'Center and Scale' command in curve fit toolbox or by increasing the degree of the polynomial.

REFERENCES:

1. Curve Fitting and criterion to choose the best fit

2. Curve Fitting Toolbox

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.