Project

Curve fitting

Aim:1.To Write code to fit a linear and cubic polynomial for the Cp data.

2.To Plot the linear and cubic fit curves along with the raw data points.

3.To Write a code to show splitwise method.

4.Explain the parameters used to measure the fitness characteristics for both the curves.

Software used: octave

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.

Fitting Functions to data points

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

1.The first degree polynomial equation is y=ax+b

2.The second degree polynomial equation is y=ax2+bx+c

3.The third degree polynomial equation is y=ax3+bx2+cx+d

There are four quantities to measure the goodness of fit

1.The sum of squares due to error (SSE)

2.R-square

3.Adjusted R-square

4.Root mean squared error (RMSE)

1.Sum of Squares due to error (SSE): 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 labelled as SSE. A value closer to 0 indicates a better fit.

2.R-Square: This statistic measures how successful the fit is 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).

Where, SSR=Sum of squares of the regression

SST=SSR+SSE

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

3.Adjusted R-Square: It measures the proportion of variation explained by only those independent variables that really help in explaining the dependent variable. It penalizes you for adding independent variable that do not help in predicting the dependent variable.

Adjusted R-Squared can be calculated mathematically in terms of sum of squares. The only difference between R-square and Adjusted R-square equation is degree of freedom.


Adjusted R-squared value can be calculated based on value of r-squared, number of independent variables (predictors), total sample size.

Where, N=total sample size

P=number of predictors

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

4.Root Mean Squared Error (RMSE): 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,

An RMSE value closer to 0 indicates a better fit.

Code for Curve Fitting

clear all
close all
clc

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

%linear fit
%cp=a*T+b
coeffs1=polyfit(temperature,specific_heat,1);
predicted_cp1=polyval(coeffs1,temperature);

%comparing the linear fit with original data 
figure(1)
hold on
plot(temperature,specific_heat,'linewidth',3)
plot(temperature,predicted_cp1,'linewidth',3)
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
legend('Original Dataset','Linear Fit')
title('Temperature vs Specific Heat')

%Checking the fitness of the curve by R_square method for linear fit
Error1=(specific_heat-predicted_cp1);
sse=0;
ssr=0;
M=mean(specific_heat);
for i=1:length(specific_heat)
  SSE1=sse+Error1(i)^2;
  SSR1=ssr+(predicted_cp1(i)-M)^2;
endfor
SST1=SSR1+SSE1;
R_square_linear=SSR1/SST1


%cubic fit
%cp=a*T^3+b*T^2+c*T+d
coeffs3=polyfit(temperature,specific_heat,3);
predicted_cp3=polyval(coeffs3,temperature);

%comparing cubic fit curve with original data
figure(2)
hold on
plot(temperature,specific_heat,'linewidth',3)
plot(temperature,predicted_cp3,'linewidth',3)
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
legend('Original Dataset','Cubic Fit')
title('Temperature vs Specific Heat')

%Checking the fitness of the curve by R_square method for cubic fit
Error3=(specific_heat-predicted_cp3);
sse=0;
ssr=0;
M=mean(specific_heat);
for i=1:length(specific_heat)
  
  SSE3=sse+Error3(i)^2;
  SSR3=ssr+(predicted_cp3(i)-M)^2;
endfor

SST3=SSR3+SSE3;
R_square_cubic=SSR3/SST3


%Comparision between linear and cubic fit curve
figure(3)
hold on
plot(temperature,specific_heat,'linewidth',3)
plot(temperature,predicted_cp1,'linewidth',3)
plot(temperature,predicted_cp3,'linewidth',3,'color','g')
legend('Original Dataset','Linear Fit','Cubic Fit')
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
title('Temperature vs Specific Heat')

Explanation of code

1.Start the program with clear all, close all and clc commands.

2.Data file should be in the same registry as that of the octave files registry and the load command is used to load the values .

3.For first degree polynomial we have the equation y=ax+b.

