Project

Program to curve fit a linear and cubic polynomial using MATLAB

Curve fitting - Curve fiiting is a process of constructing a curve to co-relate the real series of data and mathematical function representing the curve fit.Curve fit 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.

Here in this program a linear and cubic polynomial is used:

Linear polynomial equation is `a*x+b`

Cubic polynomial equation is `a*x^3+a*x^2+a*x+b`

Functions used:

Polyfit function polyfit(x,y,n) is used to return the coefficient for a polynomial p(x) of degree n that is a best fit for the data in y.

Polyval function polyval(p,x) evaluates the polynomial p at each point in x.

Data set used has a set of 3200 diffrent values of temperature and their respective specific heat(as specific heat changes with change in temperature)

Code for curve fitting:

clear all
close all
clc

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

%curve fit for linear polynomial
%cp = a*t+b
coefficient1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(coefficient1,temperature);

%curve fit for cubic polynomial
%cp = a*t^3+a*t^2+a*t+b
coefficient2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(coefficient2,temperature);

%comparison of curve fit with the original data for linear  polynomial
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
xlabel('temperature[k]')
ylabel('specific heat[kj/kmol-k]')
legend('original dataset','curve fit')
title('Linear polynomial')

%comparison of curve fit with the original data for cubic polynomial 
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp2,'linewidth',3,'color','r')
xlabel('temperature[k]')
ylabel('specific heat[kj/kmol-k]')
legend('original data set','curve fit')
title('Cubic polynomial')

Curve fit for linear polynomial:

Curve fit for cubic polynomial:

Alternate method:

We can do the curve fitting using an alternate method called splitwise method.In this method the data set is taken in splits.However,in this program we have taken the data set in two splits(0-1600)(1600-3200)

Code for curve fitting using splitwise method :

%splitwise curve fitting for linear polynomial
coefficient3 = polyfit(temperature(1:1600),cp(1:1600),1);
predicted_cp3 = polyval(coefficient3,temperature(1:1600));

figure(3)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature(1:1600),predicted_cp3,'linewidth',3,'color','r')
xlabel('temperature[k]')
ylabel('specific heat[kj/kmol-k]')
legend('original dataset','curve fit')
title('Linear polynomial')

%splitwise curve fitting for cubic polynomial
coefficient4 = polyfit(temperature(1600:3200),cp(1600:3200),3);
predicted_cp4 = polyval(coefficient4,temperature(1600:3200));

figure(4)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature(1600:3200),predicted_cp4,'linewidth',3,'color','r')
xlabel('temperature[k]')
ylabel('specific heat[kj/kmol-k]')
legend('original dataset','curve fit')
title('Cubic polynomial')

Linear curve fit using splitwise method:

Cubic curve fit using splitwise method:

Fitness character parameters:

Four parameters are used to measure the fitness characters of a curve

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

2. R-square

3. Adjusted R-square

4. Root mean squared error(RMSE)

Sum of squatres 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.

`SSE= n sum i=1 w i (y i − ˆ y i ) 2`

R -square:

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

`SR= n sum i=1 w i ( ˆ y i − ‾ y ) 2`

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

`SST= n sum i=1 w i (y i − ‾ y ) 2`

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

R-square`= (SSR)/(SST) =1− (SSE)/( SST)`

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.

Adjusted R-square`=1− SSE(n−1)/ (SST(v)`

Root mean squated 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

`RMSE=s=sqrtMSE`

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

How to make a curve fit perfectly?

Curve fit can be made perfect by interpolating the data sets.By interpolation the error can be minimised and it is also possible to reduce the errors to zero.

How to get the best fit?

The best fit can be obtained by increasing the order of polynomial and also by splitting the curve fits into multiple number of polynomials for different sets of temperature and specific heat values,

What could be done to improve the cubic fit?

The cubic fit can be improved by splitting the curve fits into multiple number of polynomials for different sets of temperature values.It can also be improved by reducing the errors between the original data set and curve fit value.

Refrence:

https://in.mathworks.com/matlabcentral/answers/349776-how-to-find-the-best-curve-fit-for-a-set-of-data

https://in.mathworks.com/help/curvefit/evaluating-goodness-of-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.