Project

Curve fitting using Matlab

Objective

> Write code to fit a linear and cubic polynomial for the given data.

> To measure the fitness characteristics for both the curves.

> Write code to fit a curve using splitwise method.

> To explain best fit, perfect fit, and improvements for cuibc fit.

Solution

The code to fit a linear and cubic ploynomial for the given data is given below

clear all 
close all
clc

% Input of actual data
Data = load('data');
Temp = Data(:,1);
Cp   = Data(:,2);

% Curve_fit_linear
Coeff_linear = polyfit(Temp,Cp,1);
PCp_linear = polyval(Coeff_linear,Temp);

% Fitness characteristics
S = sum(Cp);
len = length(Cp);
mean = S/len;

% Goodness of fit (Linear)
L_sse = 0;
L_ssr = 0;
for i = 1:len
    L_error = abs((sum((Cp(i) - PCp_linear(i)))));
    L_sse = L_sse + L_error^2;
    L_ssr = L_ssr + sum((PCp_linear(i) - mean)^2);
end
L_sst = L_sse + L_ssr;
Linear_Rsq = L_ssr/L_sst
L_RMSE = (L_sse/len)^0.5;


% Curve_fit_Cubic
[Coeff_cubic,s,mu] = polyfit(Temp,Cp,3);
PCp_cubic = polyval(Coeff_cubic,Temp,s,mu);

% Goodness of fit (Cubic)
C_sse = 0;
C_ssr = 0;
for i = 1:len
    C_error = abs((sum((Cp(i) - PCp_cubic(i)))));
    C_sse = C_sse + C_error^2;
    C_ssr = C_ssr + sum((PCp_cubic(i) - mean)^2);
end
C_sst = C_sse + C_ssr;
Cubic_Rsq = C_ssr/C_sst
C_RMSE = (C_sse/len)^0.5;

% Plot
plot(Temp,Cp,'linewidth',3,'color','r')
hold on
plot(Temp,PCp_linear,'linewidth',3,'color','k')
plot(Temp,PCp_cubic,'linewidth',3,'color','b')
xlabel('Temperature [k]')
ylabel('Specific heat [kJ/kcal*k]')
title('Curve fit comparison')
legend('Actual curve','Linear curve fit','Cubic curve fit')

Explanation

> The data is given as input for the variable temperature and specific heat using 'load' command

> Then the linear curve fit is made using polyfit command.

> From the above snip it can be seen that the temperature and Cp variables are given as inputs with 1 degree, to make a linear curve fit.

> The polyval command is used to evaluates the polynomial 'Coeff_linear' at each point in 'temperature'.

> Then to make the goodness of fit, the following parameters are used.

> M(mean) = `sum_(i=1)^n Cp(i)` / len(Cp)

> E(error) = `abs(y(i) - f(x(i))`

> SSE(sum of squares due to error) = `sum_(i=1)^n E(i)^2`

> SSR(sum of squares of regresssion) = `sum_(i=1)^n (f(x(i))-M)^2`

> SST(sum of squares total) = `SSR +SST`

> R_sq = `(SSR)/(SST)`

> The SSE and SSR values are evaluated using a for loop and the following calculations are made.

> The above similar steps were carried for cubic curve fit with few changes.

> The degree is changed as 3 for cubic curve fit and the syntax is changed as, [p,S,mu] = polyfit(x,y,n) which finds the coefficients of a polynomial in

`hat x = (x-mu1)/(mu2)`

where,

`mu1 = mean(x)`

`mu2 = std(x)`

`mu` is the two-element vector `[mu1,mu2]`.

> This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.

Result

The output of the code is shown below

> From the above plot it is seen that the cubic curve fit is best.

> The Rsq value of the cubic curve fit is closer to 1 than, the Rsq value of linear curve fit shown in the command window, which validates the above point.

Best fit

A best fit can be obtained by increasing the order of the polynomial. As we can see, the cubic curve fit is the best fit when compared to the linear curve fit.

Perfect fit

When the curve fits the actual curve without any centering and scaling errors in a given degree of polynomial it can be considerd as a perfect fit.

To improve cubic fit

A better fit to the same curves can be obtained by splitwise method in which, the dataset is split into equal or unequal intervals and plotting each curve for the intervals. The main advantage is that since, the entire dataset is splitted into multiple sub intervals, each sub curve becomes less complicated. Hence the fit can be better achieved.

Splitwise Curve fit Method

> The steps to make a stepwise cubic curve fit is similar to the previous code but the data points given are split.

> The main factor in splitting the data is the 'number of splits’. More the number of splits, better the output curve. in our case the number of splits is FOUR

> The code to fit a cubic ploynomial using splitwise method for the given data is given below

% Splitwise Curve fit method
clear all 
close all
clc

% Input of actual data
Data = load('data');
Temp = Data(:,1);
Cp   = Data(:,2);

% Splitting the data into 4 sections
% Section_1
Temp1 = Data(1:500,1);
Cp1 = Data(1:500,2);
% Section_2
Temp2 = Data(501:1500,1);
Cp2 = Data(501:1500,2);
% Section_3
Temp3 = Data(1501:2400,1);
Cp3 = Data(1501:2400,2);
% Section_4
Temp4 = Data(2401:3200,1);
Cp4 = Data(2401:3200,2);

% Cubic_Curve_fit
% Section_1
[Coeff_cubic1,s1,mu1] = polyfit(Temp1,Cp1,3);
PCp_cubic1 = polyval(Coeff_cubic1,Temp1,s1,mu1);
% Section_2
[Coeff_cubic2,s2,mu2] = polyfit(Temp2,Cp2,3);
PCp_cubic2 = polyval(Coeff_cubic2,Temp2,s2,mu2);
% Section_3
[Coeff_cubic3,s3,mu3] = polyfit(Temp3,Cp3,3);
PCp_cubic3 = polyval(Coeff_cubic3,Temp3,s3,mu3);
% Section_4
[Coeff_cubic4,s4,mu4] = polyfit(Temp4,Cp4,3);
PCp_cubic4 = polyval(Coeff_cubic4,Temp4,s4,mu4);

% Plotting
plot(Temp,Cp,'linewidth',3,'color','r')
hold on
plot(Temp1,PCp_cubic1,'linewidth',3,'color','k')
plot(Temp2,PCp_cubic2,'linewidth',3,'color','b')
plot(Temp3,PCp_cubic3,'linewidth',3,'color','c')
plot(Temp4,PCp_cubic4,'linewidth',3,'color','m')
xlabel('Temperature [k]')
ylabel('Specific heat [kJ/kcal*k]')
title('Splitwise Curve fit')
legend('Actual curve','1st section','2nd section','3rd section','4th section')

Result

The result of the splitwise cubic curve fit is as shown below

> Since the number of splits is four, it is seen that four sections of the data is plotted.

> The sections of the splitwise curve fit is made to be a single curve and compared to the cubic cuve fit as shown below.

> From the above comparison it is seen that splitwise cubic curve fit is the best fit.

> To derive the same result without using splitwise method it can take upto 9th order polynomial.

References

https://in.mathworks.com/help/matlab/ref/polyfit.html

https://projects.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.