4.For linear fit, we make use of polyfit command to find the coefficients and are stored in coeffs1 variable. Using these coefficients, we find new Cp values for linear fit using the polyval command and are stored in predicted_cp1 variable.

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

Polyval-It evaluates a polynomial for a given set of x values.

5.A graph 'Temperature vs Specific Heat' is plotted to compare the original dataset and linear fit curve.

6.Calculating the R_square value for linear polynomial.

7.The third degree polynomial equation is y=ax3+bx2+cx+d.

8.For cubic fit, we make use of polyfit command to find the coefficients and are stored in coeffs3 variable. Using these coefficients, we find new Cp values for cubic fit using the polyval command and are stored in predicted_cp3 variable.

9.A graph 'Temperature vs Specific Heat’ is plotted to compare the original dataset and cubic fit curve.

10.Calculating the R_square value for cubic polynomial.

11.A graph is plotted to compare the original dataset, linear fit curve and cubic fit curve.

Plots

Plot for the linear fit curve along with the raw data points

Plot for the cubic fit curve along with the raw data points

Plot to compare the original dataset, linear fit curve and cubic fit curve

code to show splitwise method

clear all
close all
clc

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

%splitting data
temperature1=cp_data(1:800,1);
specific_heat1=cp_data(1:800,2);

temperature2=cp_data(801:1600,1);
specific_heat2=cp_data(801:1600,2);

temperature3=cp_data(1601:2400,1);
specific_heat3=cp_data(1601:2400,2);

temperature4=cp_data(2401:3200,1);
specific_heat4=cp_data(2401:3200,2);

%Considering the cubic polynomial ; cp=a*T^3+b*T^2+c*T+d
coeffs_1=polyfit(temperature1,specific_heat1,3);
predicted_cp3_1=polyval(coeffs_1,temperature1);

coeffs_2=polyfit(temperature2,specific_heat2,3);
predicted_cp3_2=polyval(coeffs_2,temperature2);

coeffs_3=polyfit(temperature3,specific_heat3,3);
predicted_cp3_3=polyval(coeffs_3,temperature3);

coeffs_4=polyfit(temperature4,specific_heat4,3);
predicted_cp3_4=polyval(coeffs_4,temperature4);

%plotting
plot(temperature,specific_heat,'linewidth',3,'color','b')
hold on
plot(temperature1,predicted_cp3_1,'linewidth',3,'color','r')
hold on
plot(temperature2,predicted_cp3_2,'linewidth',3,'color','r')
hold on
plot(temperature3,predicted_cp3_3,'linewidth',3,'color','r')
hold on
plot(temperature4,predicted_cp3_4,'linewidth',3,'color','r')
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/Kmol-K)')
axis([0 4000 900 1400])
legend('Original Dataset','Cubic Fit(splitwise)')
title('Temperature vs Specific Heat')

Plot for splitwise method

We can observe that by splitting the data ,the cubic fit results can be improved.

Errors Obtained: Simple errors were obtained and were rectified easily

Result

Conclusion

1.From the plots for linear and cubic curve fits , it can be observed that by increasing the order of the polynomial the error between the curve fit and the original dataset is reduced.

2. The value of R_square for linear fit is found to be 0.95920 and that of cubic fit is found to be 0.99071.We know that value closer to 1 indicates a better fit and from the results it is clear that value of cubic fit is higher than the linear fit.So Cubic curve fit has better fitness characteristics than linear curve fit.

3.The Cubic fit results can be improved by using the splitwise method.

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

Questions

1.How to make a curve fit perfectly?

The fit is perfect when R2 value is exactly equal to 1.This is obtained when the predicted curve and the original dataset curve meet at all the points. So, we need to increase the order of the polynomial for the curve to fit perfectly .

2.How to get the best fit?

Upon finding a curve which satisfies the data points there are four quantities which help us measure the goodness of fit criteria or how well the equation is representing the datapoints.

1.The sum of squares due to error (SSE)

2.R-square

3.Adjusted R-square

4.Root mean squared error (RMSE)

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

The Cubic fit results can be improved by using the splitwise method.

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.