All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
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…
Jeffry Raakesh
updated on 28 Mar 2021
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) = n∑i=1Cp(i) / len(Cp)
> E(error) = |y(i)−f(x(i))|
> SSE(sum of squares due to error) = n∑i=1E(i)2
> SSR(sum of squares of regresssion) = n∑i=1(f(x(i))−M)2
> SST(sum of squares total) = SSR+SST
> R_sq = SSRSST
> 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
ˆx=x−μ1μ2
where,
μ1=mean(x)
μ2=std(x)
μ is the two-element vector [μ1,μ2].
> 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
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Solving 1D Linear convection equation using MATLAB
Objective > Write a Matlab code to simulate a One-dimensional Linear Convection equation. Conditions > The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else > length is L = 1m > First-order forward differencing for the time derivative…
26 Aug 2021 12:45 PM IST
Deriving 4th order approximation of a 2nd order derivative by differencing schemes and evaluating them using MATLAB
Objective > Derive the following 4th order approximations of the second-order derivative. Central difference Skewed right-sided difference Skewed left-sided difference > To prove that the skewed schemes are fourth-order accurate. > Write a program in Matlab to evaluate the second-order derivative of the…
23 Aug 2021 07:21 AM IST
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…
28 Mar 2021 04:00 PM IST
Breaking Ice with Air cushion Vehicle - Find minimum pressure with Newton-Raphson method in Python
Objective > To find out the value of pressure for h = 0.6 ft by the below given equation using Newton-Raphson method. p3(−β2)+(0.4hβ2−σh2r2)p2+(σ2h43r4)p−(σh23r2)3=0 p = Cushion pressure β = Width of the ice wedge …
28 Mar 2021 03:52 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.