Project

Curve Fitting

AIM:

Write a code to fit a linear and cubic polynomial for the Cp data.

THEORY:

Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.

QUESTIONS AND ANSWERS:

1) How to make a curve fit perfectly?

In order to have a curve to fit perfectly over the data points provided, the error should be negligible, i.e. the values of R-Square should be exactly ‘1’. Generally, minimal error is taken into account for best fit but to obtain perfect fit, all the errors including centering and scaling errors should be nulled.

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

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

To improve the cubic curve fit we can us centering and scaling technique and also we can increase the number of iterations.

GOVERNING EQUATIONS:

Sum of squared errors method:

SSE = ∑ni=1(Y(i) - f(x(i)))2

R-square method:

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

where,

SSR = ∑ni=1(f(x(i) - Mean)2

SST = SSR + SSE

Adjusted R-square method:

Ra2 =1-(1-R2)⋅(n-1)n-k-1">=`1 - [((1-R^2)(n-1))/(n-k-1)]`

where,

n = number of datapoints

k = number of independent variables

Root Mean Square Error Method:

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

OBJECTIVE:

The main objective of the project to plot the linear and cubic curve fit for the cp data and find the goodness of the fits by different methods like R square,Adjusted R square and Root mean squared error .

WORKFLOW OF THE PROJECT:

  1. Initially we load the value of cp form the data file.
  2. Then we save the value of temperature and cp from the data loaded to a variable.
  3. Then we plot the curve for original and predicted data for linear and cubic polynomials.
  4. At last, we find the goodness of the fit of both the curve by different methods.

Link for data file that is loaded in the program is shown below:

https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view

PROGRAMMING LANGUAGE USED – MATLAB

MATLAB CODE:

%A program for curve fitting for linear and cubic polynimials
clear all 
close all
clc

%loading the data
c_pdata = load('data');
temperature = c_pdata(:,1);
cp = c_pdata(:,2);

figure(1)
coefficient_1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(coefficient_1,temperature);

%plotting for linear curve fit
hold on
plot(temperature,cp,'linewidth',3);
plot(temperature,predicted_cp1,'linewidth',3,'color','r');
xlabel('Temperature[K]');
ylabel('specific heat[KJ/Kmole-K]');
title('Curve Fitting For Linear Polynomial');
legend('original data','predicted data','location','best');

figure(2)
coefficient_3 = polyfit(temperature,cp,3);
predicted_cp3 = polyval(coefficient_3,temperature);

%plotting for cubical curve fit
hold on
plot(temperature,cp,'linewidth',3);
plot(temperature,predicted_cp3,'linewidth',3,'color','r');
xlabel('Temperature[K]');
ylabel('specific heat[KJ/Kmole-K]');
title('Curve Fitting For cubic Polynomial');
legend('original data','predicted data','location','best');

%Getting the mean of the cp value
Mean = mean(cp);

%CALCULATING GOODNESS OF THE FIT
%for linear curve fit
%initializing the variables
%sum of squared errors
sse_l = 0;
%sum of squared regression
ssr_l = 0;

for i=1:length(cp)
    sse_l = sse_l + (cp(i)-predicted_cp1(i))^2;
    ssr_l = ssr_l + (predicted_cp1(i)-Mean)^2;
end

%total sum of squares
sst_l = sse_l + ssr_l;

fprintf('For Linear Polynomial \n');
% R square method
R_square_l = ssr_l/sst_l;
fprintf('The value of R square is %f \n ',R_square_l);

%R square adjusted method
%no. of datapoints
n = 3200;
%no. of independent variable assumed
k = 1;
R_sqr_adj_l = 1-((1-R_square_l)*(n-1)/(n-k-1));
fprintf('The value of R sqr_adj is %f \n ',R_sqr_adj_l);

%root mean square method 
rsme_l = sse_l/n;
fprintf('The value of RSME is %f \n \n ',rsme_l);


%for cubic curve fit
%initializing the variables
%sum of squared errors
sse_c = 0;
%sum of squared regression
ssr_c = 0;

for i=1:length(cp)
    sse_c = sse_c + (cp(i)-predicted_cp3(i))^2;
    ssr_c = ssr_c + (predicted_cp3(i)-Mean)^2;
end

%total sum of squares
sst_c = sse_c + ssr_c;

fprintf('For Cubic Polynomial \n');
%calculatingt the value of R square
R_square_c = ssr_c/sst_c;
fprintf('The value of R square is %f \n',R_square_c);

%R square adjusted method
%no. of datapoints
n = 3200;
%no. of independent variable assumed
k = 1;
R_sqr_adj_c = 1-((1-R_square_c)*(n-1)/(n-k-1));
fprintf('The value of R sqr_adj is %f \n ',R_sqr_adj_c);

%root mean square method 
rsme_c = sse_c/n;
fprintf('The value of RSME is %f \n ',rsme_c);

CODE EXPLAINATION:

  1. Initially we load the data of cp by using the load command.
  2. Then we fit the curve by using the polyfit command.
  3. After that we obtained the value of the unlnown coefficient of the equation by using the polyval command.
  4. After that we plot the curve of original data and predicted data for linear polynomial and similar we can do for cubic polynomial.
  5. Then we find the mean of all the cp data by using the mean command.
  6. Atlast, we find the goodness of fit by calculating it with different methods like R square,Adjusted R square and Root mean squared error for both linear and cubic polynomials.

OUTPUT:

Output plot for linear curve fit is shown below:

Output plot for cubic curve fit is shown below:

The goodness of the fit value obtained for linear and cubic polynomials by R square,Adjusted R square and Root mean squared error methods is shown below:

CONCLUSION:

Thus we have seen the plot of curve fit for linear and cubic polynomial and also the goodness of the fits for both of them was calculated.

WARNINGS OBTAINED AND IT'S SOLUTION:

There was a warning obtained that the polynomial is badly conditioned.To overcome this problem we can use scaling and centering technique.


REFERENCES:

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

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

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.