Project

Curve fitting Using MATLAB

Curve fitting is the process of constructing a curve that has the best fit to a series of data points. The curve fits all data points for information or the set of data points.

Let us consider a polynomial function `f(x)`

now if the function is linear then `f(x)` can be represented as

`f(x) =ax+b`

now if the function is a two-degree polynomial then `f(x)` can be represented as

`f(x) =ax^2+bx+c`

similarly, if the function is a three-degree polynomial then `f(x)` can be represented as

`f(x) =ax^3+bx^2+cx+d`

and so on.

now let us consider that we have some sort of data for two variables and we want to get an equation to relate those variables we can do this by curve fitting.

In MATLAB by using polyfit command we can obtain the values of the coefficients and once we get the coefficients by using polyval command we can generate the predicted data points, comparing with the original dataset and changing the degree of the equation for prediction we can get the perfect curve fitting.

In this project, we will

  • Calculate Correlation Coefficients
  • Fit a Polynomial to the Data
  • Plot and Calculate Confidence Bounds

The MATLAB program is as follows

%curve fitting
clear all
close all
clc

%prepareing the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);

%linear curve fit 
[co_eff,error] = polyfit(temp,cp,1);
[predicted_cp,delta] = polyval(co_eff,temp,error);

%cubic curve fit 
[co_eff1,error1] = polyfit(temp,cp,3);
[predicted_cp1,delta1] = polyval(co_eff1,temp,error1);


%compare my curvefit with original data
figure(1)
plot(temp,cp,'linewidth',3)
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
plot(temp,predicted_cp1,'linewidth',3,'color','g')
plot(temp,predicted_cp+delta,'r:')
plot(temp,predicted_cp1+delta1,'g:')
plot(temp,predicted_cp-delta,'r:')
plot(temp,predicted_cp1-delta1,'g:')
title('Curve fitting')
xlabel('Temperature (K)')
ylabel('Specific heat (kJ/kmol-K)')
legend('original data','Linear curve fit','cubic curve fit','95% confidance limit for linear fit','95% confidance limit for cubic fit')


Output

Confidence bounds are confidence intervals for a predicted response. The width of the interval indicates the degree of certainty of the fit.

The above code uses an interval of ±Δ which corresponds to a 95% confidence interval for large samples.

The curve fit obtained for linear and cubic in Curve fitting toolbox in MATLAB are as follows

To make the curve more perfectly fit we should try for different degrees of polynomial or using different degrees of the polynomial in different ranges of temperature.

References :-

https://www.mathworks.com/help/matlab/data_analysis/programmatic-fitting.html#f1-7407

https://www.mathworks.com/help/curvefit/referencelist.html?type=function

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.