Project

Curve Fitting

AIM:

To write a code to fit the linear and cubic polynomial for specific heat data.

COMMANDS USED:

1. Load: It was used to load the data of the file into the matlab

2. p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1.

3.[p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation.

4. y = polyval(p,x) evaluates the polynomial p at each point in x. The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an nth-degree polynomial

PROGRAM:

% Curve Fitting using the Temp-Specific heat values

clear all

close all

clc

% Available Data

specific_heat_values = load('data');

temperature_values = specific_heat_values(:,1);

specific_heat = specific_heat_values(:,2);

% Curve Fit 

% Linear function

coefficients = polyfit(temperature_values,specific_heat,1);

predicted_specific_heat = polyval(coefficients, temperature_values);

% Cubic equation

[coefficients_c,s,mu] = polyfit(temperature_values,specific_heat,3);%Scaling and centering

% [p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation,

predicted_specific_heat_c = polyval(coefficients_c, temperature_values,[],mu);

% Comparison of curve fit to the original data

plot(temperature_values,specific_heat,'linewidth',3,'color','r')

hold on

plot(temperature_values,predicted_specific_heat,'linewidth',3,'color','b')

hold on

plot(temperature_values,predicted_specific_heat_c,'linewidth',3,'color','y')

xlabel('Temperature [K]')

ylabel('Specific heat [KJ/Kmol-k]')

legend('Original Data','Curve Fit Linear polynomial','Curve Fit Cubic polynomial','Location','northwest')

title('Curve fit with Linear and Cubic Polynomial')

% Finding the goodness of the curve fit

% This can be done by calculating SSE , R-square, Adjusted R-square, RMSE

% For 1st degree polynomial

[polyfit1,gof] = fit(temperature_values,predicted_specific_heat,'poly1')

% For 3rd degree polynomial

[polyfit2,gof] = fit(temperature_values,predicted_specific_heat_c,'poly3','normalize','on')

% Using curve fitting toolbox

cftool


PROCEDURE:

Step 1: Loading the data file and assigning them to the variables

specific_heat_values = load('data');

temperature_values = specific_heat_values(:,1);

specific_heat = specific_heat_values(:,2);

Step 2: Using the polyfit command to obtain the coefficients for the polynomial of the 1st degree that is the best fit for the data

coefficients = polyfit(temperature_values,specific_heat,1);

Step 3: Using the polyval command to evaluate the polynomial at each point of the coefficient.

predicted_specific_heat = polyval(coefficients, temperature_values);

Step 4: Similarly using the polyfit and polyval for the 3rd degree polynomial but with centering and scaling as it imporves the numerical properties of both polynomial and fitting algorithm.

[coefficients_c,s,mu] = polyfit(temperature_values,specific_heat,3);
predicted_specific_heat_c = polyval(coefficients_c, temperature_values,[],mu);

Step 5: Plotting the graph between the original data and the obtained data from the curve fit.

Step 6: Determining the goodness of the fit by using the below syntax that determines the sum of squares of error (SSE), R-square, Adjusted R-square, Root mean square error(RMSE)

[polyfit1,gof] = fit(temperature_values,predicted_specific_heat,'poly1')
[polyfit2,gof]=fit(temperature_values,predicted_specific_heat_c,'poly3','normalize','on')

RESULTS:

1. Plot for Original data and the linear and cubic degree polynomial curve fit

2. Goodness of the fit

polyfit1 =

Linear model Poly1:
polyfit1(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.09875 (0.09875, 0.09875)
p2 = 995.2 (995.2, 995.2)

gof =

struct with fields:

sse: 1.5737e-22
rsquare: 1
dfe: 3198
adjrsquare: 1
rmse: 2.2183e-13

polyfit2 =

Linear model Poly3:
polyfit2(x) = p1*x^3 + p2*x^2 + p3*x + p4
where x is normalized by mean 1900 and std 923.9
Coefficients (with 95% confidence bounds):
p1 = 15.03 (15.03, 15.03)
p2 = -25.19 (-25.19, -25.19)
p3 = 64.19 (64.19, 64.19)
p4 = 1208 (1208, 1208)

gof =

struct with fields:

sse: 6.0807e-22
rsquare: 1
dfe: 3196
adjrsquare: 1
rmse: 4.3619e-13

3. Goodness of fit using curve fit toolbox

GOODNESS OF FIT STATISTICS:

  • 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.

  • 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). SSR is defined as

    SST is also called the sum of squares about the mean, and is defined as

    where SST = SSR + SSE. Given these definitions, R-square is expressed as

    R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model.

  • Adjusted R-square

    The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested — that is, a series of models each of which adds additional coefficients to the previous 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. Negative values can occur when the model contains terms that do not help to predict the response.

  • Root Mean Squared Error

    This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as

    where MSE is the mean square error or the residual mean square

    Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.

1. How to male a curve fit perfectly?

It can be done by obtaining the data same as the original data such that no error is found

2. How to get the best fit?

It can be done by reducing the error as much as possible by using different methods and to find out the method in which the error is less i.e., the obtaining data is near to the original data. The different methods are Interploation and smoothing, Linear and non-linear regression or curve fitting.

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

There are different ways to improve the cubic fit :

  • Centering and scaling
  • By using one type polynomial for one set of range and other polynomial for other set of range

Here i used the centering and scaling method.

ERRORS FACED:

1. Error while using the 3rd degree polynomial

Solution:

% By centering and scaling
[coefficients_c,s,mu] = polyfit(temperature_values,specific_heat,3)

CONCLUSION:

Thus the code for the curve fit is made using MATLAB.

REFERENCES:

1. https://www.statisticshowto.com/curve-fitting/

2. https://www.iiserpune.ac.in/~bhasbapat/phy221_files/curvefitting.pdf

3. https://www.mathworks.com/help/matlab/ref/polyfit.html?s_tid=answers_rc2-2_p5_MLT

4. https://www.mathworks.com/help/matlab/ref/polyval.html

5. https://www.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html

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

7. https://www.mathworks.com/help/curvefit/evaluate-a-curve-fit.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.