Project

CURVE FITTING USING MATLAB

Aim

Determine the best Curve (linear, quadratic & cubic) that fits to given data Using MATLAB

Objective

  • Write a code to fit linear and cubic fit polynomial for the cp data
  • Plot the linear and cubic fit curves along with the raw data points
  • Understand how the value of Cp (specific heat) changes w.r.to temperature

Theory

Curve fitting is the process of constructing a curve or mathematical functions which posses 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 process. Upon solving multiple system of equation, we can find the coefficients using the either by code or by mathematics. Here choosing a best fit is done by following criteria.

  1. The sum of square due to error (SSE)
  2. R-square
  3. Adjusted R-square
  4. Root mean square error (RMSE)

MATLAB CODE

clear all
close all
clc

%Preparation of data
load ('data');
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);

%curve fitting of linear,quadratic and cubic polynomials

%[1] cp = a*T+b (linear polynomial)
coefficients_1 = polyfit(temp,cp,1);
predicted_cp_1 = polyval(coefficients_1,temp);
Error_1 = (cp - predicted_cp_1);
E1 =abs(Error_1) %absolute value of error in linear polynomial


%[2] cp = a*T^2+b*T+c (quadratic polynomial)
coefficients_2 = polyfit(temp,cp,2);
predicted_cp_2 = polyval(coefficients_2,temp);
Error_2 = cp - predicted_cp_2;
E2 =abs(Error_2) %absolute value of error in quadratic polynomial

%[3] cp = a*T^3+b*T^2+c*T+d (cubic polynomial)
coefficients_3 = polyfit(temp,cp,3);
predicted_cp_3 = polyval(coefficients_3,temp);
Error_3 = cp - predicted_cp_3;
E3 = abs(Error_3)  %absolute value of error in cubic polynomial


%plotting of polynomials with orginal data set

figure(1)
plot(temp,cp,'color','black','linewidth',3)
hold on
plot (temp,predicted_cp_1,'linewidth',2,'color','green')
plot (temp,predicted_cp_2,'linewidth',2,'color','yellow')
plot (temp,predicted_cp_3,'linewidth',2,'color','red')
xlabel ('tempreture  [k]')
ylabel ('specific heat  [kJ/kmol-k]')
legend('Orginal Dataset','Curve fit 1','Curve fit 2','Curve fit 3')
grid on

%to open Curve fit Tool box
cftool(temp,cp)

%table
T1 = table(cp,predicted_cp_2,E1)
T2 = table(cp,predicted_cp_2,E2)
T3 = table(cp,predicted_cp_2,E3)

Explanation of the code

  1. The given data is loaded by using load command and is assigned as cp_data. Data has to columns cp and temperature. Then the temperature and cp value is extracted from the data by : operator this process is called indexing.
  2. linear polynomial a*T+b, quadratic polynomial a*T^2+b*T+c, cubic polynomial is a*T^3+b*T^2+c*T+d. where a,b,c,d are constants.
  3. Then value of coefficient is calculated by using polfit and polyval commands.
  4. Error is calculated from the equation [Error = cp – predicted_cp]
  5. plotting command is used for plotting the temperature and cp / specific heat.
  6. Cftool(temp,cp) is used to open the cf tool in MATLAB, in tool box we can check the closeness of the curve to the orginal dataset.
  7. Table command is used to create table of cp and predicted cp and error

OUTPUTS

  • Plot Obtained
  • workspace
  • Validation of linear, quadratic & cubic polynomial using curve fit tool box in MATLAB

FOR LINEAR POLYNOMIAL

  • SSE = 2.163 e+0.6
  • R-square =0.9249
  • Adjusted R-square =0.9249
  • RMSE =26.01

FOR QUADRATIC POLYNOMIAL

  • SSE =5.401 e+0.5
  • R-square =0.9812
  • Adjusted R-square =0.9812
  • RMSE =13

FOR CUBIC POLYNOMIAL

  • SSE =9.427 e+0.4
  • R-square =0.9967
  • Adjusted R-square =0.9967
  • RMSE =5.431
  • Summery

POLYNOMIAL

SSE

R-SQUARE

Adjusted

R-square

RMSE

Linear

2.163 e+0.6

0.9249

0.9249

26.01

Quadratic

5.401 e+0.5

0.9812

0.9812

13

Cubic

9.427 e+0.4

0.9967

0.9967

5.431

Using four quantities Matlab effectively deduces the goodness of fit, typically is R-square is greater than 0.95 the fit is assumed to be good.

Conclusion

when the degree of polynomial increases the fitness of curve getting more closer and which gives us the best fit on here, we consider only linear,quadratic and cubic polynomials. among these cubic curve shows the best fit towards the given data set.

Questions

  • How to make a curve fit perfectly?

By increasing the order of the polynomial we can fit the curve perfectly

  • How to get the best fit?

The error in each fit determines which is the best fit,the error must reduce while curve fitting.

  • What could be done to improve the cubic fit?

We have to increase the closeness of curve to the orginal data

Reference

[1] YOUTUBE

[2] MATLAB DOCUMENTATION

[3] SKILL LYNC

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.