Project

Curve Fitting using Matlab

ABSTRACT:

The main aim of this project is to write a code to fit a linear and cubic polynomial for the specific heat data set, calculate the goodness of fit using different parameters and different ways to improve the fit. Here, Matlab code is used to generate the linear and cubic polynomial fits and using Curve fitting app the goodness of fit has been calculated.

THEORY:

Curve fitting is the process of constructing a curve or mathematical function that best fits the data points, possibly subjected to constraints. It helps us in understanding the type of relationship between the set of data and helps in predicting the unknown values.

The goodness of fit of a statistical data determines how well it fits a set of observation. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question. There are four methods to quantify the goodness of fit. They are

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

MATLAB CODE:

Curve fitting with linear and cubic polynomials:

%Curve fitting
clear all
close all
clc

%loading of data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%linear polynomial (cp = a*T + b)
%calculation of linear coefficients through polyfit
coeffs1 = polyfit(temperature,cp,1);
%predicting cp values using polyval
pred_cp_values_lp = polyval(coeffs1,temperature);

%Cubic polynomial (cp = a*T^3+b*T^2+c*T+d)
coeffs2 = polyfit(temperature,cp,3);
pred_cp_values_cp = polyval(coeffs2,temperature);

figure(1)
%plotting of actual cp values vs temperature
plot(temperature,cp,'linewidth',3,'color','r')
hold on
%plotting of linear predicted cp values
plot(temperature,pred_cp_values_lp,'linewidth',2,'color','b')
%plotting of cubic predicted cp values
plot(temperature,pred_cp_values_cp,'linewidth',2,'color','g')

title('Comparing of linear and cubic polynomial curve fits with original data')
xlabel('Temperature[kelvin]')
ylabel('Specific Heat[KJ/KmolK]')
legend('Original dataset','Linear Curvefit','Cubic Curvefit','location','southeast')

Steps:

  • Data set with temperature and specific heat has been loaded. Here, the first column is temperature and the second column is of specific heat
  • Initially when the polynomial is considered as linear, using polyfit function with degree 1 has been given as input. It calculates the coefficients of a and b in the linear polynomial.
  • Next coefficients were calculated for the cubic polynomial by giving degree 3 as input in polyfit function.
  • Specific heat values can be predicted using the polyval function for both linear and cubic polynomials.
  • Predicted specific heat values have been plotted against the original data set and found that degree 3 polynomial have values closer to the original dataset.

PLOTS FOR LINEAR AND CUBIC POLYNOMIAL:

a)Curve fitting with a linear polynomial:

b)Curve fitting with a cubic polynomial:

GOODNESS OF FIT USING CURVE FITTING TOOLBOX:

a)Curve fitting tool for linear polynomial:

Value for the R-square came to be 0.9249 which is not a good fit. A good fit has an R-square value of more than 0.95.

b)Curve fitting tool for cubic polynomial:

Value of R-square is 0.9967 which indicates it as a good fit.

But in the results column, it mentions that the equation is badly conditioned.

Now that the equation is not proper we can divide the data set into two parts and fit a curve using Matlab code:

MATLAB CODE:

% Curve fitting by breaking the given data set to multiple dataset
clear all
close all
clc

%loading of Cp vs Temperature data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

%seperation of dataset into two equal halves
cp01 = cp([1:1600]);%Cp values for temps from 0 to 1600
temperature01 = temperature([1:1600]);
cp02 = cp([1601:3200]);%Cp values for temps from 1601 to 3200
temperature02 = temperature([1601:3200]);

plot(temperature,cp,'linewidth',3,'color','r')
hold on
%predicting CP values using fit function for first half
[predict_cp1,GOF1] = fit(temperature01,cp01,'poly2')
plot(predict_cp1,'b')
%predicting CP values using fit function for second half
[predict_cp2,GOF2] = fit(temperature02,cp02,'poly2')
plot(predict_cp2,'g')

hold off
title('Curvefitting of Temperature vs Specific heat')
xlabel('Temperature (Kelvin)')
ylabel('Specific heat [KJ/KMOL-K]')
legend('original Specific heat data','Trend of CP for temp 1-1600' , 'Trend of CP for temp from 1601-3200 ','location','southeast')

STEPS INVOLVED:

1) Data set has been divided into two equal halves from 1 to 1600 and 1601 to 3200

2) Using fit function CP values and goodness of fit has been found out for both the halves.

3) Curves have been plotted for both the sets.

PLOT:

Measurement of the goodness of fits for both halves:

R-squares for the first half is 0.9959 and for the second half is 0.9997.

Both the values are greater than 0.95 indicates that it is a good fit.

Therefore we can say that we can achieve a good fit either by increasing the degree of polynomial or by dividing the total data set into subsets.

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.