Project

Curve Fitting in MATLAB

OBJECTIVES:

  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 explain the parameters used to measure the fitness characteristics for both the curves.

INTRODUCTION:

Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.

PROCEDURE:

  1. We load the data of temperature and specific heat into the program.
  2. We assign the values for the independent and dependent variables, temperature and specific heat respectively.
  3. We use the 'polyfit' command to predict the co-effiecients and 'polyval' command to predict the values of specific heats.
  4. We plot the different observed dataset and predicted datasets using 'plot' command.
  5. We calculate the different errors and determine the maximum error percentage.

1. CURVE FITTING FOR LINEAR, QUADRATIC, CUBIC & 4th ORDER POLYNOMIAL

CODE:

clear all
close all
clc

%PREPARING THE DATA
cp_data=load('data'); %Loading data 
temperature=cp_data(:,1); %Loading temperature from the first column
cp=cp_data(:,2); %Loading Specific Heat from second column

%CURVE FITTING
%Linear (cp = aT + b)
coeffs=polyfit(temperature,cp,1); %Finding the co-efficients
predicted_cp=polyval(coeffs,temperature); %Finding the predicted specific heat

%Quadratic(cp= aT^2+ bT + c)
coeffs1=polyfit(temperature,cp,2);
predicted_cp1=polyval(coeffs1,temperature);

%cp and predicted cp comparison
%Plotting
plot(temperature,cp,'r','linewidth',3)%Plot on observed data
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
hold on

plot(temperature,predicted_cp,'k','linewidth',3)%Plot on linear data
error=((cp-predicted_cp)./cp).*100;
fprintf('Maximum error percentage= ');
disp(max(error));
hold on

plot(temperature,predicted_cp1,'b','linewidth',3)%Plot on quadratic data
error=((cp-predicted_cp1)./cp).*100;
fprintf('Maximum error percentage= ');
disp(max(error));
legend('Original dataset','Linear Fit','Quadratic Fit')

%Error for Linear Fit
error1=cp-predicted_cp;
sse_linear=sum(error1.^2);
ssr_linear=sum((cp-mean(cp)).^2);
sst_linear=ssr_linear+sse_linear;
r_linear_square=ssr_linear/sst_linear;
rsme_linear=sqrt(sse_linear/3200);

%Error for Quadratic Fit
error2=cp-predicted_cp1;
sse_quadratic=sum(error2.^2);
ssr_quadratic=sum((cp-mean(cp)).^2);
sst_quadratic=ssr_quadratic+sse_quadratic;
r_quadratic_square=ssr_quadratic/sst_quadratic;
rsme_quadratic=sqrt(sse_quadratic/3200);

OUTPUT:

Maximum error percentage for linear fit = 2.4188

Maximum error percentage for quadratic fit = 1.8753

We then define the different mathematical relationships to determine the best fit.

1. error = cp - predicted_cp

2. SSE = sum of squared errors

3. SSR = sum of squares of the regression = (cp-mean(cp))^2

4. SST = SSE + SSR

5. R-Square = SSR/SST

6. Root Mean Square error(RMSE) = sqrt(SSE/n)

where n = 3200

The values obtained is shown below:

Curve fitting for cubic fit:

Maximum error percentage= 0.8865

Curve fitting for polynomial of order 4:

Maximum error percentage= 1.3046

2. CURVE FITTING WITH SPLITWISE POLYNOMIAL:

CODE:

clear all 
close all
clc

%SPLITWISE POLYNOMIAL

cp_data=load('data'); %Loading data
temperature=cp_data(:,1); %Loading temperature from the first column
cp=cp_data(:,2);  %Loading Specific Heat from second column

%SPLITTING THE CURVE INTO 4 POLYNOMIALS

t1=cp_data(1:800,1);
cp1=cp_data(1:800,2);

t2=cp_data(801:1600,1);
cp2=cp_data(801:1600,2);

t3=cp_data(1601:2400,1);
cp3=cp_data(1601:2400,2);

t4=cp_data(2401:3200,1);
cp4=cp_data(2401:3200,2);

[coeffs1,s1,a1]=polyfit(t1,cp1,3);
[coeffs2,s2,a2]=polyfit(t2,cp2,3);
coeffs3=polyfit(t3,cp3,2);
coeffs4=polyfit(t4,cp4,2);

predicted_cp1=polyval(coeffs1,t1,[],a1);
predicted_cp2=polyval(coeffs2,t2,[],a2);
predicted_cp3=polyval(coeffs3,t3);
predicted_cp4=polyval(coeffs4,t4);

%PREDICTED_CP VECTOR FOR ERROR CALCULATION
predicted_cp=[predicted_cp1;predicted_cp2;predicted_cp3;predicted_cp4];

%PLOTTING
plot(temperature,cp,'b','linewidth',3)
hold on
plot(t1,predicted_cp1,'r-.','linewidth',2)
plot(t2,predicted_cp2,'r-.','linewidth',2)
plot(t3,predicted_cp3,'r-.','linewidth',2)
plot(t4,predicted_cp4,'r-.','linewidth',2)

xlabel('Temperature [K]')
ylabel('Specific Heat KJ/Kmol-K')
legend('Original dataset','CurveFit')

%ERROR CALCULATION
error=((cp-predicted_cp)./cp).*100;
fprintf('Maximum error percentage= ');
disp(max(error));

OUTPUT:

Maximum error percentage = 0.4673

VALIDATION USING CURVE FITTING TOOLBOX:

1. Linear fit

2. Quadratic Fit

3. Cubic Fit

4. Polynomial of Order 4

REFERENCES:

1. https://www.emathzone.com/tutorials/basic-statistics/curve-fitting-and-method-of-least-squares.html#ixzz5u0dmLihc

2. https://www.originlab.com/index.aspx?go=Products/Origin/DataAnalysis/CurveFitting

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

4. https://blog.minitab.com/blog/adventures-in-statistics-2/curve-fitting-with-linear-and-nonlinear-regression

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.